Basic animations
Before we look at the examples, let’s do a quick recap of what was shown in Chapter 1, on the render loop. 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:
function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate();
With this code, we only need to call the render()
function once we’ve initialized the scene. In the render()
function itself, we use requestAnimationFrame
to schedule the next rendering. This way, the browser will make sure the render()
function is called at the correct interval (usually around 60 times or 120 times a second). Before requestAnimationFrame
was added to browsers, setInterval(function, interval)
or setTimeout(function, interval)
was used. These would call the specified function once every set interval.
The problem with this approach is that...