forked from fabioarnold/nanovg-zig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
72 lines (61 loc) · 2.1 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NanoVG Web Example</title>
<link rel="icon" href="data:,">
<style type="text/css">
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvasgl"></canvas>
<script>
var $canvasgl = document.getElementById("canvasgl");
</script>
<script src="js/wasm.js"></script>
<script src="js/webgl.js"></script>
<script>
const env = {
...wasm,
...webgl,
}
fetchAndInstantiate('zig-out/lib/main.wasm', { env }).then(instance => {
memory = instance.exports.memory;
instance.exports.onInit();
function resize() {
$canvasgl.width = window.devicePixelRatio * window.innerWidth;
$canvasgl.height = window.devicePixelRatio * window.innerHeight;
$canvasgl.style.width = window.innerWidth + "px";
$canvasgl.style.height = window.innerHeight + "px";
instance.exports.onResize(window.innerWidth, window.innerHeight, window.devicePixelRatio);
}
window.addEventListener('resize', resize, false);
resize();
const onAnimationFrame = instance.exports.onAnimationFrame;
document.addEventListener('keydown', e => instance.exports.onKeyDown(e.keyCode));
// document.addEventListener('keyup', e => instance.exports.onKeyUp(e.keyCode, 0));
// document.addEventListener('mousedown', e => instance.exports.onMouseDown(e.button, e.x, e.y));
// document.addEventListener('mouseup', e => instance.exports.onMouseUp(e.button, e.x, e.y));
document.addEventListener('mousemove', e => instance.exports.onMouseMove(e.x, e.y));
function step(timestamp) {
onAnimationFrame(timestamp);
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
});
function fetchAndInstantiate(url, importObject) {
return fetch(url)
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, importObject))
.then(results => results.instance);
}
</script>
</body>
</html>