animateToScale method

void animateToScale(
  1. double scale, {
  2. Duration duration = const Duration(milliseconds: 400),
  3. Curve curve = Curves.easeInOut,
})

Animates the viewport to a specific zoom level, keeping the center fixed.

Parameters:

  • scale: Target zoom level (1.0 = 100%)
  • duration: Animation duration (default: 400ms)
  • curve: Animation curve (default: easeInOut)

Example:

controller.animateToScale(1.5); // Zoom to 150%
controller.animateToScale(1.0); // Reset to 100%

Implementation

void animateToScale(
  double scale, {
  Duration duration = const Duration(milliseconds: 400),
  Curve curve = Curves.easeInOut,
}) {
  if (_screenSize.value == Size.zero) return;

  final currentVp = _viewport.value;
  final clampedScale = scale.clamp(
    _config.minZoom.value,
    _config.maxZoom.value,
  );

  // Calculate the current center in graph coordinates
  final centerX =
      (_screenSize.value.width / 2 - currentVp.x) / currentVp.zoom;
  final centerY =
      (_screenSize.value.height / 2 - currentVp.y) / currentVp.zoom;

  // Calculate new viewport to keep center fixed at new scale
  animateToViewport(
    GraphViewport(
      x: _screenSize.value.width / 2 - centerX * clampedScale,
      y: _screenSize.value.height / 2 - centerY * clampedScale,
      zoom: clampedScale,
    ),
    duration: duration,
    curve: curve,
  );
}