-
Notifications
You must be signed in to change notification settings - Fork 312
Zoom to fit #181
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
base: main
Are you sure you want to change the base?
Zoom to fit #181
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 |
|---|---|---|
|
|
@@ -122,6 +122,7 @@ function createPanZoom(domElement, options) { | |
| smoothZoom: smoothZoom, | ||
| smoothZoomAbs: smoothZoomAbs, | ||
| showRectangle: showRectangle, | ||
| zoomToFit: zoomToFit, | ||
|
|
||
| pause: pause, | ||
| resume: resume, | ||
|
|
@@ -418,43 +419,62 @@ function createPanZoom(domElement, options) { | |
| zoomByRatio(clientX, clientY, ratio); | ||
| } | ||
|
|
||
| function centerOn(ui) { | ||
| var parent = ui.ownerSVGElement; | ||
| if (!parent) | ||
| throw new Error('ui element is required to be within the scene'); | ||
|
|
||
| function centerOn(smooth) { | ||
| // TODO: should i use controller's screen CTM? | ||
| var clientRect = ui.getBoundingClientRect(); | ||
| var clientRect = owner.getBoundingClientRect(); | ||
| var cx = clientRect.left + clientRect.width / 2; | ||
| var cy = clientRect.top + clientRect.height / 2; | ||
|
|
||
| var parent = owner.parentElement.parentElement; | ||
| var container = parent.getBoundingClientRect(); | ||
| var dx = container.width / 2 - cx; | ||
| var dy = container.height / 2 - cy; | ||
|
|
||
| internalMoveBy(dx, dy, true); | ||
| internalMoveBy(dx, dy, smooth); | ||
| } | ||
|
|
||
| function internalMoveBy(dx, dy, smooth) { | ||
| if (!smooth) { | ||
| return moveBy(dx, dy); | ||
| } | ||
| function zoomToFit(smooth) { | ||
| // TODO: should i use controller's screen CTM? | ||
| var clientRect = owner.getBoundingClientRect(); | ||
| var cx = clientRect.left + clientRect.width/2; | ||
| var cy = clientRect.top + clientRect.height/2; | ||
|
|
||
| if (moveByAnimation) moveByAnimation.cancel(); | ||
| var parent = owner.parentElement.parentElement; | ||
|
Owner
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. I'm not following this. Why are we fetching Same applies to the use in the |
||
| var container = parent.getBoundingClientRect(); | ||
| var dx = container.width/2 - cx; | ||
| var dy = container.height/2 - cy; | ||
|
|
||
| var from = { x: 0, y: 0 }; | ||
| var to = { x: dx, y: dy }; | ||
| var lastX = 0; | ||
| var lastY = 0; | ||
| var wx = window.innerWidth / 2; | ||
| var wy = window.innerHeight / 2; | ||
|
Comment on lines
+447
to
+448
Owner
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. This doesn't seem right for the cases when container is smaller than the window |
||
|
|
||
| moveByAnimation = animate(from, to, { | ||
| step: function (v) { | ||
| moveBy(v.x - lastX, v.y - lastY); | ||
| var fitRatio = Math.min(wx / (clientRect.width / 2), wy / (clientRect.height / 2)); | ||
|
|
||
| lastX = v.x; | ||
| lastY = v.y; | ||
| } | ||
| }); | ||
| internalMoveBy(dx, dy, smooth, () => {smoothZoom(wx , wy, fitRatio);}); | ||
| } | ||
|
|
||
| function internalMoveBy(dx, dy, smooth, done) { | ||
| if (!smooth) { | ||
| moveBy(dx, dy); | ||
| if (done !== undefined) | ||
| done(); | ||
| } else { | ||
| if (moveByAnimation) moveByAnimation.cancel(); | ||
|
|
||
| var from = { x: 0, y: 0 }; | ||
| var to = { x: dx, y : dy }; | ||
| var lastX = 0; | ||
| var lastY = 0; | ||
|
|
||
| moveByAnimation = animate(from, to, { | ||
| step: function(v) { | ||
| moveBy(v.x - lastX, v.y - lastY); | ||
|
|
||
| lastX = v.x; | ||
| lastY = v.y; | ||
| }, | ||
| done: done | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| function scroll(x, y) { | ||
|
|
||
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.
The original idea of this method was for SVG elements, that would allow clients to center on a specific element of the scene.
As the library grew this become a bit obsolete in the context of WebGL/DOM scenes (which are also supported by the library).
May I propose to keep the API, but delegate the center inference to the controller? For example, svgController API could have a method that takes the UI element and returns
cx/cy, while domController does the same for the DOM nodes?I interpret this change as a desire to zoom into the center of the actual container, right?
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.
yes, right.