An issue arises when trying to use the variables mouseX
and mouseY
in the render()
function without initializing them first. Since these variables are tied to mouse movement, they remain undefined until the cursor actually enters the screen.
function render() {
....
mesh4.rotation.x = mouseY; //undefined here
mesh4.rotation.y = mouseX; //undefined here
...
}
The values for mouseX and mouseY are only set within the onDocumentMouseMove()
function:
function onDocumentMouseMove( event ) {
var windowHalfX = window.innerWidth/2;
var windowHalfY = window.innerHeight;
mouseX = ( event.clientX - windowHalfX ) / 11000; // <-- gets a value here
mouseY = ( event.clientY - windowHalfY ) / 11000; // <-- and here
}
To fix this issue, it is recommended to initialize both variables to zero in the init()
function so that they are not undefined during rendering:
function init() {
mouseX = 0;
mouseY = 0;
...
}
You can view the code example on jsfiddle: https://jsfiddle.net/L6c8dhxd/3/
I hope this explanation clarifies the problem.