I am making a geometrical optics demo in HTML5 canvas, the mouse points the light source, you can craft some barriers, javascript do the calculation work and display light and shadow on on a line behind those barriers.
from those canvas drag'n'drop tutorials I found online, all of them are using setInterval()
to make mouse interactions happen, which means the whole canvas is updated and re-painted all the time. This makes me feel the whole canvas business is an ugly hack.
My question is: are there any other way to make mouse interactions in canvas
without the setInterval()
way? I want to paint the background once and only redraw the moving part while onmousemove
when the mouse is actually moving.
Any idea is appreciated. Thanks :)
You can redraw the canvas without the element being dragged, then draw the element to a new canas on top of the base. Then you can use absolute positioning to move the one being dragged without any redrawing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With