One of the most engaging aspects of 3D web experiences is the ability to bring objects to life through animation, and even more so when that animation is directly controlled by the user's actions. In this section, we'll explore how to trigger animations in response to user input, making your Three.js scenes feel dynamic and interactive.
The core idea is to listen for specific user events, such as mouse clicks, keyboard presses, or mouse movements, and then use those events to alter an object's properties over time. This often involves manipulating the object's rotation, position, or scale.
A common way to manage animations is by using the requestAnimationFrame loop. This function tells the browser that you want to perform an animation and requests that your browser call a specified function to update an animation before the next repaint. Inside this loop, we can check if an animation is active and update the object's transformation accordingly. We'll then use a flag or a state variable to control whether the animation should be running.
let isAnimating = false;
let objectToAnimate = null;
function animate() {
requestAnimationFrame(animate);
if (isAnimating && objectToAnimate) {
objectToAnimate.rotation.y += 0.01;
}
renderer.render(scene, camera);
}
animate();Now, let's tie this animation loop to user input. For instance, we can use a click event listener on the renderer.domElement (the canvas where your 3D scene is rendered) to toggle the isAnimating flag.
renderer.domElement.addEventListener('click', () => {
isAnimating = !isAnimating;
});When the canvas is clicked, the isAnimating variable will flip its value. If it was false, it becomes true, and the animation inside the requestAnimationFrame loop will start for objectToAnimate. If it was true, it becomes false, and the animation will pause.
We also need to ensure that objectToAnimate is properly assigned. This could be done by selecting an object from the scene, perhaps by raycasting or by having a reference to it. Let's assume you have a reference to a cube object called myCube.