kipluo
React8 min read3 steps

How to Implement Spring Physics Without a Heavy Animation Library

Spring animation is just Hooke's law plus damping integrated over time. With fixed-timestep semi-implicit Euler integration, you get identical physics across 60 Hz, 120 Hz, and throttled tabs in under 50 lines of code.

You needTypeScriptrequestAnimationFrame
Loading demo
Drag and release the slider thumb to see spring velocity settlement without any external animation runtime.Component page

1. The exact equation behind every UI spring

A damped harmonic oscillator computes acceleration from two forces: spring pull toward the target (-stiffness * (x - target)) and friction opposing velocity (-damping * v). Dividing by mass gives acceleration a = F / m.

Unlike cubic-bezier durations, springs don't ask 'how many milliseconds?'. They preserve current velocity when a target changes mid-flight, which is why interrupted UI interactions feel natural.

2. Use fixed 120 Hz sub-steps so springs never explode

If a user switches browser tabs for 3 seconds and returns, a raw frame delta dt = 3.0s will blow up Euler integration to Infinity. Clamp dt to at most 1/20s (50ms) and advance in fixed 1/120s sub-steps so behavior is identical on 60 Hz laptops and 120 Hz ProMotion phones.

spring.ts
export type SpringConfig = { stiffness: number; damping: number; mass?: number };

export const PRESETS = {
  snappy: { stiffness: 420, damping: 30, mass: 1 },
  bouncy: { stiffness: 260, damping: 14, mass: 1 },
  gentle: { stiffness: 140, damping: 20, mass: 1 },
} as const;

const STEP = 1 / 120;

export class Spring1D {
  value: number;
  target: number;
  velocity = 0;

  constructor(initial = 0) {
    this.value = initial;
    this.target = initial;
  }

  step(cfg: SpringConfig, dt: number): boolean {
    const mass = cfg.mass ?? 1;
    let remaining = Math.min(dt, 1 / 20); // Clamp tab-resume spikes

    while (remaining > 0) {
      const h = Math.min(remaining, STEP);
      const force = -cfg.stiffness * (this.value - this.target) - cfg.damping * this.velocity;
      this.velocity += (force / mass) * h; // Semi-implicit Euler updates velocity first
      this.value += this.velocity * h;
      remaining -= h;
    }

    const moving = Math.abs(this.velocity) > 0.02 || Math.abs(this.value - this.target) > 0.02;
    if (!moving) {
      this.value = this.target;
      this.velocity = 0;
    }
    return moving;
  }
}

3. Three presets cover 95% of product UI

You rarely need to invent new spring math per component. Use a snappy preset (stiffness 420, damping 30) for toggles, tabs, and tooltips; a bouncy preset (260, 14) for rewards and icons; and a gentle preset (140, 20) for sheets and drawers.

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 2 checked

Build it yourself or install it

Build it yourself when

You want full ownership of a tiny 50-line physics core for custom animations.

Install Kipluo when

Kipluo's open @kipluo/core uses this exact fixed-step semi-implicit Euler integrator—and pairs it with 75+ components, 1,036 icons, and an identical Dart port for Flutter.

Keep building