Animation Loops and Model Animation
🎯 Introduction
By the end of this article, you will be able to build a stable render loop, keep animation speed consistent across refresh rates, and use AnimationMixer to play, pause, and switch glTF model actions.
🧱 requestAnimationFrame and delta time
renderer.render() draws a single frame. requestAnimationFrame invokes a callback before the next screen repaint. Scheduling it repeatedly creates a render loop:
const clock = new THREE.Clock();
let frameId = 0;
const animate = () => {
frameId = requestAnimationFrame(animate);
const delta = clock.getDelta();
cube.rotation.y += delta * 0.8; // Rotate approximately 0.8 radians per second
renderer.render(scene, camera);
};
animate();
delta is the time in seconds since the previous frame. Driving animation with time rather than a fixed increment per frame produces more consistent speeds on 60 Hz and 120 Hz displays.
Use clock.getElapsedTime() to calculate periodic animation from total elapsed time:
const elapsed = clock.getElapsedTime();
cube.position.y = Math.sin(elapsed * 2) * 0.3;
This demo illustrates the basic animation code above. The object continuously rotates and bobs up and down as time advances:
🛠 Playing glTF animations with AnimationMixer
After loading a model, animation clips are usually available in gltf.animations. AnimationMixer advances animation time:
Think of AnimationClip as a recorded motion, AnimationAction as its playback instance on the current model, and AnimationMixer as the system advancing all actions over time. Finding a clip without calling play(), or omitting mixer.update(delta) from the loop, leaves the model stationary.
const mixer = new THREE.AnimationMixer(gltf.scene);
if (gltf.animations.length === 0) return;
const clip = gltf.animations[0];
if (!clip) return;
const action = mixer.clipAction(clip);
action.reset().play();
const animate = () => {
requestAnimationFrame(animate);
mixer.update(clock.getDelta());
renderer.render(scene, camera);
};
animate();
For models with multiple actions, select a clip by name using gltf.animations.find((item) => item.name === 'Walk'). Here is how to pause, resume, and switch actions:
action.paused = true;
action.paused = false;
const nextAction = mixer.clipAction(nextClip);
nextAction.reset().fadeIn(0.25).play();
action.fadeOut(0.25);
Use a short fade when switching actions to avoid abrupt pose changes. Check gltf.animations.length before playback, since not every model includes animations.
This demo uses a glTF model with animation clips to show how AnimationMixer.update(delta) advances model animation:
🧾 Summary
requestAnimationFramesynchronizes with browser repainting.Clock.getDelta()advances animation using frame time differences.getElapsedTime()is useful for periodic motion.AnimationMixermanages glTF animation clips.AnimationActioncontrols playback, pausing, looping, and fading.- On component unmount, cancel animation frames and stop using the mixer.
❓ Questions and answers
Q1: Why not add 0.01 each frame?
Different frame rates produce different speeds. Using delta * speed gives a more consistent change per second.
Q2: Why is the model stationary even though I call mixer.update?
Check that gltf.animations is not empty, the clip name is correct, and clipAction(...).play() has been called.
Q3: Why does playback jump after pausing?
Avoid recreating the action. Toggle paused on the same action between true/false to preserve its current playback time.
🧪 Exercise
Create a rotating cube driven by Clock.getDelta(). Then make it bob up and down using Math.sin, and cancel animation frames on unmount.
// Write your code here
🎉 You now know how to turn a static view into continuous animation. Next, raycasting will connect the pointer to 3D objects.
