-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrustwasm3.html
47 lines (41 loc) · 1.53 KB
/
rustwasm3.html
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
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<canvas id="output"></canvas>
<h3>How does it work?</h3>
<p>The bottom line always has the maximum "heat" value. Heat values are moved up and randomly a bit left and right. When moved there is a chance that the value will decrease a little bit. Heat values of 0 always move straight up.</p>
<p>Looks pretty good for how simple it is. :)</p>
<script type="module">
import { Canvas, default as init } from './pkg/fire.js';
async function run() {
const thingy = await init('./pkg/fire_bg.wasm');
const memory = thingy.memory;
const rustcanvas = Canvas.new();
const w = rustcanvas.width();
const h = rustcanvas.height();
const jscanvas = document.getElementById("output");
jscanvas.width = w;
jscanvas.height = h;
jscanvas.style.width = 1.5*w;
jscanvas.style.height = 1.5*h;
const ctx = jscanvas.getContext('2d');
const memoryPtr = rustcanvas.buf();
const buf = new Uint8ClampedArray(memory.buffer, memoryPtr, w * h * 4);
const img = new ImageData(buf, w, h);
let start = 0;
const render = (time) => {
if(!start) start = time;
const elapsed = time - start;
rustcanvas.update(elapsed);
rustcanvas.draw();
ctx.putImageData(img, 0, 0)
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
run();
</script>
</body>
</html>