animateToNode method

void animateToNode(
  1. String nodeId, {
  2. double? zoom = 1.0,
  3. Duration duration = const Duration(milliseconds: 400),
  4. Curve curve = Curves.easeInOut,
})

Animates the viewport to center on a specific node.

This combines panning and optional zooming into a smooth animation.

Parameters:

  • nodeId: The ID of the node to center on
  • zoom: Target zoom level (default: 1.0). Pass null to keep current zoom.
  • duration: Animation duration (default: 400ms)
  • curve: Animation curve (default: easeInOut)

Does nothing if the node doesn't exist or screen size is zero.

Example:

controller.animateToNode('node-123');
controller.animateToNode('node-123', zoom: 1.5);
controller.animateToNode('node-123', zoom: null); // keep current zoom
controller.animateToNode('node-123', duration: Duration(milliseconds: 200));

Implementation

void animateToNode(
  String nodeId, {
  double? zoom = 1.0,
  Duration duration = const Duration(milliseconds: 400),
  Curve curve = Curves.easeInOut,
}) {
  final node = _nodes[nodeId];
  if (node == null || _screenSize.value == Size.zero) return;

  final pos = node.position.value;
  final size = node.size.value;
  final targetZoom = (zoom ?? _viewport.value.zoom).clamp(
    _config.minZoom.value,
    _config.maxZoom.value,
  );

  // Calculate node center and target viewport
  final nodeCenterX = pos.dx + size.width / 2;
  final nodeCenterY = pos.dy + size.height / 2;

  animateToViewport(
    GraphViewport(
      x: _screenSize.value.width / 2 - nodeCenterX * targetZoom,
      y: _screenSize.value.height / 2 - nodeCenterY * targetZoom,
      zoom: targetZoom,
    ),
    duration: duration,
    curve: curve,
  );
}