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

Multiplayer cursor position inside canvas #70

Open
alquist42 opened this issue Jan 2, 2024 · 1 comment
Open

Multiplayer cursor position inside canvas #70

alquist42 opened this issue Jan 2, 2024 · 1 comment

Comments

@alquist42
Copy link

Hello again :)

I'm trying to play with multiplayer presence technology and to show the other participant cursor position inside invinite viewer.

infinite-viewer-cursors

But as you can see the cursors have window position and not adjusted to scrolled area and not to zoom of the invinite viewer.
For example, when one cursor on "div 5" second on "div 4"
I've tried couple of things of adjusting relativly to container, but no luck yet...

Can you please advise of how to adjust the coordinate to reflect the real world inside the canvas?


Playground: codesandbox
(hot reload breaks sometime, so need to refresh manualy, you can add second preview tab)


A huge thanks anyway!

@kevinvalk
Copy link

kevinvalk commented Jan 2, 2024

Took me way to long as well, but this should give you a stable mouse position that is absolute regardless zoom or scroll.

viewport.addEventListener('mousemove', (e) => {
  const rect = viewport.getBoundingClientRect();
  mouse.x = (e.clientX - rect.left) / infiniteViewer.zoomX;
  mouse.y = (e.clientY - rect.top) / infiniteViewer.zoomY;
});

NOTE: This is not it either as it "stops" tracking when you are outside the viewport element...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants