Pixel-Level Canvas Buffer Engine.
Directly writing 32-bit RGBA integer arrays to an HTML5 Canvas ImageData buffer for algorithmic cellular patterns.
View code
<canvas aria-label="Animation demo"></canvas>:root { color-scheme: dark; font: 15px system-ui, sans-serif; color: #dbff55; }
* { box-sizing: border-box; }
body { margin: 0; height: 100dvh; display: grid; place-items: center; overflow: hidden; background: transparent; }
.tile { display: grid; place-items: center; width: 68px; aspect-ratio: 1; border-radius: 18px; background: currentColor; }
.tile { background: #dbff55; color: #202415; font-size: 30px; }
canvas { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; }
button { font: inherit; color: #f3f1e9; background: #191919; border: 1px solid #ffffff66; padding: 8px 14px; border-radius: 24px; cursor: pointer; }
:focus-visible { outline: 2px solid #dbff55; outline-offset: -3px; }
.notice { max-width: 34ch; padding: 16px; color: #fff; }
@media (prefers-reduced-motion: reduce) { .sprite { animation-play-state: paused; } }
canvas { image-rendering: pixelated; width: 100%; height: 100%; }// The gallery pauses this loop when the preview leaves the viewport.
let active = !matchMedia('(prefers-reduced-motion: reduce)').matches;
if (new URLSearchParams(location.search).has('embed')) active = false;
addEventListener('message', event => {
if (event.source === parent && event.origin === location.origin && event.data?.type === 'motion-play') {
active = event.data.active;
}
});
function loop(draw) {
let time = 0;
let previous;
function frame(now) {
const dt = previous === undefined ? 0 : Math.min((now - previous) / 1000, .05);
previous = now;
if (active && !document.hidden) {
time += dt;
draw(time, dt);
}
requestAnimationFrame(frame);
}
draw(0, 0);
requestAnimationFrame(frame);
}
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 144;
canvas.height = 90;
const image = ctx.createImageData(canvas.width, canvas.height);
loop(time => {
for (let y=0; y<image.height; y++) {
for (let x=0; x<image.width; x++) {
const offset = 4*(y*image.width+x);
const wave = .5+.5*Math.sin(Math.hypot(x-72,y-45)*.32-time*3
+ 2*Math.sin(x*.08+time) + Math.cos(y*.17-time));
image.data[offset] = 22 + 201 * wave;
image.data[offset+1] = 36 + 180 * wave;
image.data[offset+2] = 60 + 84 * (1-wave);
image.data[offset+3] = 255;
}
}
ctx.putImageData(image,0,0);
});