xxxxxxxxxx
54
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Video: https://youtu.be/l__fEY1xanY
const [w, h] = [200, 200];
const d = 2;
const colorMax = 500;
const stepsPerFrame = 10;
let x, y, step;
let arr;
function setup() {
createCanvas(w * d, h * d);
arr = new Array(w * h).fill(false);
x = floor(w / 2);
y = floor(h / 2);
step = 0;
colorMode(HSB, colorMax, 1, 1);
noStroke();
frameRate(10000);
}
function draw() {
for (let iii = 0; iii < stepsPerFrame; iii += 1) {
if (step >= w * h) {
return;
}
while (true) {
let [rx, ry] = [floor(random(2)), floor(random(2))];
[rx, ry] = [rx + ry - 1, rx - ry];
let maxSteps = floor(random(w + h));
for (let i = 0; i < maxSteps; i += 1) {
x = (x + rx + w) % w;
y = (y + ry + w) % w;
if (!arr[x + y * w]) {
break;
}
}
if (!arr[x + y * w]) {
break;
}
}
arr[x + y * w] = true;
step += 1;
fill(step % colorMax, 1, 1);
square(x * d, y * d, d);
}
}