Rotation with exact values not working as expected

I am very new to Three and am trying to get my head around how everything works before I start working on anything large and I ran into an issue with rotation. I want my square to rotate 45 degrees whenever the s key is pressed but whenever I do press it it seems to rotate 45, 90, 135, etc degrees. I am using this code

To try and do it.

I have tried Math.PI/4 and rough values (5dp) to see if there was any change but the problem still persisted.

I am running the code on Microsoft edge and I am coding in visual studio code if that is important

Sorry if this is an incredibly simple question.

-initial screen

-after s pressed once

-after s pressed thrice (no change on second press)

why would you add the listener in animate()

1 Like
function animate() {
    requestAnimationFrame( animate );
    //rotate on "s" key	
    document.addEventListener("keypress", function() {switch (event.key) {case "s": {redFace.rotation.x+=Math.PI/4}}});
    renderer.render( scene, camera );
}

Don’t put .addEventListener() into the animation loop.

You are a saint, thanks.

Yes, for example, like this:

//rotate on "s" key	
document.addEventListener("keypress", function() {switch (event.key) {case "s": {redFace.rotation.x+=Math.PI/4}}});

function animate() {
    requestAnimationFrame( animate );
    renderer.render( scene, camera );
}