This commit is contained in:
Timothy Jaeryang Baek
2026-08-13 15:33:42 -06:00
parent 2befa8f796
commit bfb68feea7
4 changed files with 184 additions and 79 deletions
@@ -10,18 +10,45 @@
export let className = '';
export let options: Partial<PanZoomOptions> = {};
export let zoomLevel = 1;
let containerElement: HTMLElement;
let instance: PanZoom | undefined;
const updateZoomLevel = () => {
zoomLevel = instance?.getTransform().scale ?? 1;
};
const center = () => ({
x: containerElement?.clientWidth ? containerElement.clientWidth / 2 : 0,
y: containerElement?.clientHeight ? containerElement.clientHeight / 2 : 0
});
export const zoomIn = () => {
if (!instance) return;
const { x, y } = center();
instance.zoomTo(x, y, 1.25);
updateZoomLevel();
};
export const zoomOut = () => {
if (!instance) return;
const { x, y } = center();
instance.zoomTo(x, y, 0.8);
updateZoomLevel();
};
export const reset = () => {
instance?.moveTo(0, 0);
instance?.zoomAbs(0, 0, 1);
updateZoomLevel();
};
onMount(() => {
const localInstance = panzoom(containerElement, { ...defaultOpts, ...options });
instance = localInstance;
updateZoomLevel();
localInstance.on('zoom', updateZoomLevel);
return () => {
localInstance.dispose();
};