Created
April 12, 2022 08:36
-
-
Save akovantsev/3662891baa9b273363e129bcec9c8557 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<canvas id="field" width="800" height="600"></canvas> | |
<script type="application/javascript"> | |
const W = 800; | |
const H = 600; | |
const X = 100000; | |
const T = 200; | |
const W1 = W; | |
const W2 = W*2; | |
const canvas = document.getElementById("field") | |
const ctx = canvas.getContext('2d'); | |
let IDS = []; | |
let zeroes = function () { return new Array(W*H).fill(0); } | |
let idx = function (h, w) { return h*W + w; } | |
let next = function (NOW, NEXT, IDX, TEST, IMG) { | |
const sum = (NOW[IDX-1-W]+NOW[IDX-W]+NOW[IDX+1-W] | |
+NOW[IDX-1 ] +NOW[IDX+1 ] | |
+NOW[IDX-1+W]+NOW[IDX+W]+NOW[IDX+1+W]); | |
if (NOW[IDX]) { | |
if (sum === 2 || sum === 3) { | |
NEXT[IDX] = 1; | |
IMG.data[IDX*4 + 0] = 255; // R value | |
// IMG.data[IDX*4 + 1] = 0; // G value | |
// IMG.data[IDX*4 + 2] = 0; // B value | |
IMG.data[IDX*4 + 3] = 255; // A value | |
} else { | |
TEST.add(IDX-2-W2);TEST.add(IDX-1-W2);TEST.add(IDX-W2);TEST.add(IDX+1-W2);TEST.add(IDX+2-W2); | |
TEST.add(IDX-2-W1);TEST.add(IDX-1-W1);TEST.add(IDX-W1);TEST.add(IDX+1-W1);TEST.add(IDX+2-W1); | |
TEST.add(IDX-2 );TEST.add(IDX-1 );TEST.add(IDX );TEST.add(IDX+1 );TEST.add(IDX+2 ); | |
TEST.add(IDX-2+W1);TEST.add(IDX-1+W1);TEST.add(IDX+W1);TEST.add(IDX+1+W1);TEST.add(IDX+2+W1); | |
TEST.add(IDX-2+W2);TEST.add(IDX-1+W2);TEST.add(IDX+W2);TEST.add(IDX+1+W2);TEST.add(IDX+2+W2); | |
// NEXT[IDX] = 0; | |
} | |
} else { | |
if (sum === 3) { | |
NEXT[IDX] = 1; | |
IMG.data[IDX*4 + 0] = 255; // R value | |
// IMG.data[IDX*4 + 1] = 0; // G value | |
// IMG.data[IDX*4 + 2] = 0; // B value | |
IMG.data[IDX*4 + 3] = 255; // A value | |
TEST.add(IDX-2-W2);TEST.add(IDX-1-W2);TEST.add(IDX-W2);TEST.add(IDX+1-W2);TEST.add(IDX+2-W2); | |
TEST.add(IDX-2-W1);TEST.add(IDX-1-W1);TEST.add(IDX-W1);TEST.add(IDX+1-W1);TEST.add(IDX+2-W1); | |
TEST.add(IDX-2 );TEST.add(IDX-1 );TEST.add(IDX );TEST.add(IDX+1 );TEST.add(IDX+2 ); | |
TEST.add(IDX-2+W1);TEST.add(IDX-1+W1);TEST.add(IDX+W1);TEST.add(IDX+1+W1);TEST.add(IDX+2+W1); | |
TEST.add(IDX-2+W2);TEST.add(IDX-1+W2);TEST.add(IDX+W2);TEST.add(IDX+1+W2);TEST.add(IDX+2+W2); | |
} else { | |
// NEXT[IDX] = 0; | |
} | |
} | |
} | |
const NOW = zeroes(); | |
for (let x=0;x<X;x++) { | |
let IDX = Math.floor(Math.random() * H*W); | |
NOW[IDX] = 1; | |
} | |
// console.log (NOW); | |
let step = function (NOW, TEST) { | |
const NEXT = zeroes(); | |
const NEXTTEST = new Set(); | |
const IMG = ctx.createImageData(W, H); | |
// console.time("c"); | |
if (!TEST) { | |
for (let h = 2; h < H - 2; h++) { | |
for (let w = 2; w < W - 2; w++) { | |
next(NOW, NEXT, idx(h, w), NEXTTEST, IMG); | |
} | |
} | |
} else { | |
for (let i of TEST.values()) { | |
next(NOW, NEXT, i, NEXTTEST, IMG); | |
} | |
} | |
ctx.putImageData(IMG, 0, 0); | |
// console.timeEnd("c"); | |
//todo: https://developer.mozilla.org/en-US/docs/Web/API/ImageData | |
// step(NEXT, Array.from(NEXTTEST)); | |
setTimeout(function (){step(NEXT, NEXTTEST)}, 1); | |
// window.requestIdleCallback(function (){step(NEXT, Array.from(NEXTTEST))}); | |
} | |
step(NOW, undefined); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment