Kinetic Physics & Variable Typography Matrix.
Continuous variable font optical width and weight morphing driven by Euler spring-mass physics and cursor velocity vectors.
View code
<canvas tabindex="0" 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 { cursor:crosshair; }// 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');
let width = 1, height = 1;
function fit() {
width = canvas.clientWidth;
height = canvas.clientHeight;
const ratio = Math.min(devicePixelRatio || 1, 2);
canvas.width = Math.max(1, width * ratio);
canvas.height = Math.max(1, height * ratio);
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
}
fit();
addEventListener('resize', fit);
let x=width/2, y=height/2, targetX=x, targetY=y, vx=0, vy=0;
const aim = event => {
const bounds = canvas.getBoundingClientRect();
targetX=event.clientX-bounds.left;
targetY=event.clientY-bounds.top;
};
canvas.addEventListener('pointermove',aim);
canvas.addEventListener('pointerdown',aim);
canvas.addEventListener('keydown',event=>{
const direction = {ArrowLeft:[-22,0],ArrowRight:[22,0],ArrowUp:[0,-22],ArrowDown:[0,22]}[event.key];
if (!direction) return;
event.preventDefault();
targetX=Math.max(12,Math.min(width-12,targetX+direction[0]));
targetY=Math.max(12,Math.min(height-12,targetY+direction[1]));
});
loop((time,dt)=>{
if (new URLSearchParams(location.search).has('preview')) {
targetX=width/2+width*.26*Math.sin(time*2);
targetY=height/2+height*.24*Math.cos(time*3);
}
const damping = Math.exp(-7*dt);
vx=(vx+(targetX-x)*92*dt)*damping;
vy=(vy+(targetY-y)*92*dt)*damping;
x+=vx*dt;
y+=vy*dt;
ctx.clearRect(0,0,width,height);
ctx.strokeStyle='#ffd2a9';
ctx.beginPath();
ctx.moveTo(targetX,targetY);
ctx.lineTo(x,y);
ctx.stroke();
ctx.fillStyle='#ff996f';
ctx.beginPath();
ctx.arc(x,y,14,0,Math.PI*2);
ctx.fill();
});