2D Rigid Body Particle Collision.
Simulating elastic collisions, restitution, and gravitational fields across dynamic circular discs in real time.
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; } }// 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);
const discs = Array.from({length:12}, (_,i)=>({
x:width*(.14+.72*(i%6)/5), y:height*(.2+.22*Math.floor(i/6)),
vx:(i%2 ? 1 : -1)*(42+i*6), vy:0, r:7+i%4*3
}));
const boost = () => discs.forEach((ball,i) => {
ball.vx += (i%2 ? 1 : -1)*35;
ball.vy = -155-i*12;
});
canvas.addEventListener('pointerdown',boost);
canvas.addEventListener('keydown',event=>{
if (!['Enter',' '].includes(event.key)) return;
event.preventDefault();
boost();
});
loop((_,dt)=>{
ctx.clearRect(0,0,width,height);
discs.forEach((ball,i)=>{
ball.vy += 390*dt;
ball.vx *= Math.exp(-.16*dt);
ball.x += ball.vx*dt;
ball.y += ball.vy*dt;
if (ball.x < ball.r || ball.x > width-ball.r) {
ball.x = Math.max(ball.r,Math.min(width-ball.r,ball.x));
ball.vx *= -.83;
}
if (ball.y > height-ball.r) {
ball.y = height-ball.r;
ball.vy = -Math.max(85 + i*6,Math.abs(ball.vy)*.83);
}
ctx.beginPath();
ctx.arc(ball.x,ball.y,ball.r,0,Math.PI*2);
ctx.fillStyle = ['#ff9978','#ffe08a','#ffcaee'][i%3];
ctx.fill();
});
});