Every frame: force → velocity → position. Swap the update order, change dt, lower the fps, stall a frame, and watch the energy: flat, blowing up, or draining…
Why it matters
Fireworks, snow, sparks, ragdolls in games: all the same loop. Once you understand the update order and dt, you know why your animation blows up, why it turns to slow motion on another phone, and why the particles leap when you come back from another tab.
The key idea
Physics on screen is only a guess per step
Common mistakes
The system blows up / the particles shake wildly Usually the update order: plain Euler (x += v·dt before v += a·dt) adds energy every step, and a stiff spring with a big dt makes it worse. Update the velocity first (semi-implicit Euler), and add a little drag a = -c·v so extra energy drains away.
The physics runs at different speeds on different devices The code takes every frame as 1/60 s. At 30 fps it turns to slow motion, at 120 Hz it runs too fast. Use dt = (now - last) / 1000 from the requestAnimationFrame timestamp and multiply every change by dt.
The simulation drifts / is inaccurate at big steps Euler is only a guess per step: the bigger dt, the further it misses, and one stalled frame (a tab left alone, a busy main thread) becomes one giant step. Cap dt (Math.min(dt, 0.1)), split it into a few fixed sub-steps, or use Verlet/RK4 when you need accuracy.
Try it in this playground
Drag the anchor on the board The spring’s anchor follows the pointer (or the Anchor arrow buttons). The yellow arrow (acceleration) always points at the anchor; the blue arrow (velocity) slowly turns after it.
Switch update() to euler The energy line on the board keeps rising: the orbit widens until particles fly off. No new force, only the order of two lines.
Pick dt = 1 / 60, then 30 fps Simulated time becomes 0.50× of real time: everything in slow motion, exactly what happens on a phone that only manages 30 fps.
Press Stall 1 frame This page really stops for 0.5 seconds. The next frame gets a dt that big, and the energy jumps. Then pick the max dt Math.min(dt, 0.1) and try again.