zoomBy method

void zoomBy(
  1. double delta
)

Zoom the viewport by a delta value while maintaining the viewport center as the focal point.

The zoom level is clamped to the min/max zoom values configured in NodeFlowConfig. The viewport automatically adjusts to keep the center point fixed during zoom.

Parameters:

  • delta: The amount to change the zoom by (positive to zoom in, negative to zoom out)

Example:

controller.zoomBy(0.1); // Zoom in by 10%
controller.zoomBy(-0.1); // Zoom out by 10%

Implementation

void zoomBy(double delta) {
  final currentVp = _viewport.value;
  final newZoom = (currentVp.zoom + delta).clamp(
    _config.minZoom.value,
    _config.maxZoom.value,
  );

  if (newZoom == currentVp.zoom) return; // No change needed

  final size = _screenSize.value;

  // Calculate the current viewport center in world coordinates
  final viewportCenterScreen = Offset(size.width / 2, size.height / 2);
  final viewportCenterWorld = screenToWorld(viewportCenterScreen);

  // After zoom, we want this world point to remain at the same screen position
  // Calculate the new pan position to keep the center point fixed
  final newPanX =
      viewportCenterScreen.dx - (viewportCenterWorld.dx * newZoom);
  final newPanY =
      viewportCenterScreen.dy - (viewportCenterWorld.dy * newZoom);

  setViewport(GraphViewport(x: newPanX, y: newPanY, zoom: newZoom));
}