I can't get working zoom relative to mouse position in JavaFX. I've read and tried rewrite this and this, but it's not working. Can't scroll viewport using scrollPane.setViewportBounds()
. I must use scrollPane.setHvalue()
and scrollPane.setVvalue()
, but can't get right recalculation to keep the same pixels under the mouse cursor. Current code keeps scrollbars positions:
ZoomHandler.java
import javafx.event.EventHandler;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
public class ZoomHandler implements EventHandler<ScrollEvent> {
private final ScrollPane scrollPane;
private final Pane canvas;
private final double minScale;
private final double maxScale;
public ZoomHandler(ScrollPane scrollPane, Pane canvas, double minScale, double maxScale) {
this.scrollPane = scrollPane;
this.canvas = canvas;
this.minScale = minScale;
this.maxScale = maxScale;
}
public ZoomHandler(ScrollPane scrollPane, Pane canvas) {
this(scrollPane, canvas, 0.1, 10);
}
@Override
public void handle(ScrollEvent e) {
if (e.isControlDown()) {
double actualScale = canvas.getScaleX();
if (actualScale > maxScale || actualScale < minScale) {
e.consume();
return;
}
double hVal = scrollPane.getHvalue();
double vVal = scrollPane.getVvalue();
double scale, factor;
if (e.getDeltaY() > 0) {
factor = 1.1;
} else {
factor = 0.9;
}
scale = actualScale * factor;
scale = Math.min(scale, maxScale);
scale = Math.max(scale, minScale);
canvas.setScaleX(scale);
canvas.setScaleY(scale);
scrollPane.setHvalue(hVal);
scrollPane.setVvalue(vVal);
e.consume();
}
}
}
Main.java
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
public static final int CANVAS_WIDTH = 1000;
public static final int CANVAS_HEIGHT = 1000;
private final Pane canvas = new Pane();
private final ScrollPane scrollPane = new ScrollPane();
private final ZoomHandler zoomHandler = new ZoomHandler(scrollPane, canvas);
public static void main(String[] args) { launch(args); }
@Override public void start(Stage primaryStage) {
canvas.setPrefWidth(CANVAS_WIDTH);
canvas.setPrefHeight(CANVAS_HEIGHT);
canvas.setOnScroll(zoomHandler);
scrollPane.setStyle("-fx-background-color: transparent; -fx-control-inner-background: transparent;");
scrollPane.setContent(new Group(canvas));
scrollPane.setPannable(true);
scrollPane.setOnScroll(zoomHandler);
addContent();
Scene scene = new Scene(scrollPane, 800, 600);
primaryStage.setTitle("Zoom");
primaryStage.setScene(scene);
primaryStage.show();
scrollPane.setHvalue(0.5);
scrollPane.setVvalue(0.5);
}
private void addContent() {
Text hello = new Text(400, 500, "Hello");
hello.setStyle("-fx-font-size: 20px; -fx-fill: red;");
Text world = new Text(500, 600, "world!");
world.setStyle("-fx-fill: blue;");
Text text = new Text(200, 350, "How to zoom to this text when mouse is over?");
text.setStyle("-fx-font-weight: bold;");
Rectangle rect = new Rectangle(600, 450, 300, 200);
rect.setFill(Color.GREEN);
Circle circle = new Circle(250, 700, 100);
circle.setFill(Color.YELLOW);
canvas.getChildren().addAll(hello, world, text, rect, circle);
}
}
Any ideas? Thanks in advance.