MouseEvent: High precision mouse position when the page is zoomed in?
Asked Answered
H

0

9

If I zoom in a page as far as it will go and move the mouse very slowly (a pixel at a time) across the page, the generated mousemove events' clientX/clientY values have no fractional part, and therefore lose precision. (Often, several mousemove events with the exact same co-ordinate values get fired in a row, because the mouse did move, but moved less than one whole CSS-pixel.) This is true for me on all browsers I have for Windows 10 -- Chrome, Firefox, Opera and Edge.

MDN's "Browser Compatibility" section states that clientX was changed from long to double in Chrome 56. So, this change seems to have been recently reverted, in Chrome's case at least.

The screenX/screenY co-ordinate values seem to be in device pixels, rather than CSS pixels, and therefore invariant to zooming. This was true for Firefox, Chrome, Edge and Opera when I tried. This seems promising, but without knowing the edges of the screen rectangle in device pixels also it may not be much use to me. (It also looks like future draft specs expect screenX/Y to be in CSS pixels anyway.)

So: is there any way I'm not seeing to get the mouse position without loss of precision at high zoom levels?

Heavily answered 30/10, 2017 at 15:40 Comment(5)
How precise do you need it to be and why? I guess I don't understand the rational behind getting anything less than 1 device pixel.Rodrique
I do not need less than 1 device pixel, but I do need exactly 1 device pixel. Currently I am only getting a resolution of 1 CSS pixel with clientX/Y, pageX/Y etc. On the other hand, screenX/Y is in device pixels which would be great except I cannot see how to contextualize these values properly, since the offset of the page area from the top-left corner of the screen does not seem to be available in device pixels?Heavily
I'm pretty disappointed nobody seems to care about this problem. I wanted to make a pixel perfect image editor on a client with a devicePixelRatio > 1.Hobo
I've just stumbled into the practical impact of this issue github.com/mui-org/material-ui/issues/22827Cockade
Still having the same issue today ...Englebert

© 2022 - 2024 — McMap. All rights reserved.