How can i set the initial zoom factor to the InfiniteCanvas? [message #1795513] |
Mon, 24 September 2018 11:13  |
Eclipse User |
|
|
|
Hello
i see i can set the scroll position with
InfiniteCanvas c = (InfiniteCanvas) viewer.getCanvas();
c.setVerticalScrollOffset(50);
c.setHorizontalScrollOffset(150);
For setting the zoom level, i tried to use setScaleX/Y but the behavior is then strange when resizing the window.
Is there an API for this?
BR
Frank
|
|
|
Re: How can i set the initial zoom factor to the InfiniteCanvas? [message #1802132 is a reply to message #1795513] |
Fri, 01 February 2019 12:27  |
Eclipse User |
|
|
|
Hi Frank,
you can use the fitToSize() method and pass-in the absolute zoom level you want to achieve.
scaleX/Y are the default JavaFX properties. Therefore, the whole canvas is scaled using these properties.
Alternatively, you can manipulate the content transform to achieve the desired result. For this, I would recommend to get inspiration from fitToSize():
// compute content center
double cx = contentBounds.getMinX() + contentBounds.getWidth() / 2;
double cy = contentBounds.getMinY() + contentBounds.getHeight() / 2;
// compute visible area center
double vx = getWidth() / 2;
double vy = getHeight() / 2;
// scroll to center position
setHorizontalScrollOffset(getHorizontalScrollOffset() + vx - cx);
setVerticalScrollOffset(getVerticalScrollOffset() + vy - cy);
// compute pivot point for zoom within content coordinates
Point2D pivot = getContentGroup().sceneToLocal(vx, vy);
// restrict zoom factor to [zoomMin, zoomMax] range
AffineTransform contentTransform = FX2Geometry
.toAffineTransform(getContentTransform());
double realZoomFactor = contentTransform.getScaleX() * zf;
if (realZoomFactor > zoomMax) {
zf = zoomMax / contentTransform.getScaleX();
}
if (realZoomFactor < zoomMin) {
zf = zoomMin / contentTransform.getScaleX();
}
// compute scale transformation (around visible center)
AffineTransform scaleTransform = new AffineTransform()
.translate(pivot.getX(), pivot.getY()).scale(zf, zf)
.translate(-pivot.getX(), -pivot.getY());
// concatenate old transformation and scale transformation to yield the
// new transformation
AffineTransform newTransform = contentTransform
.concatenate(scaleTransform);
setContentTransform(Geometry2FX.toFXAffine(newTransform));
Permalink: https://github.com/eclipse/gef/blob/5135440577a6c3381e139591b3dd88dd79c43871/org.eclipse.gef.fx/src/org/eclipse/gef/fx/nodes/InfiniteCanvas.java#L728
Best regards,
Matthias
|
|
|
Powered by
FUDForum. Page generated in 0.04000 seconds