Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add helper functions moveToCenterOfElement and moveToCenterOfBounds #83

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ declare module "panzoom" {
dispose: () => void;
moveBy: (dx: number, dy: number, smooth: boolean) => void;
moveTo: (x: number, y: number) => void;
moveToCenterOfElement: (element: Element, xOffset: number = 0, yOffset: number = 0) => void;
moveToCenterOfBounds: (bounds: DOMRect, xOffset: number = 0, yOffset: number = 0) => void;
centerOn: (ui: any) => void;
zoomTo: (clientX: number, clientY: number, scaleMultiplier: number) => void;
zoomAbs: (clientX: number, clientY: number, zoomLevel: number) => void;
Expand Down
41 changes: 41 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ function createPanZoom(domElement, options) {
moveTo: moveTo,
centerOn: centerOn,
zoomTo: publicZoomTo,
moveToCenterOfElement: moveToCenterOfElement,
moveToCenterOfBounds: moveToCenterOfBounds,
zoomAbs: zoomAbs,
smoothZoom: smoothZoom,
getTransform: getTransformModel,
Expand Down Expand Up @@ -741,6 +743,45 @@ function createPanZoom(domElement, options) {
return zoomByRatio(clientX, clientY, scaleMultiplier)
}


/**
* Calculate the center of a given bounding rectangle's position from our container viewpoint
* @param {DOMRect} elemBounds
*/
function getCenterOfBounds(elemBounds) {
const containerBounds = owner.getBoundingClientRect()

const centerX = -elemBounds.left + (((containerBounds.width / 2) - (elemBounds.width / 2)))
const centerY = -elemBounds.top + (((containerBounds.height / 2) - (elemBounds.height / 2)) + containerBounds.top)
Comment on lines +754 to +755
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this math can you please help?

😊


const newX = transform.x + centerX
const newY = transform.y + centerY

return { x: newX, y: newY }
}

/**
* Moves the view to the center of element
* @param {Element} element get the center of this HTML element
* @param {Number} xOffset offset x pixels from center horizontally
* @param {Number} yOffset offset y pixels from center vertically
*/
function moveToCenterOfElement(element, xOffset = 0, yOffset = 0) {
const bounds = element.getBoundingClientRect()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is element part of the nested tree?

Note: panzoom works not only with dom elements, but also with canvas/svg/webgl - having element passed directly like this would likely make the API less generic. Ideal solution would delegate this logic into domController or svgController

Also note: panzoom uses older version of ES language. Having consts, destructuring, default values would break the minifier

moveToCenterOfBounds(bounds, xOffset, yOffset)
}

/**
* Moves the view to the center of the bounding rectangle
* @param {DOMRect} bounds
* @param {Number} xOffset offset x pixels from center horizontally
* @param {Number} yOffset offset y pixels from center vertically
*/
function moveToCenterOfBounds(bounds, xOffset = 0, yOffset = 0) {
const { x, y } = getCenterOfBounds(bounds)
moveTo(x + xOffset, y + yOffset)
}

function cancelZoomAnimation() {
if (zoomToAnimation) {
zoomToAnimation.cancel()
Expand Down