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

Added a smoothShowRectangle and Promises to all smooth functions #221

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Added promises as return values to the other smooth functions
  • Loading branch information
Timo-Weike committed Nov 29, 2020
commit 692e5f2766ab0f2726ad7b92f751b63024decc44
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ declare module "panzoom" {
dispose: () => void;
moveBy: (dx: number, dy: number, smooth: boolean) => void;
moveTo: (x: number, y: number) => void;
smoothMoveTo: (x: number, y: number) => void;
centerOn: (ui: any) => void;
smoothMoveTo: (x: number, y: number) => Promise<boolean>;
centerOn: (ui: any) => Promise<boolean>;
zoomTo: (clientX: number, clientY: number, scaleMultiplier: number) => void;
zoomAbs: (clientX: number, clientY: number, zoomLevel: number) => void;
smoothZoom: (
clientX: number,
clientY: number,
scaleMultiplier: number
) => void;
) => Promise<boolean>;
smoothZoomAbs: (
clientX: number,
clientY: number,
toScaleValue: number
) => void;
) => Promise<boolean>;
getTransform: () => Transform;
showRectangle: (rect: ClientRect) => void;
smoothShowRectangle: (rect: ClientRect, duration: (from:ClientRect, to:ClientRect) => number) => Promise<boolean>;
Copy link

@stuffaboutpete stuffaboutpete May 22, 2024

Choose a reason for hiding this comment

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

Duration should be declared as optional

Expand Down
92 changes: 61 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,33 +512,46 @@ function createPanZoom(domElement, options) {
var dx = container.width / 2 - cx;
var dy = container.height / 2 - cy;

internalMoveBy(dx, dy, true);
return internalMoveBy(dx, dy, true);
}

function smoothMoveTo(x, y){
internalMoveBy(x - transform.x, y - transform.y, true);
return internalMoveBy(x - transform.x, y - transform.y, true);
}

function internalMoveBy(dx, dy, smooth) {
if (!smooth) {
return moveBy(dx, dy);
}

cancelAllAnimations();

var from = { x: 0, y: 0 };
var to = { x: dx, y: dy };
var lastX = 0;
var lastY = 0;

moveByAnimation = animate(from, to, {
step: function (v) {
moveBy(v.x - lastX, v.y - lastY);

lastX = v.x;
lastY = v.y;
var p = new Promise((resolve, _) => {
cancelAllAnimations();

if (!smooth) {
moveBy(dx, dy);

triggerZoomEnd();
triggerPanEnd();
resolve(true);
} else {
var from = { x: 0, y: 0 };
var to = { x: dx, y: dy };
var lastX = 0;
var lastY = 0;

moveByAnimation = animate(from, to, {
step: function (v) {
moveBy(v.x - lastX, v.y - lastY);

lastX = v.x;
lastY = v.y;
},
done: () => {
triggerZoomEnd();
triggerPanEnd();
resolve(true);
}
});
}
});
})

return p;
}

function scroll(x, y) {
Expand Down Expand Up @@ -911,12 +924,20 @@ function createPanZoom(domElement, options) {

cancelAllAnimations();

zoomToAnimation = animate(from, to, {
step: function (v) {
zoomAbs(clientX, clientY, v.scale);
},
done: triggerZoomEnd
});
var p = new Promise((resolve, _) => {
zoomToAnimation = animate(from, to, {
step: function (v) {
zoomAbs(clientX, clientY, v.scale);
},
done: () => {
triggerZoomEnd();
triggerPanEnd();
resolve(true);
}
});
})

return p;
}

function smoothZoomAbs(clientX, clientY, toScaleValue) {
Expand All @@ -926,11 +947,20 @@ function createPanZoom(domElement, options) {

cancelAllAnimations();

zoomToAnimation = animate(from, to, {
step: function (v) {
zoomAbs(clientX, clientY, v.scale);
}
});
var p = new Promise((resolve, _) => {
zoomToAnimation = animate(from, to, {
step: function (v) {
zoomAbs(clientX, clientY, v.scale);
},
done: () => {
triggerZoomEnd();
triggerPanEnd();
resolve(true);
}
});
})

return p;
}

function getTransformOriginOffset() {
Expand Down