-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
300 lines (242 loc) · 7.31 KB
/
Copy pathindex.js
File metadata and controls
300 lines (242 loc) · 7.31 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
// Import the WebAssembly memory at the top of the file.
import { memory } from "convida/convida_bg";
import { Universe, Cell } from "convida";
const CELL_SIZE = 10; // px
const GRID_COLOR = "#000000";
const DEAD_COLOR = "#000000";
const ALIVE_COLOR = "#FFFFFF";
//Construct the universe, and get its width and height.
const universe = Universe.new();
const width = universe.width();
const height = universe.height();
// Give the canvas room for all of our cells and a 1px border
// around each of them.
const canvas = document.getElementById("game-of-life-canvas");
canvas.height = (CELL_SIZE + 1) * height + 1;
canvas.width = (CELL_SIZE + 1) * width + 1;
const pops = ["#DB37C4","#ED68D9","#49CCD4","#678CFA","#4635F7"];
let theme = 'random';
const delay = document.getElementById('delay');
let DELAY = delay.value || 100;
delay.addEventListener('change', () => {
DELAY = delay.value;
});
const ticks = document.getElementById('ticks');
let TICKS_PER_RENDER = ticks.value || 1;
ticks.addEventListener('change', () => {
TICKS_PER_RENDER = ticks.value;
});
const ctx = canvas.getContext("2d");
const themeSelect = document.getElementById('theme');
console.log(themeSelect.value);
themeSelect.addEventListener('change', event => {
console.log(themeSelect.value);
theme = themeSelect.value;
drawGrid();
drawCells();
});
if (ctx === null) {
alert("unable to initialize WebGL.");
}
let animationId = null;
const playPauseButton = document.getElementById("play-pause");
const pre = document.getElementById("pre");
const play = () => {
playPauseButton.textContent = "⏸";
renderLoop();
run = true;
}
const pause = () => {
playPauseButton.textContent = "▶";
cancelAnimationFrame(animationId);
animationId = null;
run = false;
}
let run = true
playPauseButton.addEventListener("click", event => {
if (run == false) {
play();
} else {
pause();
}
});
const resetButton = document.getElementById("reset");
resetButton.addEventListener("click", event => {
universe.reset();
drawGrid();
drawCells();
})
const clearButton = document.getElementById("clear");
clearButton.addEventListener("click", event => {
universe.clear();
drawGrid();
drawCells();
})
const step = document.getElementById("step");
step.addEventListener("click", event => {
universe.tick();
drawGrid();
drawCells();
animationId = requestAnimationFrame(mynull);
})
const mynull = () => {
return 0;
};
canvas.addEventListener("click", event => {
let index = idx(canvas);
let row = index.row;
let col = index.col;
if (event.ctrlKey) {
console.log("ctrl");
universe.glider(row, col);
}
else if (event.shiftKey) {
console.log("shift");
universe.pulsar(row, col);
}
else {
universe.toggle_cell(row, col);
}
drawGrid();
drawCells();
});
function idx(canvas) {
const boundingRect = canvas.getBoundingClientRect();
const scaleX = canvas.width / boundingRect.width;
const scaleY = canvas.height / boundingRect.height;
const canvasLeft = (event.clientX - boundingRect.left) * scaleX;
const canvasTop = (event.clientY - boundingRect.top) * scaleY;
const row = Math.min(Math.floor(canvasTop / (CELL_SIZE + 1)), height - 1);
const col = Math.min(Math.floor(canvasLeft / (CELL_SIZE + 1)), width - 1);
return {
row: row,
col: col
};
}
// The result of 'requestAnimationFrame' is assigned to 'animationId'.
const renderLoop = () => {
fps.render();
for (let i = 0; i < TICKS_PER_RENDER; i++) {
universe.tick();
}
drawGrid();
drawCells();
animationId = requestAnimationFrame(wait);
};
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const wait = async () => {
//animationId = null;
if (run == true) {
setTimeout(renderLoop, DELAY);
}
else {
while(run == false) {
await sleep(100);
}
renderLoop();
}
}
const drawGrid = () => {
ctx.beginPath();
ctx.strokeStyle = GRID_COLOR;
// Vertical lines.
for (let i = 0; i <= width; i++) {
ctx.moveTo(i * (CELL_SIZE + 1) + 1, 0);
ctx.lineTo(i * (CELL_SIZE + 1) + 1, (CELL_SIZE + 1) * height + 1);
}
for (let j = 0; j <= height; j++) {
ctx.moveTo(0, j * (CELL_SIZE + 1) + 1);
ctx.lineTo((CELL_SIZE + 1) * width + 1, j * (CELL_SIZE + 1) + 1);
}
ctx.stroke();
};
const getIndex = (row, column) => {
return row * width + column;
};
const drawCells = () => {
const cellsPtr = universe.cells();
const cells = new Uint8Array(memory.buffer, cellsPtr, width * height);
ctx.beginPath();
ctx.fillStyle = ALIVE_COLOR;
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const idx = getIndex(row, col);
if (cells[idx] !== Cell.Alive) {
continue;
}
if (theme == 'random') {
// https://stackoverflow.com/questions/1152024/best-way-to-generate-a-random-color-in-javascript/1152508
ctx.fillStyle = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
}
else if (theme == 'retro') {
ctx.fillStyle = pops[Math.floor(Math.random() * pops.length)];
}
ctx.fillRect(
col * (CELL_SIZE + 1) + 1,
row * (CELL_SIZE + 1) + 1,
CELL_SIZE,
CELL_SIZE
);
}
}
ctx.fillStyle = DEAD_COLOR;
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const idx = getIndex(row, col);
if (cells[idx] !== Cell.Dead) {
continue;
}
ctx.fillRect(
col * (CELL_SIZE + 1) + 1,
row * (CELL_SIZE + 1) + 1,
CELL_SIZE,
CELL_SIZE
);
}
}
ctx.stroke();
};
const fps = new class {
constructor() {
this.fps = document.getElementById("fps");
this.frames = [];
this.lastFrameTimeStamp = performance.now();
}
render() {
// Convert the delta time since the last frame render into a measure
// of frames per second.
const now = performance.now();
const delta = now - this.lastFrameTimeStamp;
this.lastFrameTimeStamp = now;
const fps = 1 / delta * 1000;
// Save only the latest 100 timings.
this.frames.push(fps);
if (this.frames.length > 100) {
this.frames.shift();
}
// Find the max, min, and mean of our last 100 latest timings.
let min = Infinity;
let max = -Infinity;
let sum = 0;
for (let i = 0; i < this.frames.length; i++) {
sum += this.frames[i];
min = Math.min(this.frames[i], min);
max = Math.max(this.frames[i], max);
}
let mean = sum / this.frames.length;
// Render the statistics.
this.fps.textContent = `
Frames per Second:
latest = ${Math.round(fps)}
avg of last 100 = ${Math.round(mean)}
min of last 100 = ${Math.round(min)}
max of last 100 = ${Math.round(max)}
`.trim();
}
}
drawGrid();
drawCells();
//requestAnimationFrame(renderLoop);
play();