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 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
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,51 @@ To pan in a smooth way use `smoothMoveTo(<number>,<number>)`:
panzoom.smoothMoveTo(0, 0);
```

To pan and zoom to a specific in dom/svg coordinate use:

``` js
rect = {
top: 10,
left: 20,
bottom: 30,
right: 40,
};

panzoom.showRectangle(rect);
```

to do this in a smooth fashion do

``` js
panzoom.smoothShowRectangle(rect);
```
you can also give a function that gets the current rect and the target rect to determine the duration of the animation, like:

``` js
panzoom.smoothShowRectangle(rect, (from, to) => {
var distance = Math.sqrt(
Math.pow(from.top - to.top, 2)
+ Math.pow(from.right - to.right, 2)
+ Math.pow(from.bottom - to.bottom, 2)
+ Math.pow(from.left - to.left, 2)
);

var exp_diff = Math.exp(distance / 1000);
var sigmoid = (exp_diff * 1000) / (exp_diff + 1);

return sigmoid;
})
```

Further more: the `smoothShowRectangle` return a promise, so that the caller can act on the completion of the animation.

``` js
panzoom.smoothShowRectangle(rect)
.then(() => {
console.log("animation complete");
});
```

# license

MIT
9 changes: 5 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,23 @@ 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

pause: () => void;
resume: () => void;
isPaused: () => boolean;
Expand Down
Loading