Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Gravity, walls and collisions in a requestAnimationFrame loop. Pull back and let go to launch a ball.
import 'nuclo' type Ball = { x: number; y: number; vx: number; vy: number; r: number; color: string } const W = 320 const H = 200 const COLORS = ['#ff3f00', '#2563eb', '#ff8a00'] const rand = (min: number, max: number) => Math.round(min + Math.random() * (max - min)) let balls: Ball[] = Array.from({ length: 7 }, (_, i) => ({ x: 30 + i * 42, y: 40 + (i % 3) * 30, vx: i % 2 ? 2 : -2, vy: 0, r: 10 + (i % 3) * 4, color: COLORS[i % 3], })) let gravity = true let aim: { x: number; y: number; toX: number; toY: number } | null = null const at = (e: PointerEvent) => { const box = (e.currentTarget as Element).getBoundingClientRect() return [ ((e.clientX - box.left) / box.width) * W, ((e.clientY - box.top) / box.height) * H, ] } // dt is the frame's length in 60 fps frames. function step(dt: number) { for (const b of balls) { if (gravity) b.vy += 0.3 * dt b.x += b.vx * dt b.y += b.vy * dt // Walls: back inside, bounce, lose a little energy. if (b.x < b.r) { b.x = b.r; b.vx = Math.abs(b.vx) * 0.85 } if (b.x > W - b.r) { b.x = W - b.r; b.vx = -Math.abs(b.vx) * 0.85 } if (b.y < b.r) { b.y = b.r; b.vy = Math.abs(b.vy) * 0.85 } if (b.y > H - b.r) { b.y = H - b.r; b.vy = -Math.abs(b.vy) * 0.85; b.vx *= 0.98 } } // Ball against ball: push the pair apart, then trade momentum // along the line between the centers. The mass is r². for (let i = 0; i < balls.length; i++) { for (let j = i + 1; j < balls.length; j++) { const a = balls[i] const b = balls[j] const dx = b.x - a.x const dy = b.y - a.y const dist = Math.hypot(dx, dy) || 1 const overlap = a.r + b.r - dist if (overlap <= 0) continue const nx = dx / dist const ny = dy / dist a.x -= (nx * overlap) / 2; a.y -= (ny * overlap) / 2 b.x += (nx * overlap) / 2; b.y += (ny * overlap) / 2 const v = (b.vx - a.vx) * nx + (b.vy - a.vy) * ny if (v >= 0) continue const impulse = (1.85 * v) / (a.r ** 2 + b.r ** 2) a.vx += impulse * b.r ** 2 * nx; a.vy += impulse * b.r ** 2 * ny b.vx -= impulse * a.r ** 2 * nx; b.vy -= impulse * a.r ** 2 * ny } } } // The frame loop steps the physics and calls update('balls'), // which refreshes only the scoped element, so the handlers just // change state. when() shows the aiming line while pulling back. export const Balls = () => div( scope('balls'), svgSvg( { viewBox: '0 0 320 200', width: '320', height: '200', style: { touchAction: 'none' }, onMount: () => { let last = performance.now() let id = requestAnimationFrame(function frame(now: number) { step(Math.max(0, Math.min(2, (now - last) / (1000 / 60)))) last = now update('balls') id = requestAnimationFrame(frame) }) return () => cancelAnimationFrame(id) }, onPointerDown: (e) => { e.currentTarget.setPointerCapture(e.pointerId) const [x, y] = at(e) aim = { x, y, toX: x, toY: y } }, onPointerMove: (e) => { if (aim) [aim.toX, aim.toY] = at(e) }, onPointerUp: () => { if (!aim) return balls.push({ x: aim.x, y: aim.y, vx: (aim.x - aim.toX) / 8, vy: (aim.y - aim.toY) / 8, r: rand(8, 18), color: COLORS[balls.length % 3], }) if (balls.length > 30) balls.shift() aim = null }, }, rectSvg({ x: 0.5, y: 0.5, width: W - 1, height: H - 1, rx: 12, fill: 'none', stroke: '#ddd', }), list(() => balls, (b) => gSvg( { transform: () => `translate(${b.x.toFixed(1)} ${b.y.toFixed(1)})` }, circleSvg({ r: b.r, fill: b.color }), // a small highlight circleSvg({ cx: -b.r * 0.35, cy: -b.r * 0.35, r: b.r * 0.32, fill: 'white', 'fill-opacity': 0.35, }), ), ), when(() => aim !== null, lineSvg({ stroke: '#999', 'stroke-width': 2, 'stroke-dasharray': '4 4', x1: () => aim?.x ?? 0, y1: () => aim?.y ?? 0, x2: () => (aim ? 2 * aim.x - aim.toX : 0), y2: () => (aim ? 2 * aim.y - aim.toY : 0), }), circleSvg({ r: 12, fill: 'none', stroke: '#999', 'stroke-width': 2, cx: () => aim?.x ?? 0, cy: () => aim?.y ?? 0, }), ), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, button(() => (gravity ? 'Gravity on' : 'Gravity off'), { 'aria-pressed': () => String(gravity), onClick: () => { gravity = !gravity }, }), button('Shake', { onClick: () => { for (const b of balls) { b.vx += rand(-6, 6); b.vy -= rand(4, 9) } }, }), button('Clear', { onClick: () => { balls = [] } }), ), div(() => `${balls.length} balls · pull back and let go to launch one`), )