Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to get mouse coordinates without mouse moving #10584

Open
oleedd opened this issue Aug 28, 2024 · 14 comments
Open

A way to get mouse coordinates without mouse moving #10584

oleedd opened this issue Aug 28, 2024 · 14 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest

Comments

@oleedd
Copy link

oleedd commented Aug 28, 2024

What problem are you trying to solve?

Getting mouse coordinates at some concrete moment.

What solutions exist today?

No response

How would you solve it?

For example, something like navigator.mouse.clientX, navigator.mouse.clientY.

Anything else?

No response

@oleedd oleedd added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Aug 28, 2024
@nektro
Copy link

nektro commented Aug 28, 2024

could you elaborate more on the intended use case?

@oleedd
Copy link
Author

oleedd commented Sep 5, 2024

No mouse moving. The task is to determine over which element the cursor is. It is impossible.

@nektro
Copy link

nektro commented Sep 5, 2024

that doesn't explain the why

@oleedd
Copy link
Author

oleedd commented Sep 5, 2024

Because your code can't wait for some event. You need the coordinates right away.
And because setting an event listener and removing it just to get coordinates is a bit stupid.

@pshaughn
Copy link
Contributor

pshaughn commented Sep 7, 2024

It's possible for a page to lose focus and then regain focus with the mouse in a different position, without receiving any mouse event about the position change. For example, in Firefox on Cinnamon, pressing the Win/Super key, moving the mouse, and pressing Escape will do this.

I can think of a cosmetic use case for detecting such a position change: if the page is using Javascript to animate a cursor, the animation won't match the real cursor position until a mouse event comes in.

However, this use case could be supported by just guaranteeing mousemove events in more situations, rather than adding a whole different API. I can't think of any actual use case for a separate API.

@oleedd
Copy link
Author

oleedd commented Sep 7, 2024

navigator.mouse.clientX and navigator.mouse.clientY should use getters to get the coordinates at some moment (very easy in c++ without any mouse movements). So it is not about much frequent updates than mousemove.
And it is very good for the use case you described.
Adding an event listener and removing it just to get coordinates once is a really bad way, very bulky. And bad performance. But only this way is available. And it requires waiting for an event.

@pshaughn
Copy link
Contributor

pshaughn commented Sep 7, 2024

You'll have to explain your actual use case if you want anyone to follow along. Why would you simultaneously:
(1) only want the coordinates just once,
(2) in code that's being triggered by an event that isn't a MouseEvent, and
(3) in a context where the performance cost of an event handler is meaningful?

@oleedd
Copy link
Author

oleedd commented Sep 7, 2024

To use elementFromPoint() once.
For example, triggered by a keyboard event. No mouse moving.
A good developer has to always think about performance because it consists of grains. But the bulkiness is more annoying.

@BlobTheKat
Copy link

BlobTheKat commented Sep 14, 2024

Many low level libraries will expose mouse cursor by events. The issue here is not that the cursor position isn't accessible from anywhere but that no mouse event occurs when the tab is refocused. I believe I have seen it mentioned elsewhere that not being able to see the mouse position before the user has hovered over the tab is intentional and will not be addressed.

Rather than set up and event listener to delete it again, keep one event listener on and use it to update a variable that you can read at any time.

@oleedd
Copy link
Author

oleedd commented Sep 16, 2024

Yes, events are generated only when the cursor is inside the page. Because we use window.addEventListener or document.addEventListener and then the cursor is outside document and window. So it is logical. And navigator.mouse.clientX, navigator.mouse.clientY may work in such cases. But I don't see why getting coordinates may be needed when it is outside.

Rather than set up and event listener to delete it again, keep one event listener on and use it to update a variable that you can read at any time.

It is very bad. To handle events a whole day to use it several times. These events consume a lot of performance. 1 more reason to add this.

@pshaughn
Copy link
Contributor

Have you measured this performance change?

@oleedd
Copy link
Author

oleedd commented Sep 16, 2024

Yes, the cursor lags a bit.

@pshaughn
Copy link
Contributor

From my own experiences, I believe it far more likely that whatever lag you're observing is a symptom of some performance bug in your code, or some misunderstanding of how to use events, not because of the performance cost of one simple mousemove handler.

Implementers (Apple, Google, Mozilla) rarely add new API just for performance reasons, and certainly not without data. If there actually is an issue here (which I'll admit I continue to doubt), you'll need to show a reproducible, measurable demonstration of the problem, not an anecdote.

@oleedd
Copy link
Author

oleedd commented Sep 16, 2024

I came here to suggest a new useful feature, not to get a solution (because there is nothing about it that I don't know).
First of all, it is to make syntax more clear and short. And to not adjust the code to event handling functions.
Plus handling all mousemove events to get the coordinates once is a terrible things in terms of programming. But only this may be used for now to get the coordinates at a keyboard event.
Performance is a secondary thing. Go to stackoverflow, they even take into account the performance of obtaining the modulus of a number. But here we have creating a huge object with a lot of info several times per second.

But I know that you (WHATWG) will add things which will be used by 1% of programmers (some very specific things) instead of adding general useful things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest
Development

No branches or pull requests

4 participants