Physics-based Animation — Web Animation

Physics-based animation writes forces, not durations: the duration comes out by itself. A spring (Hooke's law: the further away, the harder it pulls)…

A motorbike's shock absorber has three parts that matter. Its spring pushes the wheel back to its normal position. The oil inside (damping) stops the bike from bouncing again and again. The rider's weight (mass) makes every movement slower. A shock with no oil left keeps bouncing, while a shock whose damping is too strong (its oil too thick) comes back very slowly.

Physics-based animation is built from the same three parts: a spring, damping and mass. Its code does not say "move 16 px in 300 ms with this curve". Its code writes down forces (pushes or pulls): a spring pulling toward the target, damping holding it back, friction slowing it. Then every frame, the position is calculated from those forces. The duration comes out by itself.

Spring force and damping

A spring follows Hooke's law: the further the element is from the target, the harder the spring pulls. Damping works against the velocity: the faster the element moves, the harder damping holds it back. In the formula, both carry a minus sign because they act in the opposite direction: the spring pulls back toward the target, and damping works against the direction of movement. Acceleration is the change in velocity per second:

spring force = -stiffness × (position - target)
damping force = -damping × velocity
acceleration = (spring force + damping force) / mass

Position in px, velocity in px per second, time in seconds. Stiffness, damping and mass are the same three numbers you write in transition={{ type: 'spring', stiffness, damping, mass }} in Motion.

One step: semi-implicit Euler

A real spring moves continuously, but a computer cannot calculate that way. It moves forward a little at a time, dt seconds per step. Each step runs in this order:

  1. Calculate the acceleration.
  2. Update the velocity first.
  3. Then update the position, with the new velocity.

Calculating in this order is called semi-implicit Euler. In this function, dt = 1 / 60 (one step is one frame at 60 Hz), and the loop runs 4 steps:

function simulateSpring(from, to, { stiffness, damping, mass }) {
  const dt = 1 / 60;
  let position = from, velocity = 0;
  const positions = [Number(position.toFixed(4))];
  for (let step = 0; step < 4; step++) {
    const acceleration = (-stiffness * (position - to) - damping * velocity) / mass;
    velocity = velocity + acceleration * dt;   // velocity first...
    position = position + velocity * dt;       // ...then position, with the new velocity
    positions.push(Number(position.toFixed(4)));
  }
  return positions;
}

Here is the trace (the values at each step) of simulateSpring(0, 100, { stiffness: 100, damping: 10, mass: 1 }), a spring pulling from 0 to 100:

Step Acceleration Velocity Position
start 0 0
1 (-100 × (0 - 100) - 0) / 1 = 10,000 166.67 2.7778
2 9,722 - 1,667 = 8,056 300.93 7.7932
3 14.534
4 22.5254

The result is [0, 2.7778, 7.7932, 14.534, 22.5254]. If the position is updated first, the method is called plain Euler. Its first step still uses a velocity of 0, so the result is [0, 0, 2.7778, 7.8704, 14.8148]. Plain Euler also adds energy (the power of the movement) bit by bit, so a lightly damped spring never settles. A spring already at its target (simulateSpring(50, 50, ...)) does not move, because its force is zero.

The damping ratio ζ

One number decides whether a spring bounces: the damping ratio ζ (zeta) = damping / (2 × √(stiffness × mass)).

stiffness, damping, mass ζ Kind A 16 px card
500, 25, 1 (Motion's default for y) 0.56 underdamped passes the target by 1.9 px
300, 30, 1 0.87 underdamped barely bounces (0.03 px)
300, 10, 1 0.29 underdamped bounces 6.2 px
100, 20, 1 1 critically damped stops exactly, as fast as possible without bouncing
100, 20, 4 0.5 underdamped four times the mass = it bounces
100, 25, 1 1.25 overdamped slow, never passes the target

Material 3 writes its springs directly with ζ. For example, motionSpringFastSpatial for buttons and switches has damping 0.9 (here that means ζ 0.9) and stiffness 1400. The "effects" springs (for colour and opacity) use ζ 1, so the value never passes its target.

A fixed step with an accumulator

In the requestAnimationFrame lesson, the time between frames varied: 8.3 ms at 120 Hz, 16.7 ms at 60 Hz, and longer when the page is busy. If dt varies with it, the physics comes out different on every screen. A large dt also makes the spring explode: the position jumps further at every step. The fix: collect real time in an accumulator (a variable that holds it, called acc in the code), then move forward in fixed steps:

const STEP = 1 / 120;                       // a fixed physics step: 8.3 ms
let acc = 0, last = null, x = 16, v = 0;    // the card starts 16 px below its target

function frame(now) {
  if (last !== null) acc += Math.min((now - last) / 1000, 0.1); // real time, capped
  last = now;
  while (acc >= STEP) {                     // 0, 1 or 2 steps per frame
    const a = (-300 * x - 10 * v) / 1;
    v += a * STEP; x += v * STEP;           // semi-implicit Euler
    acc -= STEP;
  }
  card.style.transform = `translateY(${x}px)`;
  const resting = Math.abs(v) <= 2 && Math.abs(x) <= 0.5;  // Motion's default spring rest thresholds
  if (resting) card.style.transform = 'none'; else requestAnimationFrame(frame);
}
requestAnimationFrame(frame);

Every frame, the time that has passed is added to acc, at most 0.1 seconds. The while loop then runs steps of 1/120 second while acc still holds enough. The card stops when its velocity is at most 2 px/s and its distance to the target is at most 0.5 px.

We compared this way of calculating with Motion 12.38's spring function, for a 16 px card. Motion does not calculate step by step: it uses a closed-form equation, which gives the position at any time directly. The rest thresholds (the speed and distance below which the spring counts as resting) follow Motion. In Motion, a spring whose numbers you write yourself uses 2 px/s and 0.5 px, the same as the code above. Motion's default spring for x and y (500, 25) carries its own restSpeed: 10, so the first row of the table stops at 10 px/s:

Spring Step 1/120 s Step 1/1000 s Motion
500, 25, restSpeed: 10 (default for y) rests at 275 ms, passes by 1.6 px 285 ms, 1.9 px 287 ms, 1.9 px
300, 30 333 ms 317 ms 315 ms
300, 10 725 ms, passes by 6.1 px 739 ms, 6.2 px 741 ms, 6.2 px

Semi-implicit Euler loses a little energy each step, so its bounce is slightly smaller. The smaller the step, the closer it gets to Motion. At 1/120 s the difference is at most 18 ms, which nobody can feel.

Momentum: a flicked gallery

Mrs. Sri's photo gallery holds the photos "Morning terrace / Room / Bathroom". When a guest flicks the gallery (moves it fast, then lifts the finger), the gallery still has a velocity, say 1,500 px per second. Friction reduces it until it is almost still (below 20 px per second), then a spring pulls it to the nearest photo (photos are 280 px wide). Take a guess first: the gallery's code cuts the velocity by 5% every frame (v *= 0.95), and on a 60 Hz laptop the gallery glides about 494 px. On a 120 Hz laptop, how far?

How the velocity drops 60 Hz 120 Hz
v *= 0.95 every frame 494 px, the Bathroom photo 247 px, the Room photo
v *= Math.pow(0.95, dt * 60) (time-based) 494 px, Bathroom 487 px, Bathroom

At 120 Hz the gallery glides only about 247 px, half, and the guest lands on a different photo. At 120 Hz there are twice as many frames per second, so the velocity is cut by 5% twice as often. The time-based version uses dt (the time since the previous frame, in seconds). Math.pow(0.95, dt * 60) cuts 5% for every 1/60 second that passes, whatever the screen's refresh rate. Its time constant (a measure of how fast the velocity drops) is -(1/60) / ln(0.95) = 325 ms, the same as the default timeConstant of inertia (the glide after a flick) in Motion 12.38. An equivalent formula: v *= Math.exp(-dt / 0.325).

The spring that pulls the gallery to the nearest photo starts with the velocity left over from the flick, so the movement carries on smoothly. That is why a spring suits gestures (finger movements, such as dragging or flicking) better than ease-out. A tween curve always starts at the speed its curve sets, not at the finger's speed. When "Reduce motion" is on, skip the glide and the bounce: go straight to the target photo with a 150 ms fade.

⚠️ Springs that explode, never settle, or differ per screen

TL;DR: Physics-based animation writes forces, not durations: the duration comes out by itself. A spring (Hooke's law: the further away, the harder it pulls): acceleration = (-stiffness × (position - target) - damping × velocity) / mass. Semi-implicit Euler (a way to calculate step by step): velocity first (v += a * dt), then position (x += v * dt). simulateSpring(0, 100, { stiffness: 100, damping: 10, mass: 1 }) with dt = 1/60 gives [0, 2.7778, 7.7932, 14.534, 22.5254], and a spring already at its target does not move. The damping ratio ζ = damping / (2 × √(stiffness × mass)): < 1 underdamped (bounces; 300/10 = 0.29, passing by 6.2 px), = 1 critically damped (100/20, stops at the target without bouncing), > 1 overdamped (slow); mass counts (100/20/4 = 0.5). Use a fixed 1/120 s step with an accumulator (a variable that collects time) and dt limited to 0.1 seconds. Motion's rest thresholds (the limits for stopping) are 2 px/s and 0.5 px (10 px/s for the default x/y spring); with that fixed step, the springs 500/25, 300/30 and 300/10 rest at 275, 333, 725 ms, in Motion at 287, 315, 741 ms. Friction must be time-based: with v *= 0.95 per frame, a flicked gallery glides 494 px at 60 Hz but 247 px at 120 Hz; Math.pow(0.95, dt * 60) = a 325 ms time constant (the same as Motion's inertia timeConstant). Springs suit gestures (finger movements) because they start from the finger's velocity. When "Reduce motion" is on, go straight to the target with a 150 ms fade.

⚡ Forces, not durations

Meaning: The code of a physics-based animation doesn't say "move 16 px in 300 ms". It writes down forces: a spring (stiffness) pulling toward the target, damping holding it back, and mass making the movement heavier. The position is calculated from those forces every frame, so the duration comes out by itself.

⚡ Velocity first, then position

Write:

const acceleration = (-stiffness * (position - to) - damping * velocity) / mass;
velocity = velocity + acceleration * dt;   // velocity first...
position = position + velocity * dt;       // ...then position, with the new velocity

Meaning: to is the target position, and dt the length of one step, in seconds. Each step, this code calculates the acceleration from the spring and damping forces, then updates the velocity first, then the position. This order is called semi-implicit Euler. If the position is updated first, energy is added bit by bit, and a lightly damped spring never settles.

⚡ The damping ratio ζ

Guess first: how does a spring with very low damping move?

It passes the target, then bounces back and forth before it rests. This movement is called underdamped.

Meaning: The damping ratio ζ (zeta) = damping / (2 × √(stiffness × mass)). Below 1, the spring bounces. At exactly 1, it stops right at the target without bouncing (critically damped). Above 1, it is slow and never passes the target (overdamped).

⚡ Time-based friction

Guess first: a photo gallery flicked by a finger cuts its velocity by 5% every frame (v *= 0.95), and glides 494 px on a 60 Hz laptop. How far at 120 Hz?

Only about 247 px, half, because at 120 Hz the velocity is cut twice as often.

Meaning: Use v *= Math.pow(0.95, dt * 60), with dt = the time since the previous frame, in seconds. Then friction counts per second, not per frame.

What you will learn