-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
138 lines (122 loc) · 3.22 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Zig WASM Canvas demo</title>
<style>
html,
body {
margin: 0;
padding: 0;
background-color: grey;
}
canvas {
width: 400px;
background-color: white;
}
</style>
<script type="module">
import indexWasmUrl from "./index.wasm?url"
const canvas = document.body.querySelector("canvas")
const ctx = canvas.getContext("2d")
const module = await WebAssembly.compileStreaming(fetch(indexWasmUrl))
const decodeString = (pointer, length) => {
const slice = new Uint8Array(
memory.buffer,
pointer,
length
)
return new TextDecoder().decode(slice)
}
const encodeString = (string) => {
const buffer = new TextEncoder().encode(string)
const pointer = allocUint8(buffer.length + 1)
const slice = new Uint8Array(
memory.buffer,
pointer,
buffer.length + 1
)
slice.set(buffer)
slice[buffer.length] = 0
return pointer
}
const {
exports: { memory, _draw, allocUint8, free },
} = await WebAssembly.instantiate(module, {
app: {
_allocPromptName(pointer, length) {
const message = decodeString(pointer, length)
const name = window.prompt(message) ?? "Human"
return encodeString(name)
},
},
env: {
_throwError(pointer, length) {
const message = decodeString(pointer, length)
throw new Error(message)
},
_consoleLog(pointer, length) {
const message = decodeString(pointer, length)
console.log(message)
},
},
ctx: {
save: ctx.save.bind(ctx),
restore: ctx.restore.bind(ctx),
translate(x, y) {
ctx.translate(x, y)
},
scale(x, y) {
ctx.scale(x, y)
},
rotate(angle) {
ctx.rotate(angle)
},
beginPath() {
ctx.beginPath()
},
moveTo(x, y) {
ctx.moveTo(x, y)
},
lineTo(x, y) {
ctx.lineTo(x, y)
},
_font(pointer, length) {
ctx.font = decodeString(pointer, length)
},
_fillStyle(pointer, length) {
ctx.fillStyle = decodeString(pointer, length)
},
_fillText(pointer, length, x, y) {
const text = decodeString(pointer, length)
ctx.fillText(text, x, y)
},
fill() {
ctx.fill()
},
lineWidth(width) {
ctx.lineWidth = width
},
_lineCap(pointer, length) {
ctx.lineCap = decodeString(pointer, length)
},
_strokeStyle(pointer, length) {
ctx.strokeStyle = decodeString(pointer, length)
},
_strokeText(pointer, length, x, y) {
const text = decodeString(pointer, length)
ctx.strokeText(text, x, y)
},
stroke() {
ctx.stroke()
},
},
})
_draw();
</script>
</head>
<body>
<canvas width="800" height="800">Canvas required. Please use Chrome, Firefox, or Safari</canvas>
</body>
</html>