xxxxxxxxxx
51
// warning: flickers
var randomThreshold = 0.01;
var back;
function setup() {
randomSeed(56);
pixelDensity(1);
createCanvas(400, 400);
canvas.style.width = "100%";
canvas.style.height = "100%";
loadPixels();
for(var i = 0; i < pixels.length; i += 4) {
pixels[i] = 0;
pixels[i+1] = 0;
pixels[i+2] = 0;
pixels[i+3] = 255 * (random(100) > randomThreshold*100);
}
updatePixels();
}
function draw() {
loadPixels();
for(var x = 0; x < width; x++) {
for(var y = 0; y < height; y++) {
var neighbours = [[x-1, y], [x-1, y-1], [x, y-1], [x+1, y-1], [x+1, y], [x+1, y+1], [x, y+1], [x-1, y+1]];
var num = 0;
for(var [x_, y_] of neighbours) {
num += 1 * (pixels[index(x_, y_)] > 0);
}
var ind = index(x, y);
if(num < 2) pixels[ind] = 0;
if(num == 3) pixels[ind] = 255;
if(num > 3) pixels[ind] = 0;
}
}
updatePixels();
}
function index(x, y) {
if(x < 0) x = width - 1;
if(x > width-1) x = 0;
if(y < 0) y = height - 1;
if(y > height-1) y = 0;
return (y * width + x) * 4 + 3;
}