centerOn method

void centerOn(
  1. Offset point
)

Centers the viewport on a specific point in graph coordinates without changing zoom.

This is useful for centering the viewport on an arbitrary location, such as where a new node should be created.

Does nothing if the screen size is zero.

Parameters:

  • point: The point in graph coordinates to center on

Example:

// Center on a specific point where user wants to add a node
final center = controller.getViewportCenter();
controller.centerOn(center);

// Or center on a specific coordinate
controller.centerOn(Offset(500, 300));

Implementation

void centerOn(Offset point) {
  if (_screenSize.value == Size.zero) return;

  final currentVp = _viewport.value;

  setViewport(
    GraphViewport(
      x: _screenSize.value.width / 2 - point.dx * currentVp.zoom,
      y: _screenSize.value.height / 2 - point.dy * currentVp.zoom,
      zoom: currentVp.zoom,
    ),
  );
}