Im using the formula to calculate the distance between two points in order to determine how much to rotate an image when I move my mouse over it.
- click, I capture the point where I clicked
- mouse moves dragging, I record the location of the point where my mouse has moved to
- I calculate the distance between origin and end
- I rotate the image using this value which starts at 1.
So lets say I click on the center of the image, I start dragging my mouse to the right, my image rotates perfectly, then without letting go the click i drag back the mouse on my image, my image is now counter rotating, which is perfect. Now once the mouse reaches that center(where I clicked initially) it forces my image rotate in the oposite directions because the distance reached its limit(0) and the new values are all positive even though im moving to the left.
I understand that the distance between two points is supposed to be positive but I need something to make negative once it comes back to (0) so that I can rotate my image properly.
http://jsfiddle.net/wRexz/4/
This will find the relative angle of rotation between two points. It updates each time the
mousemoveevent is fired, so it will never be more than 180 degrees in one go (except when dragging around the midpoint).Math.atan2simplifies the logic compared toMath.atanas the latter isn’t meant for a coordinate system.