Skip to content

add helper functions moveToCenterOfElement and moveToCenterOfBounds#83

Open
Illyism wants to merge 1 commit into
anvaka:mainfrom
officient:moveToCenterOfBounds
Open

add helper functions moveToCenterOfElement and moveToCenterOfBounds#83
Illyism wants to merge 1 commit into
anvaka:mainfrom
officient:moveToCenterOfBounds

Conversation

@Illyism
Copy link
Copy Markdown

@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
Copy Markdown

savtwo commented Aug 31, 2020

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

Comment thread index.js
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
Copy Markdown
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?

😊

Comment thread index.js
* @param {Number} yOffset offset y pixels from center vertically
*/
function moveToCenterOfElement(element, xOffset = 0, yOffset = 0) {
const bounds = element.getBoundingClientRect()
Copy link
Copy Markdown
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

@Jasonvdlee
Copy link
Copy Markdown

@anvaka I need this too.

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.

4 participants