Skip to content

Commit

Permalink
Stop sending event on pressure changes (#2762)
Browse files Browse the repository at this point in the history
## Description

When the pressure changes, the stylus sends a `pointermove` event. Because it is highly sensitive to these changes, these events were being sent constantly. This PR introduces a check to see whether the pointer has changed its position. If it hasn't, the `pointermove` events won't be sent.

## Test plan

Tested on example app (draggable example and `console.log` inside `onPointerMove` inside `Pan`)
  • Loading branch information
m-bert authored Feb 19, 2024
1 parent 4f33384 commit a3309b6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/web/tools/PointerEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,27 @@ export default class PointerEventManager extends EventManager<HTMLElement> {
}
});

const lastPosition: { x: number | null; y: number | null } = {
x: null,
y: null,
};

this.view.addEventListener('pointermove', (event: PointerEvent): void => {
if (event.pointerType === PointerType.TOUCH) {
return;
}

// Stylus triggers `pointermove` event when it detects changes in pressure. Since it is very sensitive to those changes,
// it constantly sends events, even though there was no change in position. To fix that we check whether
// pointer has actually moved and if not, we do not send event.
if (
event.pointerType === PointerType.PEN &&
event.x === lastPosition.x &&
event.y === lastPosition.y
) {
return;
}

const adaptedEvent: AdaptedEvent = this.mapEvent(event, EventTypes.MOVE);
const target = event.target as HTMLElement;

Expand Down Expand Up @@ -136,6 +152,9 @@ export default class PointerEventManager extends EventManager<HTMLElement> {
this.onPointerOutOfBounds(adaptedEvent);
}
}

lastPosition.x = event.x;
lastPosition.y = event.y;
});

this.view.addEventListener('pointercancel', (event: PointerEvent): void => {
Expand Down

0 comments on commit a3309b6

Please sign in to comment.