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);
  final availableHeight = math.max(1, viewportSize.height - padding * 2);
  final fitted = math.min(
    availableWidth / bounds.width,
    availableHeight / bounds.height,
  );
  return copyWith(
    center: bounds.center,
    // Explicit fit must contain the requested bounds even when they need
    // more zoom-out than the interactive zoom gesture limit permits.
    zoom: math.min(fitted, maxZoom),
  );
}