Skip to content

Commit

Permalink
fix: Better reset zoom controls
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 committed Apr 5, 2024
1 parent ac99d43 commit 02679e1
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions web/composables/usePanZoom.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import type { PanZoom } from 'panzoom';
import type { PanZoom, Transform } from 'panzoom';
import panzoom from 'panzoom';

export default function (container: Ref<HTMLElement | undefined>) {
let instance = ref<PanZoom>();
const scale = ref<number>();
let initialTransform: Transform | undefined;

whenever(container, (container) => {
instance.value = panzoom(container, {
autocenter: true,
minZoom: 0.2,
maxZoom: 10,
});
scale.value = instance.value.getTransform().scale;
instance.value.on('zoom', () => {
scale.value = instance.value?.getTransform().scale;
});
});
whenever(
container,
(container) => {
if (instance.value != null) return;
instance.value = panzoom(container, {
autocenter: true,
minZoom: 0.2,
maxZoom: 10,
});
initialTransform = { ...instance.value.getTransform() };
scale.value = initialTransform.scale;
instance.value.on('zoom', () => {
scale.value = instance.value?.getTransform().scale;
});
},
{},
);
whenever(
() => !container.value,
() => {
Expand Down Expand Up @@ -44,8 +51,13 @@ export default function (container: Ref<HTMLElement | undefined>) {
zoomIn: () => zoomBy(0.1),
zoomOut: () => zoomBy(-0.1),
resetZoom: () => {
setZoom(() => 1);
instance.value?.smoothMoveTo(0, 0);
if (initialTransform == null) return;

const { scale, x, y } = initialTransform;
setZoom(() => scale, x, y);
setTimeout(() => {
instance.value?.smoothMoveTo(x, y);
}, 200);
},
};
}

0 comments on commit 02679e1

Please sign in to comment.