-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (52 loc) · 1.6 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
document.querySelector(".buttons").onclick = e => start(+e.target.innerText * 1e6)
var Module = { canvas: canvasWasm }
const width = canvasJs.width,
height = canvasJs.height,
radius = Math.min(width, height) / 2,
ctx = canvasJs.getContext('2d'),
green = pixel(144, 238, 144),
red = pixel(205, 92, 92)
ctx.fillStyle = "white"
function pi(iter) {
let x, y, colour, hits = 0
ctx.fillRect(0, 0, width, height)
for (let i = 0; i < iter; i++) {
x = Math.random()
y = Math.random()
if (x * x + y * y < 1) {
hits++
colour = green
}
else {
colour = red
}
ctx.putImageData(colour, x * width, y * height)
}
return 4 * hits / iter
}
function start(n) {
document.querySelector(".container").classList.add("loading")
setTimeout(() => runSimulation(n), 100)
}
function runSimulation(iter) {
let start = performance.now()
let res = Module.ccall("pi", "number", ["double"], [iter])
showResult("wasm", res, performance.now() - start)
start = performance.now()
res = pi(iter)
showResult("js", res, performance.now() - start)
document.querySelector(".results").classList.remove("hide")
document.querySelector(".container").classList.remove("loading")
}
function showResult(type, result, time) {
document.querySelector(`.${type} .result`).textContent = result.toFixed(4)
document.querySelector(`.${type} .value`).textContent = Math.floor(time)
}
function pixel(r, b, g) {
let col = ctx.createImageData(1, 1)
col.data[0] = r
col.data[1] = b
col.data[2] = g
col.data[3] = 255
return col
}