-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
342 lines (306 loc) · 10.3 KB
/
main.js
File metadata and controls
342 lines (306 loc) · 10.3 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"use strict";
(function () {
let canvas, canvas2;
let context, context2;
// inputBuffer starts with canvas image data. We write to outputBuffer,
// then copy that either to the canvas or to canvas2 for display, and into
// inputBuffer for the next iteration.
let inputBuffer;
let inputView;
let outputBuffer;
let outputBuffer2;
let outputView;
let outputView2;
let activeWidth;
let activeHeight;
const borderSize = 1; // Leave a 1-pixel sentinel border.
const originX = borderSize;
const originY = borderSize;
function init() {
canvas = document.getElementById('canvas');
canvas2 = document.getElementById('canvas2');
canvas2.width = canvas.width;
canvas2.height = canvas.height;
context = canvas.getContext('2d');
context2 = canvas2.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context2.clearRect(0, 0, canvas2.width, canvas2.height);
showTestToggled();
showDebugToggled();
showDebugControlsToggled();
getColorInfo();
canvas.addEventListener('click', onCanvasClicked);
canvas2.addEventListener('click', onCanvasClicked);
window.addEventListener('resize', resizeCanvasCSS);
initAnimation();
toggleRun();
}
window.init = init;
function setDimensions(w, h) {
canvas.width = w;
canvas.height = h;
canvas2.width = w;
canvas2.height = h;
activeWidth = canvas.width - 2 * borderSize;
activeHeight = canvas.height - 2 * borderSize;
resizeCanvasCSS();
}
function resizeCanvasCSS() {
function helper(container, canvas) {
let rect = container.getBoundingClientRect();
let boundingWidth = rect.width;
let boundingHeight = rect.height;
if (!(boundingWidth && boundingHeight && canvas.width && canvas.height)) {
return; // not yet initialized
}
let ratio = canvas.height / canvas.width;
if (boundingWidth * ratio > boundingHeight) {
// height is the limiting dimension
canvas.style.height = boundingHeight;
canvas.style.width = boundingHeight / ratio;
} else {
canvas.style.height = boundingWidth * ratio;
canvas.style.width = boundingWidth;
}
}
helper(document.getElementById('canvas-container'), canvas);
helper(document.getElementById('canvas2-container'), canvas2);
}
function initBuffers() {
inputBuffer = context.getImageData(0, 0, canvas.width, canvas.height);
outputBuffer = context.createImageData(inputBuffer)
outputBuffer2 = context.createImageData(inputBuffer)
inputView = new Uint32Array(inputBuffer.data.buffer);
outputView = new Uint32Array(outputBuffer.data.buffer);
outputView2 = new Uint32Array(outputBuffer2.data.buffer);
}
var curFunc;
let animation;
function initAnimation() {
// Add 2 for the sentinel borders, which the animation doesn't think
// about.
setDimensions(animation.width + 2, animation.height + 2);
initBuffers();
let c = new CanvasWrapper(outputBuffer);
c.fillRect(0, 0, 0, canvas.width, canvas.height);
animation.init(c, originX, originY, activeWidth, activeHeight,
OBVIOUS_COLORS);
context.clearRect(0, 0, canvas.width, canvas.height);
context.putImageData(outputBuffer, 0, 0);
inputView.set(outputView);
curFunc = animation.f;
}
function registerAnimation(name, width, height, init, f) {
animation = {
init: init,
f: f,
name: name,
width: width,
height: height
};
}
window.registerAnimation = registerAnimation;
function getAddr32(i, j) {
return i + canvas.width * j
}
function dumpImageData(view, x, y, w, h) {
for (let j = y; j < y + h; ++j) {
let addr = getAddr32(x, j);
var t = _.map(view.slice(addr, addr + w), i => i.toString(16)).join(', ');
console.log(t);
}
}
window.dumpImageData = dumpImageData;
function dumpBoard(x, y, w, h) {
console.log('board state:');
x = x || 0;
y = y || 0;
w = w || canvas.width;
h = h || canvas.height;
dumpImageData(inputView, x, y, w, h);
}
window.dumpBoard = dumpBoard;
// TODO: Look into array.subarray instead of the topData, midData, botData
// copies. Could be cleaner and faster; faster still if we passed them into
// f instead of the flattened array, assuming that was useful to f.
function runConv3x3Step(f, inputView, outputView) {
let inputRow = inputView.subarray(0, canvas.width);
let outputRow = outputView.subarray(0, canvas.width);
outputRow.set(inputRow);
let addr = getAddr32(0, canvas.height - 1);
inputRow = inputView.subarray(addr, addr + canvas.width);
outputRow = outputView.subarray(addr, addr + canvas.width);
outputRow.set(inputRow);
for (let j = 0; j < canvas.height; ++j) {
addr = getAddr32(0, j);
outputView[addr] = inputView[addr];
outputView[addr + canvas.width - 1] = inputView[addr + canvas.width - 1];
}
for (let j = originY; j < canvas.height - borderSize; ++j) {
let i = originX;
let topAddr = getAddr32(i - 1, j - 1);
let topData = [0]; // placeholder
topData.push(inputView[topAddr++])
topData.push(inputView[topAddr++])
let midAddr = getAddr32(i - 1, j);
let midData = [0]; // placeholder
midData.push(inputView[midAddr++])
midData.push(inputView[midAddr++])
let botAddr = getAddr32(i - 1, j + 1);
let botData = [0]; // placeholder
botData.push(inputView[botAddr++])
botData.push(inputView[botAddr++])
let outputAddr = getAddr32(i, j);
for (; i < canvas.width - borderSize; ++i, ++outputAddr) {
topData.shift();
topData.push(inputView[topAddr++])
midData.shift();
midData.push(inputView[midAddr++])
botData.shift();
botData.push(inputView[botAddr++])
let value = f(_.flatten([topData, midData, botData]), i, j)
outputView[outputAddr] = value;
}
}
}
function test() {
runConv3x3Step(curFunc, inputView, outputView2);
context2.putImageData(outputBuffer2, 0, 0);
}
window.test = test;
function step() {
runConv3x3Step(curFunc, inputView, outputView);
context.putImageData(outputBuffer, 0, 0,
originX, originY, activeWidth, activeHeight);
inputView.set(outputView);
}
window.step = step;
function showTestToggled() {
if (document.getElementById('toggle_test').checked) {
document.getElementById('canvas2').parentElement.style.display = 'inline';
} else {
document.getElementById('canvas2').parentElement.style.display = 'none';
}
resizeCanvasCSS();
}
window.showTestToggled = showTestToggled;
function showDebugToggled() {
if (document.getElementById('toggle_debug').checked) {
document.getElementById('debug').style.display = 'inline';
} else {
document.getElementById('debug').style.display = 'none';
}
resizeCanvasCSS();
}
window.showDebugToggled = showDebugToggled;
function showDebugControlsToggled() {
if (document.getElementById('toggle_debug_controls').checked) {
document.getElementById('debugging-controls').style.display = 'flex';
} else {
document.getElementById('debugging-controls').style.display = 'none';
}
resizeCanvasCSS();
}
window.showDebugControlsToggled = showDebugControlsToggled;
let goFast = false;
function fastToggled() {
goFast = document.getElementById('toggle_fast').checked;
}
window.fastToggled = fastToggled;
let OBVIOUS_COLORS;
function getColorInfo() {
OBVIOUS_COLORS = document.getElementById('toggle_obvious').checked;
}
function showObviousToggled() {
getColorInfo();
initAnimation();
}
window.showObviousToggled = showObviousToggled;
let frameReady = false;
function asyncStep() {
let doStep = () => {
runConv3x3Step(curFunc, inputView, outputView);
inputView.set(outputView);
frameReady = true;
++fpsFrames;
}
doStep();
let deadline = lastFrameStart + 0.9 * 1000 / 60; // 90% of frame in ms
if (running) {
while (goFast && (performance.now() < deadline)) {
doStep();
}
}
}
let lastFrameStart = 0;
function asyncAnimationFrame(timestamp) {
lastFrameStart = timestamp;
if (running) {
if (frameReady) {
frameReady = false;
context.putImageData(outputBuffer, 0, 0,
originX, originY, activeWidth, activeHeight);
updateFPS(timestamp);
}
window.setTimeout(asyncStep, 0);
requestAnimationFrame(asyncAnimationFrame);
} else {
resetFPS();
}
}
function onCanvasClicked(e) {
let xScale = canvas.clientWidth / canvas.width;
let yScale = canvas.clientHeight / canvas.height;
let x = Math.floor(e.offsetX / xScale);
let y = Math.floor(e.offsetY / yScale);
let addr = getAddr32(x, y);
let view;
if (e.currentTarget.id === 'canvas') {
view = inputView; // Not outputView, which may differ.
} else {
view = outputView2;
assert(e.currentTarget.id === 'canvas2');
}
let value = view[addr]
// Assumes the animation attaches ns to canvas on selection.
let s = `(${x},${y}):${value.toString(16)}\n` +
canvas.ns.getDescription(view[addr])
document.getElementById('debug').value = s;
}
let running = false;
/* On frame callback:
If you have a frame ready to show, copy it in, tell updateFPS about it, and
kick off the next compute in the background.
Update FPS.
Request the next frame.
On frame completion: mark the frame ready to show.
*/
function toggleRun() {
running = !running;
if (running) {
requestAnimationFrame(asyncAnimationFrame);
}
}
window.toggleRun = toggleRun;
var fpsFrames = 0;
var fpsStartTime = -1;
function resetFPS() {
fpsFrames = 0;
fpsStartTime = -1;
document.getElementById('fps').innerText = "N/A";
}
function updateFPS(timestamp) {
if (fpsStartTime < 0) {
fpsStartTime = timestamp;
} else {
var timeDiff = timestamp - fpsStartTime
// If it's been over a second and we've done something, update.
if (timeDiff >= 1000 && fpsFrames > 0) {
let fps = fpsFrames * 1000 / timeDiff
document.getElementById('fps').innerText = fps.toFixed(3)
fpsFrames = 0;
fpsStartTime = timestamp
}
}
}
})()