xxxxxxxxxx
const dead = [0, 0, 0];
const alive = [255, 255, 255];
const n = 500 ; // Resolution
let cells;
let g;
function setup() {
const m = min(windowWidth, windowHeight);
createCanvas(m, m);
cells = new Float32Array(n * n).fill().map((e) => random(-1, 1));
g = createGraphics(n, n);
}
function getValue(x, y) {
x = (x + n) % n;
y = (y + n) % n;
return cells[x + y * n];
}
function draw() {
g.background(dead).loadPixels();
const aliveData = new Uint8Array(alive);
const zoom = n / width;
const mx = int(constrain(mouseX * zoom, 0, n - 1));
const my = int(constrain(mouseY * zoom, 0, n - 1));
cells[mx + my * n] = sin(frameCount);
let next = new Float32Array(cells);
for (let y = 0; y < n; y++) {
for (let x = 0; x < n; x++) {
const self = getValue(x, y);
const neighbors =
getValue(x - 1, y - 1) +
getValue(x - 1, y + 1) +
getValue(x + 1, y - 1) +
getValue(x + 1, y + 1)
const neighbors1 =
getValue(x - 2, y - 2) +
getValue(x - 2, y + 2) +
getValue(x + 2, y - 2) +
getValue(x + 2, y + 2) ;
const newValue =
+ self * 0.25
+ (neighbors + neighbors1*0.25) * (mouseIsPressed ? -0.1 : 0.1);
const i = x + y * n;
next[i] = newValue;
newValue > 0 && g.pixels.set(aliveData, i * 4);
}
}
cells = next;
g.updatePixels();
image(g, 0, 0, width, height);
}