-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
361 lines (335 loc) · 11.5 KB
/
Copy pathscript.js
File metadata and controls
361 lines (335 loc) · 11.5 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let gridSize = 100;
let cellSize = canvas.width / gridSize;
let xOffset = 0;
let yOffset = 0;
let frameRate = 10;
let pause = false;
let theme = "dark";
let generation = 0;
let grid = [];
function randomiseGrid(seed) {
// create a random grid of 1s and 0s of size gridSize x gridSize
// We essentially use a linear congruential generator (LCG) to generate a random number
// See a simpler example on : https://www.github.com/mayankkamboj47/numtheory
if (seed === undefined) seed = Math.floor(Math.random() * (1 << 48));
const multiplier = 0x5DEECE66D;
const mask = (1 << 48) - 1;
let random = seed;
for (let i = 0; i < gridSize; i++) {
grid[i] = [];
for (let j = 0; j < gridSize; j++) {
random = (multiplier * random + 0xB) & mask;
grid[i][j] = Math.floor(random / (1 << (48 - 1))) % 2;
}
}
}
function gameLoop() {
generation++;
updateGrid();
drawGrid();
if (!pause) setTimeout(gameLoop, 1000 / frameRate);
}
function updateGrid() {
let newGrid = [];
for (let i = 0; i < gridSize; i++) {
newGrid[i] = [];
for (let j = 0; j < gridSize; j++) {
try {
const cell = grid[i][j];
const neighbours = countNeighbours(i, j);
if (cell === 0 && neighbours === 3) {
newGrid[i][j] = 1;
} else if (cell === 1 && (neighbours < 2 || neighbours > 3)) {
newGrid[i][j] = 0;
} else {
newGrid[i][j] = cell;
}
} catch (e) {
if (e instanceof TypeError) newGrid[i][j] = 0;
else throw e;
}
}
}
grid = newGrid;
}
function resizeGrid(size) {
gridSize = size;
cellSize = canvas.width / gridSize;
updateGrid();
drawGrid();
}
canvas.addEventListener("click", function (e) {
const rect = canvas.getBoundingClientRect();
const coefficient = canvas.width / rect.width;
const x = Math.floor((e.clientX - rect.left) * coefficient / cellSize);
const y = Math.floor((e.clientY - rect.top) * coefficient / cellSize);
grid[x + xOffset][y + yOffset] = (grid[x + xOffset] || [])[y + yOffset] === 1 ? 0 : 1;
drawGrid();
});
canvas.addEventListener("mousedown", function (e) {
const onMove = (e) => {
const rect = canvas.getBoundingClientRect();
const coefficient = canvas.width / rect.width;
const x = Math.floor((e.clientX - rect.left) * coefficient / cellSize) + xOffset;
const y = Math.floor((e.clientY - rect.top) * coefficient / cellSize) + yOffset;
grid[x][y] = 1;
drawGrid();
};
canvas.addEventListener("mousemove", onMove);
canvas.addEventListener("mouseup", function () {
canvas.removeEventListener("mousemove", onMove);
});
});
function countNeighbours(x, y) {
let neighbours = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
const x1 = x + i;
const y1 = y + j;
if (x1 < 0 || x1 >= gridSize || y1 < 0 || y1 >= gridSize) continue;
if (grid[x1][y1] === 1) neighbours++;
}
}
return neighbours;
}
function drawGrid() {
if (theme === "light") ctx.fillStyle = "white";
else ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
const cell = grid[i + xOffset] ? grid[i + xOffset][j + yOffset] : undefined;
if (cell === 1) {
if (theme === "light") ctx.fillStyle = "black";
else ctx.fillStyle = "white";
ctx.fillRect(i * cellSize, j * cellSize, cellSize, cellSize);
}
}
}
}
function clearGrid() {
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
try {
grid[i][j] = 0;
}
catch {
console.log(grid, i, j);
}
}
}
generation = 0;
}
// Start the game
randomiseGrid(); // pick a random grid
gameLoop(); // and begin the simulation
function elt(name, attributes, children) {
let node = document.createElement(name);
if (attributes) {
for (let attr in attributes)
if (attributes.hasOwnProperty(attr))
node.setAttribute(attr, attributes[attr]);
}
for (let i = 2; i < arguments.length; i++) {
let child = arguments[i];
if (typeof child == "string")
child = document.createTextNode(child);
node.appendChild(child);
}
return node;
}
(
function () {
const label = elt("label", null, elt("span", null, "Grid Size"));
const slider = elt("input", { type: "range", min: 100, max: 500, value: 100, step: 1 });
label.appendChild(slider);
document.body.appendChild(label);
slider.addEventListener("change", function (e) {
resizeGrid(e.target.value);
});
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "X offset"));
const slider = elt("input", { type: "range", min: 0, max: 100, value: 0, step: 1 });
label.appendChild(slider);
document.body.appendChild(label);
slider.addEventListener("change", function (e) {
xOffset = parseInt(e.target.value);
});
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Y offset"));
const slider = elt("input", { type: "range", min: 0, max: 100, value: 0, step: 1 });
label.appendChild(slider);
document.body.appendChild(label);
slider.addEventListener("change", function (e) {
yOffset = parseInt(e.target.value);
});
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Frame Rate"));
const slider = elt("input", { type: "range", min: 1, max: 60, value: 10, step: 1 });
label.appendChild(slider);
document.body.appendChild(label);
slider.addEventListener("change", function (e) {
frameRate = e.target.value;
});
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Pause"));
const checkbox = elt("input", { type: "checkbox" });
label.appendChild(checkbox);
document.body.appendChild(label);
checkbox.addEventListener("change", function (e) {
if (!pause) pause = true;
else {
pause = false;
gameLoop();
}
});
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Clear"));
const button = elt("button", null, "Clear");
label.appendChild(button);
document.body.appendChild(label);
button.addEventListener("click", function (e) {
clearGrid();
drawGrid();
});
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Randomise"));
const button = elt("button", null, "Random");
const seed = elt("input", { type: "text", value: "1331", placeholder: "seed" });
label.appendChild(seed);
label.appendChild(button);
document.body.appendChild(label);
button.addEventListener("click", function (e) {
randomiseGrid(seed.value);
seed.value = Math.floor(Math.random() * 10000);
drawGrid();
}
);
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Theme"));
const button = elt("button", null, "Toggle");
label.appendChild(button);
document.body.appendChild(label);
button.addEventListener("click", function (e) {
if (theme === "light") theme = "dark";
else theme = "light";
drawGrid();
document.querySelector("body").classList.toggle("dark");
});
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Download the grid"));
const button = elt("button", null, "Download");
label.appendChild(button);
document.body.appendChild(label);
button.addEventListener("click", function (e) {
// download the grid as a json file
const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(grid));
const dlAnchorElem = elt('a', { href: dataStr, download: 'grid.json' });
document.body.appendChild(dlAnchorElem);
dlAnchorElem.click();
dlAnchorElem.remove();
});
}
)();
(
function () {
const label = elt("label", { class: "upload" }, elt("span", null, "Upload a grid"));
const input = elt("input", { type: "file" });
label.appendChild(input);
document.body.appendChild(label);
input.addEventListener("change", function (e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const contents = e.target.result;
grid = JSON.parse(contents);
gridSize = grid.length;
cellSize = canvas.width / gridSize;
drawGrid();
};
reader.readAsText(file);
}
);
}
)();
(
function () {
const label = elt("label", null, elt("span", null, "Presets"));
const select = elt("select", null);
const options = [
{ name: "Glider", value: "glider" },
{ name: "Glider Gun", value: "gliderGun" },
{ name: "Beacon", value: "beacon" },
];
const presetGrids = {
glider: [
"...#.",
".#.#.",
"..##.",
].map(line => [...line].map(char => char === "#" ? 1 : 0)),
beacon: [
[1, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 1, 1]
],
gliderGun : [
"........................#...........",
"......................#.#...........",
"............##......##............##",
"...........#...#....##............##",
"##........#.....#...##..............",
"##........#...#.##....#.#...........",
"..........#.....#.......#...........",
"...........#...#....................",
"............##......................",
].map(line => [...line].map(char => char === "#" ? 1 : 0)),
};
options.forEach(function (option) {
const opt = elt("option", { value: option.value }, option.name);
select.appendChild(opt);
}
);
label.appendChild(select);
document.body.appendChild(label);
select.addEventListener("change", function (e) {
const preset = e.target.value;
newGrid = presetGrids[preset].slice();
clearGrid();
// put the preset in the middle of the grid
const offset = Math.floor((gridSize - newGrid.length) / 4);
for (let i = 0; i < newGrid.length; i++) {
for (let j = 0; j < newGrid[0].length; j++) {
grid[i + offset][j + offset] = newGrid[i][j];
}
}
drawGrid();
});
})();