Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 39 ยท Getting Started

The Render Loop and Timing

Build a correct Three.js animation loop with setAnimationLoop, delta time and Clock so motion runs at the same speed on every screen, with pausing tips.

A 3D scene that moves is a loop that runs once per display refresh. On most screens that is 60 times per second, but on a 144 Hz gaming monitor it is 144, and on a struggling phone it might drop to 30. If you write cube.rotation.y += 0.02 in the loop, the cube spins faster on the faster screen. The fix is to base motion on time, not on frames. This lesson shows how.

requestAnimationFrame and setAnimationLoop

The browser function requestAnimationFrame(callback) calls your function right before the next repaint, and pauses automatically when the tab is hidden. You call it again inside the callback to keep going.

JavaScript
function tick(timeMs) {
  // update and render here
  requestAnimationFrame(tick);
}
requestAnimationFrame(tick);

Three.js offers renderer.setAnimationLoop(callback), which does the same thing and passes you the timestamp. It is the preferred method because it also works with WebXR (virtual reality), where the browser decides when frames happen. Stop the loop with renderer.setAnimationLoop(null).

flowchart LR
  A["Browser: next frame"] --> B["Your callback"]
  B --> C["Update objects"]
  C --> D["renderer.render"]
  D --> A

One turn of the loop is one frame.

Delta time

The callback receives the time in milliseconds since the page started. Subtract the previous value to get delta time: how long the last frame took. Multiply every speed by delta and motion becomes frame-rate independent.

JavaScript
let last = 0;
renderer.setAnimationLoop((now) => {
  const delta = (now - last) / 1000; // seconds
  last = now;
  cube.rotation.y += delta * Math.PI;  // half a turn per second, on any screen
  renderer.render(scene, camera);
});

Now the rule is simple: speeds are expressed per second, and every update multiplies by delta.

THREE.Clock

THREE.Clock does this subtraction for you. clock.getDelta() returns seconds since the last call, and clock.getElapsedTime() returns seconds since it started. Do not call both in the same frame, since getElapsedTime calls getDelta internally and will steal the interval.

JavaScript
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
  const delta = clock.getDelta();
  const elapsed = clock.elapsedTime; // reading the property is safe
  ...
});

clock.elapsedTime is updated by getDelta(), so reading the property is the safe way to also get total time.

Delta time in action

The demo shows two cubes spinning side by side. The left one uses a fixed step per frame; the right one uses delta time. On your machine they may look the same, but the right one is guaranteed to complete one turn every four seconds. We also show a sine wave bobbing, which depends on elapsed time, and a live frame rate reading.

Frame-based vs time-based motion

Clamping delta

When a tab is hidden, requestAnimationFrame stops. When you return, the first delta can be several seconds long, and an object moving at constant speed would suddenly jump. Clamp the value to a sensible maximum:

JavaScript
const delta = Math.min(clock.getDelta(), 0.1);

Animation by formula

Time gives you easy procedural animation. Feed elapsed into a function and assign the result.

Effect Formula
Bobbing up and down y = Math.sin(t * speed) * amplitude
Orbiting in a circle x = Math.cos(t) * r; z = Math.sin(t) * r
Pulsing size s = 1 + Math.sin(t * 3) * 0.1
Smooth move to a target value += (target - value) * (1 - Math.exp(-k * delta))

The last row, exponential smoothing, is a favorite for cameras and cursor following, because it looks natural and stays stable even when delta varies.

Orbits from time

Stopping and pausing

Stop the loop with renderer.setAnimationLoop(null) and restart it with the callback. Pausing when the canvas is off screen saves battery; later chapters on performance use IntersectionObserver for that. Rendering on demand, only when something changed, is the right pattern for static viewers: call renderer.render() from the event handler that changed the scene.

If you use Clock and also want a pause button, keep your own paused flag and still call getDelta() every frame, then skip the update. Otherwise the first delta after resuming includes the whole pause.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

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

    Per-frame steps tie speed to the refresh rate. Multiply a per-second speed by delta time instead.
  2. 2. What does clock.getDelta() return?

    getDelta returns the elapsed time in seconds since it was last called.
  3. 3. Why prefer renderer.setAnimationLoop to requestAnimationFrame?

    XR sessions drive their own frame callbacks, and setAnimationLoop handles that transparently.
  4. 4. Why clamp delta with Math.min(delta, 0.1)?

    After a hidden tab the first delta can be seconds long. Clamping avoids a large sudden step.

Summary

  • The render loop updates objects and calls renderer.render() every frame.
  • Use renderer.setAnimationLoop, and express speeds per second multiplied by delta time.
  • THREE.Clock gives getDelta() and elapsedTime; do not mix getElapsedTime() with getDelta().
  • Clamp delta, and use sine and cosine of elapsed time for procedural motion.
  • For static scenes, render on demand instead of continuously.

Next: Helpers and Debugging Tools