Search⌘ K
AI Features

Animations in Three.js

Explore how to create basic animations in Three.js by using the render loop and requestAnimationFrame. Understand how to smoothly animate 3D object properties such as rotation, position, and scale, and learn how to incorporate dynamic camera movements for engaging 3D scenes.

Basic animations

Before we look at the examples, let’s quickly review the render loop for creating 3D scenes. To support animations, we need to tell Three.js to render the scene every so often. For this, we use the standard HTML5 requestAnimationFrame functionality, as follows:

Javascript (babel-node-es2024)
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();

With this code, we only need to call the render() function (line 3) once we’ve initialized the scene. In the render() function itself, we use requestAnimationFrame (line 2) to schedule the next rendering. This way, the browser will make sure the render() function is called at the correct interval (usually ...