question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set mouse event offset?

See original GitHub issue

I encountered this issue with pixi-scrollbox and yy-ui. Whenever the WebGL Canvas element which contains PIXI does not start from 0,0 x/y in the browser window, the mouse events will not correctly offset. For example if I try to scroll down with the mouse wheel, then mouse events will trigger if my mouse is over 0,0, which is not where the canvas is. Say the canvas is placed at 100,100 and is 300 in width and 300 in height. Well the mouse event will trigger at 0,0 where the canvas isn’t even at, but it will not trigger at 400,400 where the canvas is, because it will be offset by it’s browser window position.

It seems to understand where the PIXI.JS container/display object is relative to the start of the browser window x/y position, but NOT where the canvas actually is, resulting in all mouse events triggering only outside of the actual canvas if the canvas does not start perfectly at 0,0 in the browser.

Why is the mouse position calculated by where the mouse at relative to the browser window and not relative to where the canvas is placed / inside the canvas, and how can I fix that / correctly offset it?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
davidfigcommented, Aug 28, 2018

Try out v3.4.0. You need to set the PIXI.interaction in the Viewport options:

new Viewport({ interaction: renderer.plugins.interaction })

where renderer = your instantiated PIXI.WebGLRenderer or PIXI.CanvasRenderer

Let me know if this helps.

0reactions
davidfigcommented, Aug 30, 2018

Closing this. I’ll update the documentation to try to call out this option more clearly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MouseEvent.offsetX - Web APIs - MDN Web Docs
The offsetX read-only property of the MouseEvent interface provides the offset in the X coordinate of the mouse pointer between that event ...
Read more >
MouseEvent offsetX Property
The offsetX property returns the x-coordinate of the mouse pointer, relative to the target element. Tip: To get the y-coordinate, use the offsetY...
Read more >
Positioning with Mouse Events, Offset, getBoundingClientRect ...
MouseEvent.offsetX / MouseEvent.offsetY ~ Coordinates of mouse event relative to the edge of the padding on the targeted node.
Read more >
HTML | DOM MouseEvent offsetX Property
The MouseEvent offsetX property is a read-only property which is used for returning the x-coordinate of the mouse pointer, relative to the ...
Read more >
offsetX property (event) JavaScript - Dottoro Web Reference
Sets or retrieves the x-coordinate of the mouse pointer relative to the top-left corner of the offsetParent element of the element that fires...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found