Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 39 ยท Animation and Interaction

The Three.js Animation Loop and Timing

Animate Three.js scenes with setAnimationLoop, Clock and Timer, delta time, frame-rate independence, lerp, damping and easing functions, with runnable examples.

Up to now most scenes have rendered once. A 3D scene comes alive when you redraw it many times per second while changing something between frames: a rotation, a position, a color. The redraw cycle is called the animation loop (or render loop), and getting its timing right is the difference between smooth motion that runs identically on every device and motion that is twice as fast on a gaming monitor.

In this lesson you will build the loop, measure time with Clock and Timer, make animation independent of the frame rate, and use interpolation and easing to make movement feel natural.

The render loop

The browser calls a function before every repaint when you hand it to requestAnimationFrame. Three.js wraps this in renderer.setAnimationLoop, which is the recommended entry point because it also works with WebXR headsets and WebGPU:

JavaScript
renderer.setAnimationLoop((time) => {
  // update the world...
  renderer.render(scene, camera);
});

The callback receives a timestamp in milliseconds. To stop the loop, call renderer.setAnimationLoop(null). Browsers also pause animation frames for hidden tabs, which saves battery for free.

flowchart LR
  A["Browser frame"] --> B["Your callback(time)"]
  B --> C["Update objects"]
  C --> D["renderer.render()"]
  D --> E["Screen"]
  E -.-> A

Every frame: update the world, render it, repeat.

Delta time

The naive version adds a fixed amount per frame: cube.rotation.y += 0.02. On a 60 Hz screen that is 1.2 radians per second; on a 144 Hz screen it is 2.9 radians per second, and the animation looks twice as fast. The fix is to scale every change by the delta time: the seconds since the previous frame.

JavaScript
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
  const dt = clock.getDelta();          // seconds since the last call
  cube.rotation.y += 1.5 * dt;          // 1.5 radians per SECOND, on any screen
  renderer.render(scene, camera);
});

clock.getElapsedTime() gives the total seconds since the clock started, which suits animations expressed as a function of time (Math.sin(t)). Do not mix getDelta() and getElapsedTime() on the same clock, because getElapsedTime() calls getDelta() internally; call getDelta() once per frame and accumulate your own total if you need both.

Newer Three.js versions also include THREE.Timer, which you feed the browser timestamp. With timer.connect(document) it ignores the time a hidden tab spent paused, so you avoid a huge delta when the user comes back:

JavaScript
const timer = new THREE.Timer();
renderer.setAnimationLoop((timestamp) => {
  timer.update(timestamp);
  const dt = timer.getDelta();
  const t = timer.getElapsed();
});
Delta-time animation with Clock and Timer

Frame-rate independence, in numbers

You can prove why delta time matters without any graphics. The console example below simulates one second of animation on three different displays and compares the two approaches.

JS Why delta time matters (console)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The per-frame version covers 6, 12 or 28.8 units depending on the display; the delta version always covers 10. Likewise the smoothing function reaches the same value at any frame rate, because the decay factor 1 - Math.exp(-lambda * dt) accounts for the step size. The naive current += (target - current) * 0.1 is frame-rate dependent.

Interpolation and easing

Rather than moving at a constant speed, objects usually accelerate and settle. Three.js includes helpers in THREE.MathUtils:

Helper What it does
lerp(a, b, t) Linear blend of two numbers, t from 0 to 1
damp(current, target, lambda, dt) The frame-rate independent smoothing above
smoothstep(x, min, max) S-curve between 0 and 1
pingpong(x, length) Bounces a value back and forth between 0 and length
clamp, mapLinear, degToRad Everyday helpers

Vectors and quaternions have their own lerp (and Quaternion.slerp for rotations, which avoids the odd shortcuts that interpolating Euler angles produces). An easing function reshapes progress t before you interpolate:

JavaScript
const easeInOutCubic = (t) => (t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2);
const easeOutBack = (t) => 1 + 2.70158 * (t - 1) ** 3 + 1.70158 * (t - 1) ** 2;
Easing functions side by side

Practical tips for loops

  • Keep the update cheap. Do not allocate objects (new Vector3()) inside the loop; reuse temporaries to avoid garbage collection stutters.
  • Cap huge deltas. After a tab was hidden, the next dt can be several seconds. Use Math.min(dt, 0.1) so physics and tweens do not explode.
  • Render on demand. For a static product viewer, call renderer.render only when something changes (a control event, a finished load). This saves GPU power.
  • Separate logic and rendering. Compute positions from t and state, then render; this makes recording, pausing and slow-motion simple (multiply dt by a time scale).
  • Use renderer.setPixelRatio(Math.min(devicePixelRatio, 2)) so a 3x phone screen does not triple your fill cost.
  • Resize correctly. On a window change update camera.aspect, call camera.updateProjectionMatrix() and renderer.setSize.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is cube.rotation.y += 0.02 each frame a problem?

    A fixed step per frame runs faster on 144 Hz screens than on 60 Hz ones. Multiply by delta time to get a speed per second.
  2. 2. What does clock.getDelta() return?

    getDelta returns the elapsed seconds since the last call, the value you multiply speeds by.
  3. 3. Which call stops the loop started by renderer.setAnimationLoop(fn)?

    Passing null removes the callback and ends the loop.
  4. 4. Which function reshapes progress t from 0 to 1 so motion starts slowly and ends slowly?

    Easing functions remap t before it is passed to lerp, giving acceleration and deceleration.
  5. 5. Why is MathUtils.damp preferred over current += (target - current) * 0.1?

    damp includes dt in an exponential decay, so the result does not depend on how many frames the second contained.

Summary

  • renderer.setAnimationLoop runs your update-and-render function every frame; pass null to stop it.
  • Scale changes by delta time (Clock or Timer) so speed is per second, not per frame.
  • Use MathUtils.lerp, damp, pingpong and easing functions for natural motion; use slerp for rotations.
  • Reuse objects in the loop, cap large deltas and render on demand when nothing moves.

Next: Keyframe Animation and AnimationMixer