Answer the question
In order to leave comments, you need to log in
How to play and stop animation of individual three.js objects?
I recently started to study three.js, downloaded an example from off site , tried to make an animation stop if the object is "assembled" and play the animation when the button is clicked.
function animate() {
if (anim){
id = requestAnimationFrame( animate );
render();
stats.update();
if (uniforms.amplitude.value <= 0.0005) {
anim = false;
}
}
}
function render() {
timer += 0.001;
uniforms.amplitude.value = 1.0 + Math.sin( timer* 50);
controls.update();
renderer.render( scene, camera );
}
document.getElementById("but").addEventListener("click", function(){
anim = true;
animate();
});
function animate() {
id = requestAnimationFrame( animate );
if (anim){
timer += 0.001;
uniforms.amplitude.value = 1.0 + Math.sin( timer* 50);
controls.update();
renderer.render( scene, camera );
if (uniforms.amplitude.value <= 0.0005) {
anim = false;
}
} else {
render();
}
stats.update();
}
function render() {
controls.update();
renderer.render( scene, camera );
}
Answer the question
In order to leave comments, you need to log in
In general, I managed to fix this by stopping the animation before each re-call of it
function animate() {
if (anim) {
cancelAnimationFrame(id)
id = requestAnimationFrame( animate );
explode();
} else {
cancelAnimationFrame(id)
id = requestAnimationFrame( animate );
render();
}
stats.update();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question