-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrustwasm2.html
40 lines (34 loc) · 1.05 KB
/
rustwasm2.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
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<canvas id="output"></canvas>
<script type="module">
import { Canvas, default as init } from './pkg/banana.js';
async function run() {
const thingy = await init('./pkg/banana_bg.wasm');
const memory = thingy.memory;
const rustcanvas = Canvas.new();
console.log(rustcanvas);
const w = rustcanvas.width();
const h = rustcanvas.height();
console.log(w + ' ' + h);
const jscanvas = document.getElementById("output");
jscanvas.width = w;
jscanvas.height = 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);
const render = () => {
rustcanvas.draw();
ctx.putImageData(img, 0, 0)
requestAnimationFrame(render);
}
render();
}
run();
</script>
</body>
</html>