7

I've been developing an HTML5 application and now I'm facing quite a difficult problem getting a proper local touch / mouse position inside a target div in a proper coordinate system.

There are many solutions on StackOverflow but the thing is not so easy.

The "classical" solution is to compute a target offset and subtract it from pageX and pageY position. Many people use JQuery offset function but we have found it buggy (1.8.3) on iOS in some situations when the page is scrolled and contains nested zoomed divs.

The situation is event more complicated because there are not only nested zoomed divs (CSS zoom property) but also layers with different CSS transformations (x and y translations).

Finally we have written our own function to compute the local mouse / touch position in a target div (using WebKitCSSMatrix, MSCSSMatrix and others...). It works 'almost' well but still not perfect is some situations - especially when there are nested zoomed divs.

So my question is whether there is some general solution / library to compute a local touch / mouse position inside a target div considering the above described requirements??

Thanks.

Nice day to all...

Pavel Stupka
  • 213
  • 2
  • 14
  • Is there a particular mouse/touch event you are interested in? pageX and pageY are measured in CSS pixels, so they should not be affected by zooming, CSS pixels are constant through out zooming, if things are working correctly. – nativist.bill.cutting Jul 30 '13 at 15:25

2 Answers2

0

On a mouse event you will have 5 property pairs to choose from.

Here is an excellent browser quirks reference that explains the difference:

Here are the 3 they cover in detail

  • pageX/Y gives the coordinates relative to the element in CSS pixels.
  • clientX/Y gives the coordinates relative to the viewport in CSS pixels.
  • screenX/Y gives the coordinates relative to the screen in device pixels.
nativist.bill.cutting
  • 1,292
  • 3
  • 11
  • 19
-1

If you use event.offsetX and event.offsetY, that should do it. (sorry i know its old)

carinlynchin
  • 389
  • 1
  • 3
  • 13