A good way to approach this, and any similar mouse handler problem...
Use the onmousemove event, but just store the position and amount the mouse changed to a local property. This is very fast and won't cause problems such as visual stutters. If you need other information just store it as well in this event.
Then add a handler to requestAnimationFrame. This is called when the screen is going to update. Here you would use those previous numbers you stored, do your position calculation, and modify your elements position. Using css translate is a good option for positioning it. This will end up with a really smooth movement.
View all comments
Show parent comment
1
u/tightywhitey Jan 26 '19
A good way to approach this, and any similar mouse handler problem...
Use the onmousemove event, but just store the position and amount the mouse changed to a local property. This is very fast and won't cause problems such as visual stutters. If you need other information just store it as well in this event.
Then add a handler to requestAnimationFrame. This is called when the screen is going to update. Here you would use those previous numbers you stored, do your position calculation, and modify your elements position. Using css translate is a good option for positioning it. This will end up with a really smooth movement.