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

Conversation

Illyism
Copy link

@Illyism Illyism commented Apr 2, 2019

const DOMElemRef = document.querySelector('.some-element')
moveToCenterOfElement(DOMElemRef) // moves viewport to the center of this element

const elemBounds = document.querySelector('.some-element').getBoundingClientRect()
moveToCenterOfBounds(elemBounds) // moves viewport to the center of this bounding box

moveToCenterOfBounds is useful to have separately so you can pass just the bounding box around. Or make your own one without having to use HTML, eg moveToCenterOfBounds({ left: 0, top: 0, width: 100, height: 100 })

@Illyism Illyism changed the title moveToCenterOfElement, moveToCenterOfBounds add helper functions moveToCenterOfElement and moveToCenterOfBounds Apr 3, 2019
@savtwo
Copy link

savtwo commented Aug 31, 2020

@anvaka This is very helpful. Any chance it can be merged?

Comment on lines +754 to +755
const centerX = -elemBounds.left + (((containerBounds.width / 2) - (elemBounds.width / 2)))
const centerY = -elemBounds.top + (((containerBounds.height / 2) - (elemBounds.height / 2)) + containerBounds.top)
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?

😊

* @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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants