shipped

Spring physics, from scratch

A damped spring, integrated by hand in six lines. Click to move the target, drag the sliders, watch ζ.

Sandbox

Under the hood

A spring pulls toward its target with force -k · (x - target) and loses energy to damping at -c · v. That’s the whole model. Every frame I add those up, nudge the velocity, then nudge the position with the new velocity (semi-implicit Euler). Swap the order and the spring slowly gains energy and blows up.

The physics runs on a fixed 1/240 s step, decoupled from the frame rate. A 60 Hz and a 120 Hz screen take the same steps, so the motion looks the same on both. Big variable steps are where hand-rolled springs go unstable.

One number tells you how it’ll feel: the damping ratio ζ = c / (2√k). Under 1 it overshoots and wobbles, at 1 it lands as fast as possible without overshooting, over 1 it creeps in. Most UI springs want something around 0.7 to 0.9.

Fork

spring.js
// Damped spring, integrated by hand. Click or tap the canvas, or focus it and use the
// arrow keys, to move the target. With reduced motion on, nothing animates: each change
// is settled off-screen and the finished trace is drawn once.
// mount(el) builds the whole thing inside el and returns a cleanup function.

const STEP = 1 / 240; // fixed physics step, independent of frame rate

export function mount(el) {
  el.innerHTML = `
    <canvas role="slider" tabindex="0" aria-label="Spring target position" aria-orientation="horizontal"
      aria-valuemin="0" aria-valuemax="100" aria-valuenow="80"
      style="display:block;width:100%;height:240px;cursor:crosshair;touch-action:none"></canvas>
    <div class="controls">
      <label>stiffness k <input type="range" name="k" min="20" max="600" value="170"><output></output></label>
      <label>damping c <input type="range" name="c" min="0" max="60" value="12"><output></output></label>
      <span class="readout"></span>
    </div>`;

  const canvas = el.querySelector('canvas');
  const ctx = canvas.getContext('2d');
  const inputs = { k: el.querySelector('[name=k]'), c: el.querySelector('[name=c]') };
  const readout = el.querySelector('.readout');

  let x = 0.2, v = 0, target = 0.8; // positions are 0..1 across the canvas
  let origin = x; // where the mass rests before the latest target move
  let trace = [];
  let colors = readColors(el);
  const motion = window.matchMedia('(prefers-reduced-motion: reduce)');

  const update = () => {
    for (const input of Object.values(inputs)) input.nextElementSibling.value = input.value;
    const k = +inputs.k.value, c = +inputs.c.value;
    const zeta = c / (2 * Math.sqrt(k)); // damping ratio, mass = 1
    const kind = zeta < 1 ? 'underdamped' : zeta === 1 ? 'critical' : 'overdamped';
    readout.textContent = `ζ ${zeta.toFixed(2)} · ${kind}`;
  };

  // Semi-implicit Euler: update velocity first, then position with the new velocity.
  const step = (k, c) => {
    v += (-k * (x - target) - c * v) * STEP;
    x += v * STEP;
  };

  // Reduced motion: run the whole move at once and keep the trace, one point per 1/60 s.
  const settle = () => {
    const k = +inputs.k.value, c = +inputs.c.value;
    x = origin; v = 0;
    trace = [x];
    for (let i = 1; i <= 10 * 240; i++) {
      step(k, c);
      if (i % 4 === 0 && trace.length < 240) trace.push(x);
      if (Math.abs(x - target) < 0.0005 && Math.abs(v) < 0.005) break;
    }
    draw(ctx, canvas, x, target, trace, colors);
  };

  const setTarget = (t) => {
    origin = target === t ? origin : x;
    target = Math.min(0.95, Math.max(0.05, t));
    canvas.setAttribute('aria-valuenow', String(Math.round(target * 100)));
    if (motion.matches) settle();
  };

  for (const input of Object.values(inputs)) {
    input.addEventListener('input', () => {
      update();
      if (motion.matches) settle();
    });
  }
  update();

  const onPointer = (e) => {
    const r = canvas.getBoundingClientRect();
    setTarget((e.clientX - r.left) / r.width);
  };
  canvas.addEventListener('pointerdown', onPointer);
  const onKey = (e) => {
    const next = { ArrowLeft: target - 0.05, ArrowDown: target - 0.05, ArrowRight: target + 0.05,
      ArrowUp: target + 0.05, Home: 0.05, End: 0.95 }[e.key];
    if (next === undefined) return;
    e.preventDefault();
    setTarget(next);
  };
  canvas.addEventListener('keydown', onKey);

  const redraw = () => motion.matches && draw(ctx, canvas, x, target, trace, colors);
  const onTheme = () => { colors = readColors(el); redraw(); };
  window.addEventListener('themechange', onTheme);
  window.addEventListener('resize', redraw);

  let last = performance.now(), acc = 0, raf = 0;
  const frame = (now) => {
    acc += Math.min(0.1, (now - last) / 1000);
    last = now;
    const k = +inputs.k.value, c = +inputs.c.value;
    while (acc >= STEP) {
      step(k, c);
      acc -= STEP;
    }
    trace.push(x);
    if (trace.length > 240) trace.shift();
    draw(ctx, canvas, x, target, trace, colors);
    raf = requestAnimationFrame(frame);
  };
  // Follows the OS setting live: on -> stop and settle, off -> pick the animation back up.
  const apply = () => {
    cancelAnimationFrame(raf);
    if (motion.matches) return settle();
    last = performance.now();
    acc = 0;
    raf = requestAnimationFrame(frame);
  };
  motion.addEventListener('change', apply);
  apply();

  return () => {
    cancelAnimationFrame(raf);
    motion.removeEventListener('change', apply);
    window.removeEventListener('themechange', onTheme);
    window.removeEventListener('resize', redraw);
  };
}

function draw(ctx, canvas, x, target, trace, c) {
  const dpr = window.devicePixelRatio || 1;
  const w = canvas.clientWidth, h = canvas.clientHeight;
  if (canvas.width !== w * dpr || canvas.height !== h * dpr) {
    canvas.width = w * dpr;
    canvas.height = h * dpr;
  }
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  ctx.clearRect(0, 0, w, h);

  // Trace: the last 240 frames of position, newest at the bottom.
  ctx.strokeStyle = c.faint;
  ctx.lineWidth = 1;
  ctx.beginPath();
  trace.forEach((p, i) => {
    const y = h - 16 - (trace.length - 1 - i) * ((h - 96) / 240);
    i ? ctx.lineTo(p * w, y) : ctx.moveTo(p * w, y);
  });
  ctx.stroke();

  // Target: a 1px line.
  ctx.strokeStyle = c.ink;
  ctx.beginPath();
  ctx.moveTo(Math.round(target * w) + 0.5, 0);
  ctx.lineTo(Math.round(target * w) + 0.5, h);
  ctx.stroke();

  // The mass.
  ctx.fillStyle = c.accent;
  ctx.fillRect(x * w - 16, 24, 32, 32);
}

function readColors(el) {
  const s = getComputedStyle(el);
  const get = (name, fallback) => s.getPropertyValue(name).trim() || fallback;
  return {
    accent: get('--accent', '#ff4f00'),
    ink: get('--ink-muted', '#a1a1a1'),
    faint: get('--line-strong', '#3d3d3d'),
  };
}