-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdemo.js
202 lines (170 loc) · 5.73 KB
/
demo.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
let Module = {};
// copying strings
let utfDecoder = new TextDecoder('utf-8');
let getStr = function (module, ptr, len) {
let slice = new Uint8Array(module.memory.buffer, ptr, len);
return utfDecoder.decode(slice);
};
let utfEncoder = new TextEncoder('utf-8');
let putStr = function (module, str) {
let buf = utfEncoder.encode(str);
let ptr = module.alloc(buf.length);
let slice = new Uint8Array(module.memory.buffer, ptr, buf.length);
slice.set(buf);
return {ptr: ptr, len: buf.length};
};
// /copying strings
let io = {
puts: (ptr, len) => console.log(getStr(Module, ptr, len)),
alert: (n) => alert(n),
};
let time = {
performance_time_origin: performance.timeOrigin,
performance_now: () => performance.now(),
};
let eventLoop = function(Module) {
const EVENT_DESTROYED = 0;
const EVENT_ANIMATION_FRAME = 1;
const EVENT_MOUSE_MOVE = 2;
const EVENT_KEY_DOWN = 3;
const EVENT_KEY_UP = 4;
let eventLoopsDict = new Map();
eventLoopsDict.counter = 0;
let keyEventFlags = function(event) {
return (event.shiftKey ? 1 : 0) | (event.ctrlKey ? 2 : 0) | (event.altKey ? 4 : 0);
};
let charKey = function(event) {
if (event.key.length !== 1) { return 0xffffffff; }
return event.key.charCodeAt(0);
};
let EventLoop = class {
constructor(callback) {
let self = this;
let id = ++eventLoopsDict.counter;
eventLoopsDict.set(id, self);
self.id = id;
self.dead = false;
self.rafId = null;
self.rafCb = function() {
self.rafId = null;
if (self.dead) { return; }
callback(id, EVENT_ANIMATION_FRAME, 0, 0, 0);
};
self.mouseMoveCb = function(event) {
if (self.dead) { return; }
callback(id, EVENT_MOUSE_MOVE, event.pageX, event.pageY, 0)
};
self.keyDown = function(event) {
if (self.dead) { return; }
callback(id, EVENT_KEY_DOWN, event.which, charKey(event), keyEventFlags(event));
};
self.keyUp = function(event) {
if (self.dead) { return; }
callback(id, EVENT_KEY_UP, event.which, charKey(event), keyEventFlags(event));
};
self.subscribeMouse();
self.subscribeKeyboard();
}
raf() {
let self = this;
if (self.dead) { return; }
self.rafId = requestAnimationFrame(self.rafCb);
}
caf() {
let self = this;
if (self.rafId) { cancelAnimationFrame(self.rafId); }
}
subscribeMouse() {
let self = this;
if (self.dead) { return; }
window.addEventListener('mousemove', self.mouseMoveCb);
}
unsubscribeMouse() {
let self = this;
window.removeEventListener('mousemove', self.mouseMoveCb);
}
subscribeKeyboard() {
let self = this;
if (self.dead) { return; }
window.addEventListener('keydown', self.keyDown);
window.addEventListener('keyup', self.keyUp);
}
unsubscribeKeyboard() {
let self = this;
window.removeEventListener('keydown', self.keyDown);
window.removeEventListener('keyup', self.keyUp);
}
shutdown() {
let self = this;
if (self.dead) { return; }
self.dead = true;
self.caf();
self.unsubscribeMouse();
self.unsubscribeKeyboard();
eventLoopsDict.delete(self.id);
}
};
let raf = function(id) {
if (!eventLoopsDict.has(id)) { return false; }
eventLoopsDict.get(id).raf();
return true;
};
let destroy = function(id) {
if (!eventLoopsDict.has(id)) { return false; }
eventLoopsDict.get(id).destroy();
setInterval(() => Module.event_loop_cb(id, EVENT_DESTROYED, 0, 0, 0), 0);
return true;
};
return {
event_loop_new: () => new EventLoop(Module.event_loop_cb).id,
event_loop_raf: raf,
event_loop_shutdown: destroy,
};
};
let svg = {
svg_set_path: (ptr, len) => window.path.setAttributeNS(null, 'd', getStr(Module, ptr, len)),
};
let math = {
sqrt: (x) => Math.sqrt(x),
sin: (x) => Math.sin(x),
cos: (x) => Math.cos(x),
};
let randSource = (module) => (ptr, len) => {
const OK = 0;
const RANGE_ERROR = 1;
const QUOTA_ERROR = 2;
try {
let slice = new Uint8Array(module.memory.buffer, ptr, len);
window.crypto.getRandomValues(slice);
} catch (e) {
if (e instanceof RangeError) {
return RANGE_ERROR;
} else if (e instanceof DOMException && x.code === DOMException.QUOTA_EXCEEDED_ERR) {
return QUOTA_ERROR;
} else {
throw e;
}
}
return OK;
}
let rand = {
js_fill_rand: randSource(Module),
};
let imports = {
env: Object.assign({}, time, eventLoop(Module), io, svg, math, rand),
};
fetch('/target/wasm32-unknown-unknown/release/svg_asteroids.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports))
.then(results => {
let instance = results.instance;
let exports = instance.exports;
Object.assign(Module, {
alloc: exports.alloc,
dealloc: exports.dealloc,
memory: exports.memory,
event_loop_cb: exports.event_loop_cb,
});
exports.my_main();
});