Lesson 6 of 9 · 25m
Animation Timing: requestAnimationFrame, Delta Time, and Determinism
Design animation loops that stay smooth under variable frame rates, separate simulation from rendering, respect reduced motion, and avoid time-based glitches.
Animation bugs usually appear as physics bugs, but the root cause is time. A frame arrives late, a laptop switches to 120 Hz, a background tab throttles callbacks, and objects tunnel through walls or move twice as fast. requestAnimationFrame does not promise a fixed timestep; it gives you an opportunity to draw before repaint.
Use the timestamp you are given
function frame(now) {
if (last === undefined) last = now
const dt = Math.min((now - last) / 1000, 0.05); last = now
update(dt); render(); requestAnimationFrame(frame)
}The callback timestamp is aligned with the frame. Clamp dt; otherwise a tab suspended for five seconds produces a huge simulation step.
Delta time is necessary, not sufficient
entity.x += entity.vx * dt is frame-rate independent only for simple motion. Collision detection can miss fast objects, springs can explode, and event ordering can vary when steps are irregular.
For correctness-sensitive systems, use a fixed timestep accumulator:
const step = 1 / 60
accumulator += dt
while (accumulator >= step) { simulate(step); accumulator -= step }Simulation advances deterministically at 60 Hz. Rendering can interpolate between previous and current states for smoothness.
Separate model state from visual state
simulation state positions, velocities, selection, logical time
presentation state easing, particles, camera shake, hover animation
rendering canvas/SVG/WebGL drawing commandsDo not let easing mutate core state. If a card slides in, animate its presentation transform while its logical position is already final. Visual polish should not corrupt selection, gameplay, or data truth.
Motion belongs on compositor-friendly properties
For SVG and DOM overlays, animate transform and opacity; be skeptical of width, height, top, left, filter, and box-shadow in hot paths. Respect the user's motion preference:
@media (prefers-reduced-motion: reduce) { .pulse { animation: none; } }Use the same signal in canvas to reduce camera shake, parallax, particle density, and long easing. Do not remove communication; state can change instantly or fade subtly.
Hidden tabs are a time jump
Pause rendering when hidden, but decide what time means. A wall-clock countdown may need to catch up; a physics simulation should not simulate thirty minutes in one burst. Server-backed or authoritative time belongs outside animation-local clocks.
Field notes
setIntervalis not synchronized with paint and is not a smooth rendering primitive.- Multiple independent
requestAnimationFrameloops become hard to coordinate; centralize scheduling. - Fixed-step interpolation requires both previous and current simulation states.
- Coalesce expensive pointermove processing into the frame instead of doing it synchronously for every event.
- Never use animation frames as a security timer, business scheduler, or authoritative game clock.
Was this lesson helpful?