diff --git a/index.d.ts b/index.d.ts index 2534d65..c7a1766 100644 --- a/index.d.ts +++ b/index.d.ts @@ -29,6 +29,7 @@ declare module "panzoom" { moveTo: (x: number, y: number) => void; centerOn: (ui: any) => void; zoomTo: (clientX: number, clientY: number, scaleMultiplier: number) => void; + zoomToCenter: (scaleMultiplier: number) => void; zoomAbs: (clientX: number, clientY: number, zoomLevel: number) => void; smoothZoom: ( clientX: number, diff --git a/index.js b/index.js index 87d711e..54e6b99 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,7 @@ function createPanZoom(domElement, options) { moveTo: moveTo, centerOn: centerOn, zoomTo: publicZoomTo, + zoomToCenter: zoomToCenter, zoomAbs: zoomAbs, smoothZoom: smoothZoom, getTransform: getTransformModel, @@ -741,6 +742,17 @@ function createPanZoom(domElement, options) { return zoomByRatio(clientX, clientY, scaleMultiplier) } + /** + * Zooms to the center of the container + * @param {Number} scaleMultiplier 0.8 = zoom out by 20% and 1.2 = zoom in by 20% + */ + function zoomToCenter(scaleMultiplier) { + const containerRect = owner.getBoundingClientRect() + const centerX = containerRect.width / 2 + const centerY = containerRect.height / 2 + return zoomByRatio(centerX, centerY, scaleMultiplier) + } + function cancelZoomAnimation() { if (zoomToAnimation) { zoomToAnimation.cancel()