title: JavaFx 8 - Scaling / zooming ScrollPane relative to mouse position
body: <p>I need to zoom in / out on a scroll pane, relative to the mouse position.</p>
  
  <p>I currently achieve the zooming functionality by wrapping my content in a Group, and scaling the group itself. I create a new Scale object with a custom pivot. (Pivot is set to the mouse position)</p>
  
  <p>This works perfectly for where the Group's initial scale is 1.0, however scaling afterwards does not scale in the correct direction - I believe this is because the relative mouse position changes when the Group has been scaled.</p>
  
  <p>My code:</p>
  
  <pre><code>@Override
  public void initialize(URL location, ResourceBundle resources) {
  
      Delta initial_mouse_pos = new Delta();
  
      anchorpane.setOnScrollStarted(event -&gt; {
          initial_mouse_pos.x = event.getX();
          initial_mouse_pos.y = event.getY();
      });
  
      anchorpane.setOnScroll(event -&gt; {
          double zoom_fac = 1.05;
          double delta_y = event.getDeltaY();
  
          if(delta_y &lt; 0) {
              zoom_fac = 2.0 - zoom_fac;
          }
  
          Scale newScale = new Scale();
          newScale.setPivotX(initial_mouse_pos.x);
          newScale.setPivotY(initial_mouse_pos.y);
          newScale.setX( content_group.getScaleX() * zoom_fac );
          newScale.setY( content_group.getScaleY() * zoom_fac );
  
          content_group.getTransforms().add(newScale);
  
          event.consume();
      });
  }
  
  private class Delta { double x, y; }
  </code></pre>
  
  <p>How do I get the correct mouse position at different levels of scaling? Is there a completely different way to zooming the ScrollPane that is easier?</p>