forBounds method

DiagramCameraState forBounds(
  1. DiagramRect bounds, {
  2. double padding = 48,
})

This camera fitted to bounds, leaving padding around them.

Implementation

DiagramCameraState forBounds(DiagramRect bounds, {double padding = 48}) {
  if (bounds.width <= 0 ||
      bounds.height <= 0 ||
      viewportSize.width <= 0 ||
      viewportSize.height <= 0) {
    return this;
  }
  final availableWidth = math.max(1, viewportSize.width - padding * 2);
  // Above the room the canvas keeps for its marks.
  final availableHeight = math.max(1, usableHeight - padding * 2);
  final fitted = math.min(
    availableWidth / bounds.width,
    availableHeight / bounds.height,
  );
  // Explicit fit must contain the requested bounds even when they need
  // more zoom-out than the interactive zoom gesture limit permits.
  final zoom = math.min(fitted, maxZoom);
  return copyWith(center: _aboveReserved(bounds.center, zoom), zoom: zoom);
}