Published
Edited
Dec 23, 2020
15 forks
Importers
39 stars
Insert cell
Insert cell
Insert cell
canvas = {
replay;

const w = width + 28, h = height;
const context = DOM.context2d(w, h, 1);
const image = context.getImageData(0, 0, w, h);
const indexes = [];
let frame = 0;

context.canvas.style.margin = "0 -14px";
context.canvas.style.imageRendering = "pixelated";
visit(w >> 1, h >> 1, 128, 128, 128);

function visit(x, y, r, g, b) {
if (x < 0 || x >= w || y < 0 || y >= h) return;
const i = y * w + x, j = i << 2;
if (image.data[j + 3] === 0) {
image.data[j + 0] = r + (Math.random() < 0.5 ? -1 : 1);
image.data[j + 1] = g + (Math.random() < 0.5 ? -1 : 1);
image.data[j + 2] = b + (Math.random() < 0.5 ? -1 : 1);
image.data[j + 3] = 255;
indexes.push(i);
}
}
while (indexes.length) {
const i = Math.random() * indexes.length | 0;
const j = indexes[i];
const k = indexes.pop();
if (i < indexes.length) indexes[i] = k;
const r = image.data[(j << 2) + 0];
const g = image.data[(j << 2) + 1];
const b = image.data[(j << 2) + 2];
const x = j % w, y = j / w | 0;
visit(x - 1, y, r, g, b);
visit(x + 1, y, r, g, b);
visit(x, y - 1, r, g, b);
visit(x, y + 1, r, g, b);
if (!(++frame % 1000)) {
context.putImageData(image, 0, 0);
yield context.canvas;
}
}

context.putImageData(image, 0, 0);
yield context.canvas;
}
Insert cell
height = 600
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more