|
1 |
| -import type { PanZoom } from 'panzoom'; |
| 1 | +import type { PanZoom, Transform } from 'panzoom'; |
2 | 2 | import panzoom from 'panzoom';
|
3 | 3 |
|
4 | 4 | export default function (container: Ref<HTMLElement | undefined>) {
|
5 | 5 | let instance = ref<PanZoom>();
|
6 | 6 | const scale = ref<number>();
|
| 7 | + let initialTransform: Transform | undefined; |
7 | 8 |
|
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 | + ); |
19 | 26 | whenever(
|
20 | 27 | () => !container.value,
|
21 | 28 | () => {
|
@@ -44,8 +51,13 @@ export default function (container: Ref<HTMLElement | undefined>) {
|
44 | 51 | zoomIn: () => zoomBy(0.1),
|
45 | 52 | zoomOut: () => zoomBy(-0.1),
|
46 | 53 | 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); |
49 | 61 | },
|
50 | 62 | };
|
51 | 63 | }
|
0 commit comments