Skip to content

Commit 02679e1

Browse files
committed
fix: Better reset zoom controls
1 parent ac99d43 commit 02679e1

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

web/composables/usePanZoom.ts

+26-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
import type { PanZoom } from 'panzoom';
1+
import type { PanZoom, Transform } from 'panzoom';
22
import panzoom from 'panzoom';
33

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

8-
whenever(container, (container) => {
9-
instance.value = panzoom(container, {
10-
autocenter: true,
11-
minZoom: 0.2,
12-
maxZoom: 10,
13-
});
14-
scale.value = instance.value.getTransform().scale;
15-
instance.value.on('zoom', () => {
16-
scale.value = instance.value?.getTransform().scale;
17-
});
18-
});
9+
whenever(
10+
container,
11+
(container) => {
12+
if (instance.value != null) return;
13+
instance.value = panzoom(container, {
14+
autocenter: true,
15+
minZoom: 0.2,
16+
maxZoom: 10,
17+
});
18+
initialTransform = { ...instance.value.getTransform() };
19+
scale.value = initialTransform.scale;
20+
instance.value.on('zoom', () => {
21+
scale.value = instance.value?.getTransform().scale;
22+
});
23+
},
24+
{},
25+
);
1926
whenever(
2027
() => !container.value,
2128
() => {
@@ -44,8 +51,13 @@ export default function (container: Ref<HTMLElement | undefined>) {
4451
zoomIn: () => zoomBy(0.1),
4552
zoomOut: () => zoomBy(-0.1),
4653
resetZoom: () => {
47-
setZoom(() => 1);
48-
instance.value?.smoothMoveTo(0, 0);
54+
if (initialTransform == null) return;
55+
56+
const { scale, x, y } = initialTransform;
57+
setZoom(() => scale, x, y);
58+
setTimeout(() => {
59+
instance.value?.smoothMoveTo(x, y);
60+
}, 200);
4961
},
5062
};
5163
}

0 commit comments

Comments
 (0)