-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
103 lines (94 loc) · 2.81 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="dark light" />
<title>zig-cube</title>
</head>
<body>
<h1>zig-cube thing</h1>
<label>
X
<input id="angle-x" step="0.02" min="0" max="6.3" type="range" />
</label>
<label>
Y
<input id="angle-y" step="0.02" min="0" max="6.3" type="range" />
</label>
<label>
Z
<input id="angle-z" step="0.02" min="0" max="6.3" type="range" />
</label>
<br />
<label>
Camera Distance
<input id="cam-dist" step="0.13" min="-10" max="10" type="range" />
</label>
<canvas style="display: block" id="canvas"></canvas>
<script>
/** @type {HTMLCanvasElement} */
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let w = null;
const SCALE = 6;
WebAssembly.instantiateStreaming(fetch("./zig-out/bin/zig-cube.wasm"), {
env: {
log: console.log,
},
}).then((wasmSrc) => {
w = wasmSrc;
main();
});
function main() {
const {
get_width,
get_height,
get_angle_x,
set_angle_x,
get_angle_y,
set_angle_y,
get_angle_z,
set_angle_z,
get_camera_distance,
set_camera_distance,
get_pixels_buf,
render,
memory,
} = w.instance.exports;
canvas.width = get_width() * SCALE;
canvas.height = get_height() * SCALE;
ctx.scale(SCALE, SCALE);
ctx.imageSmoothingEnabled = false;
const pixelsBuf = get_pixels_buf();
const angleXRange = document.getElementById("angle-x");
angleXRange.value = get_angle_x();
const angleYRange = document.getElementById("angle-y");
angleYRange.value = get_angle_y();
const angleZRange = document.getElementById("angle-z");
angleZRange.value = get_angle_z();
const camDistRange = document.getElementById("cam-dist");
camDistRange.value = get_camera_distance();
async function update() {
set_angle_x(angleXRange.value);
set_angle_y(angleYRange.value);
set_angle_z(angleZRange.value);
set_camera_distance(camDistRange.value);
render();
const image = new ImageData(
new Uint8ClampedArray(
memory.buffer,
pixelsBuf,
get_width() * get_height() * 4
),
get_width()
);
const bitmap = await createImageBitmap(image);
ctx.drawImage(bitmap, 0, 0);
requestAnimationFrame(update);
}
update();
}
</script>
</body>
</html>