Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Having trouble calculating scroll for viewport when zooming in

    I would like to keep the center of the viewport in the same place when I zoom in, right now the viewport stays at the same absolute top and left coordinate and the center moves.

    I am attaching my zoom in click handler showing my calculations, but the center moves way too far over when I hit the zoom button.
    Anyone have any insight into this?
    Attached Files

    #2
    Someone on another forum helped me figure out the calculation. It is:
    Code:
        double oldZoom = drawPane.getZoomLevelAsDouble();
        double oldLeft = drawPane.getScrollLeft();
        double oldTop = drawPane.getScrollTop();
        int drawPaneWidth = drawPane.getWidth();
        int drawPaneHeight = drawPane.getHeight();
        int newLeft = (int)((oldLeft/oldZoom + drawPaneWidth/oldZoom/2.0 - drawPaneWidth/newZoom/2.0)*newZoom);
        int newTop = (int)((oldTop/oldZoom + drawPaneHeight/oldZoom/2.0 - drawPaneHeight/newZoom/2.0)*newZoom);
        drawPane.setZoomLevel(newZoom);
        drawPane.scrollTo(newLeft,newTop);

    Comment

    Working...
    X