Exploring the possibilities of three.js
I'm attempting to create an animation (rotation/movement) for an object upon a mouse click event, however, I am not achieving the desired animation effects.
Below is the code snippet that I have implemented. Could it be that I am using render excessively?
document.addEventListener( 'mousedown', clickMe, false );
render();
function clickMe() {
rotation();
render();
}
var gltfModel;
function rotation() {
var rotationAnimation = 5 * (Math.PI / 180);
gltfModel.rotation.x += rotationAnimation;
render();
}
function render() {
renderer.render( scene, camera );
}
If I insert 'requestAnimationFrame(rotation);' within the rotation function:
function rotation() {
requestAnimationFrame(rotation);
var rotationAnimation = 5 * (Math.PI / 180);
gltfModel.rotation.x += rotationAnimation;
render();
}
The gltfModel continues to spin endlessly, and every time I click, the speed doubles.
Here is the complete code:
// Complete code provided here
Is this achievable with EventDispatcher? If so, what would be the approach? https://threejs.org/docs/#api/en/core/EventDispatcher
However, my preference leans towards the initial method discussed.