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

Mousemove fires outside canvas #12473

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
- [Nick Crews](https://github.com/NickCrews)
- [胡文康](https://github.com/XiaoHu1994)
- [Parth Petkar](https://github.com/parthpetkar)
- [Riley Howley](https://github.com/Riley-Howley)
12 changes: 12 additions & 0 deletions packages/engine/Source/Core/ScreenSpaceEventHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ function handleMouseMove(screenSpaceEventHandler, event) {
return;
}

const canvas = screenSpaceEventHandler._element;
const rect = canvas.getBoundingClientRect();

if (
event.clientX < rect.left ||
event.clientX > rect.right ||
event.clientY < rect.top ||
event.clientY > rect.bottom
Comment on lines +359 to +362
Copy link
Contributor

Choose a reason for hiding this comment

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

This type of check technically works but isn't the behavior I'd actually expect. This will still trigger when hovering over any elements that overlap with the viewer which is common in our sandcastles and other viewer contexts. Even things like the builtin timeline and geocoder would still trigger with this check because it's purely based on the bounds.

I think a more accurate check is whether event.target !== screenSpaceHandler._element which more directly matches the behavior of the event being attached directly to the canvas

Check out this sandcastle with an "overlay element" that I would not expect the mouse move handler to fire over.

) {
return;
}

const modifier = getModifier(event);

const position = getPosition(
Expand Down
57 changes: 57 additions & 0 deletions packages/engine/Specs/Core/ScreenSpaceEventHandlerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ describe("Core/ScreenSpaceEventHandler", function () {
const action = createCloningSpy("action");
handler.setInputAction(action, eventType, modifier);

spyOn(element, "getBoundingClientRect").and.returnValue({
left: 0,
right: 100,
top: 0,
bottom: 100,
});

expect(handler.getInputAction(eventType, modifier)).toEqual(action);

function simulateInput() {
Expand Down Expand Up @@ -601,6 +608,56 @@ describe("Core/ScreenSpaceEventHandler", function () {
);
});

describe("handles out of bounds mouse move", function () {
function testOutOfBoundsMouseMoveEvent(eventType, modifier, eventOptions) {
const action = createCloningSpy("action");
handler.setInputAction(action, eventType, modifier);

expect(handler.getInputAction(eventType, modifier)).toEqual(action);

function simulateInput() {
simulateMouseMove(
element,
combine(
{
clientX: 1,
clientY: 2,
},
eventOptions,
),
);
}

simulateInput();

expect(action.calls.count()).toEqual(0);

// should not be fired after removal
action.calls.reset();

handler.removeInputAction(eventType, modifier);

simulateInput();

expect(action).not.toHaveBeenCalled();
}

const possibleButtons = [undefined];
const possibleModifiers = [
undefined,
KeyboardEventModifier.SHIFT,
KeyboardEventModifier.CTRL,
KeyboardEventModifier.ALT,
];
const possibleEventTypes = [ScreenSpaceEventType.MOUSE_MOVE];
createAllMouseSpecCombinations(
testOutOfBoundsMouseMoveEvent,
possibleButtons,
possibleModifiers,
possibleEventTypes,
);
});

describe("handles mouse wheel", function () {
if ("onwheel" in document) {
describe("using standard wheel event", function () {
Expand Down
Loading