-
Notifications
You must be signed in to change notification settings - Fork 294
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,8 @@ function createPanZoom(domElement, options) { | |
moveTo: moveTo, | ||
centerOn: centerOn, | ||
zoomTo: publicZoomTo, | ||
moveToCenterOfElement: moveToCenterOfElement, | ||
moveToCenterOfBounds: moveToCenterOfBounds, | ||
zoomAbs: zoomAbs, | ||
smoothZoom: smoothZoom, | ||
getTransform: getTransformModel, | ||
|
@@ -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) | ||
|
||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is 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() | ||
|
There was a problem hiding this comment.
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?
😊