Skip to content

Commit

Permalink
[Editor] Avoid to use event.movementX/Y when resizing an editor
Browse files Browse the repository at this point in the history
Those propertie can have some non-expected values so use screenX/Y instead.
  • Loading branch information
calixteman committed Nov 21, 2024
1 parent 5b600e8 commit 1012302
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class AnnotationEditor {

#resizersDiv = null;

#lastPointerCoords = null;

#savedDimensions = null;

#focusAC = null;
Expand Down Expand Up @@ -736,6 +738,7 @@ class AnnotationEditor {

const savedDraggable = this._isDraggable;
this._isDraggable = false;
this.#lastPointerCoords = [event.screenX, event.screenY];

const ac = new AbortController();
const signal = this._uiManager.combinedSignal(ac);
Expand All @@ -746,6 +749,14 @@ class AnnotationEditor {
this.#resizerPointermove.bind(this, name),
{ passive: true, capture: true, signal }
);
window.addEventListener(
"touchmove",
e => {
// Prevent the page from scrolling.
e.preventDefault();
},
{ passive: false, signal }
);
window.addEventListener("contextmenu", noContextMenu, { signal });
const savedX = this.x;
const savedY = this.y;
Expand Down Expand Up @@ -886,10 +897,17 @@ class AnnotationEditor {
let ratioX = 1;
let ratioY = 1;

// We can't use event.movementX/Y because they're not reliable:
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementX
// (it was buggy on a laptop with a touch screen).
const { screenX, screenY } = event;
const [lastScreenX, lastScreenY] = this.#lastPointerCoords;
let [deltaX, deltaY] = this.screenToPageTranslation(
event.movementX,
event.movementY
screenX - lastScreenX,
screenY - lastScreenY
);
this.#lastPointerCoords[0] = screenX;
this.#lastPointerCoords[1] = screenY;
[deltaX, deltaY] = invTransf(deltaX / parentWidth, deltaY / parentHeight);

if (isDiagonal) {
Expand Down

0 comments on commit 1012302

Please sign in to comment.