williamngan / pts

A library for visualization and creative-coding
https://ptsjs.org
Apache License 2.0
5.16k stars 182 forks source link

Canvas mouse events coord is wrong on layout change #200

Open ProtonGustave opened 1 year ago

ProtonGustave commented 1 year ago

You calculate local coordinates there based on evt.pageX/evt.pageY and canvas page bounds. Element page bounds variable gets updated on "resize" page event(and manual "resize" calls), but page layout may be changed all the time(some data is loaded and element above chart became bigger for example). Easiest fix is to use evt.offsetX/evt.offsetY, but this also highlight problem with "bounds" variable, better not to use it if possible since there is no performant tracker of element position.

williamngan commented 1 year ago

You're right that it's not ideal as it'll take some manual calculations for advanced layout changes. I think I encountered some issue with offsetX/offsetY (maybe it's not supported by touch event, or some other issues).

Worth revisiting and improve this one!