Skip to content

fix: use clientX/Y for mouse coordinates#696

Merged
mattgodbolt merged 3 commits into
mattgodbolt:mainfrom
kimjune01:fix-mouse-coordinate-calculation
May 13, 2026
Merged

fix: use clientX/Y for mouse coordinates#696
mattgodbolt merged 3 commits into
mattgodbolt:mainfrom
kimjune01:fix-mouse-coordinate-calculation

Conversation

@kimjune01
Copy link
Copy Markdown
Contributor

Fixes #631.

Mouse coordinates were calculated incorrectly when the canvas was inset within the monitor image (e.g., with display filters that add non-zero canvasLeft/canvasTop). The previous approach mixed evt.offsetX/offsetY with bounding rect positions, which failed in these scenarios.

Switched to calculating mouse coordinates using evt.clientX/clientY against the screen canvas rect directly. This correctly handles all display filter scenarios and eliminates the incorrect mixing of coordinate systems.

Includes a regression test that reproduces the original issue.

kimjune01 added 2 commits May 11, 2026 16:48
Calculate mouse coordinates using evt.clientX/clientY against the
screen canvas rect directly, rather than mixing evt.offsetX/offsetY
with bounding rect positions.

The previous approach failed when the canvas was inset within the
monitor image (e.g., with display filters that add non-zero
canvasLeft/canvasTop). The new calculation correctly handles all
display filter scenarios.

Fixes mattgodbolt#631
Copy link
Copy Markdown
Owner

@mattgodbolt mattgodbolt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! And for the lovely tests - just a couple tweaks, I don't think we should test the old behaviour though I appreciate you demonstrating it was broken in test form; and I think we should test the code we execute and not a copy paste of it. Thnk you!

Comment thread tests/unit/test-mouse-coordinates.js Outdated
Comment thread tests/unit/test-mouse-coordinates.js Outdated
Extract calculateMouseCoordinates into a shared module so tests
exercise the actual production code. Remove old-behavior tests.
@kimjune01
Copy link
Copy Markdown
Contributor Author

OK removed old tests && extracted shared module.

Copy link
Copy Markdown
Owner

@mattgodbolt mattgodbolt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mattgodbolt mattgodbolt merged commit 8e87f9f into mattgodbolt:main May 13, 2026
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

Successfully merging this pull request may close these issues.

fix: CRT mouse coordinate calculation may be incorrect with display filters

2 participants