xxxxxxxxxx
const n = 200;
const brushRadius = 15;
const color0 = new Uint8Array([0, 0, 0, 255]);
const color1 = new Uint8Array([255, 255, 255, 255]);
let grid = new Uint8Array(n * n);
let zoom;
const mat = [-0.04453715622331162, -0.00887788047313287, 0.04557047718845533]
function setup() {
const m = min(windowWidth, windowHeight);
createCanvas(m, m);
background(0);
zoom = n / m;
pixelDensity(zoom);
background(255);
loadPixels();
grid = grid.map((v) => (v = random(256)));
}
function draw() {
const newGrid = new Uint8Array(grid);
for (let y = 1; y < n - 1; y++) {
for (let x = 1; x < n - 1; x++) {
const i = x + y * n;
const oldValue = grid[i];
const newValue =
+ grid[i-n] * mat[0]
+(grid[i-1] +grid[i+1] +grid[i+n] ) * mat[1]
+(grid[i-1-n]+grid[i+1-n] +grid[i-1+n] +grid[i+1+n] ) * mat[2];
newGrid[i] = newValue;
const col = newValue > oldValue? color1 : color0;
pixels.set(col, i * 4);
}
}
grid = newGrid;
updatePixels();
}
function mouseDragged() {
for (let dy = -brushRadius; dy <= brushRadius; dy++) {
for (let dx = -brushRadius; dx <= brushRadius; dx++) {
if (dx ** 2 + dy ** 2 > brushRadius ** 2) continue;
const x = floor(mouseX * zoom + dx);
const y = floor(mouseY * zoom + dy);
if (0 <= x && x < n && 0 <= y && y < n) grid[x + y * n] = random(256);
}
}
}