I am trying to create div that 3D rotates on the X and Y axis so that it always faces the mouse cursor.
I am using vanilla Javascript and Velocity.js for the animation library.
Here is my Javascript code so far, please see the Codepen link for the html and css too:
var el = document.querySelector("#circle");// get mouse position on x y
onmousemove = function(event) {
var x = event.clientX
var y = event.clientY
var HEIGHT = document.body.clientHeight;
var WIDTH = document.body.clientWidth;
console.log("x position: "+ x +", " + "y position: " + y);
var calcX= Math.floor(x/10)
var calcY= Math.floor(y/10)
Velocity(el, {rotateX:calcX})
Velocity(el, {rotateY:calcY})
// rotate element towards x and y co-ordinates
};
http://codepen.io/anon/pen/bpzjar
I know that the math calculation isn't correct, if someone knows what the correct code for that, that would be awesome!
But I'm mainly trying to look at why the code is so slow and it seems to calculate and go to through each degree rather than going straight to what the final calculation would be so that its fast and responsive.
If someone could help me figure it out that would be great.
thanks!