dy / pan-zoom

Pan / zoom for any element
MIT License
88 stars 15 forks source link

[Bug] x0, y0 not getting updated #11

Open btxtiger opened 6 years ago

btxtiger commented 6 years ago

When scrolling and not moving the cursor, and this way resizing the image, x0 any y0 remain the same values, even if they should have changed because the image size has changed. That causes a jumping when using cursor fixed zooming, because the correctionFactor dx dy is one scroll time equal to its previous value, but it has to be bigger as like the grown image dimensions. The issue does not occur, when moving the cursor while zooming.

btxtiger commented 6 years ago

My current workaround. Requires to know the scaling factor.

// Consider cursor position
let mouseX = e.x;
let mouseY = e.y;

// HACK: fix against jumping, because event not returning updated position
if (this.lastMousePosition.x === mouseX && this.lastMousePosition.y === mouseY) {
   mouseX = this.lastInterpolatedMousePosition.x * scalingFactor;
   mouseY = this.lastInterpolatedMousePosition.y * scalingFactor;
   this.lastInterpolatedMousePosition = {
      x: mouseX,
      y: mouseY
   };
} else {
   const updatedPosition = {
      x: mouseX,
      y: mouseY
   };
   this.lastMousePosition = updatedPosition;
   this.lastInterpolatedMousePosition = updatedPosition;
}