2 min read

Spring physics for interfaces

Easing curves describe time, springs describe behavior. Once you understand three numbers, your interfaces start to feel like they were made of something real.

Abstract white waves with soft shadows

A cubic bezier always takes the same time, no matter where the element starts or how fast the user flicked it. A spring doesn't have a duration at all. It has a target, a velocity and a few physical properties, and it simply moves until it comes to rest.

Stiffness, damping and mass

  • Stiffness is how strongly the spring pulls towards the target. Higher values feel snappier.
  • Damping is the friction that slows it down. Low damping bounces, high damping glides.
  • Mass is how heavy the element feels. More mass means slower starts and longer overshoots.

A spring in 20 lines

javascript
function spring(el, target, { stiffness = 170, damping = 26, mass = 1 } = {}) {
    let position = 0, velocity = 0, last = performance.now();

    function step(now) {
        const dt = Math.min((now - last) / 1000, 1 / 30);
        const force = -stiffness * (position - target) - damping * velocity;

        velocity += (force / mass) * dt;
        position += velocity * dt;
        last = now;

        el.style.transform = `translateX(${position}px)`;

        if (Math.abs(velocity) > 0.01 || Math.abs(position - target) > 0.01) {
            requestAnimationFrame(step);
        }
    }

    requestAnimationFrame(step);
}

Start with stiffness 170 and damping 26 for most interface movement. Use lower damping only for playful moments, like a confirmation or an empty state. Bouncy navigation gets tiring after the third click.