plan static method

CanvasViewportPlanResult plan({
  1. required Size2D artboard,
  2. required double targetW,
  3. required double targetH,
  4. Rect2D? bounds,
  5. double paddingPx = 0,
  6. double bleedPx = 0,
  7. CanvasFit fit = CanvasFit.contain,
  8. double? minUniformScale,
  9. double? maxUniformScale,
  10. bool tight = false,
  11. bool snappingEnabled = false,
  12. double pixelRatioForSnapping = 1.0,
})

Plan a viewport transform for editor/thumb/export.

  • If bounds is provided, viewport fits bounds instead of artboard.
  • paddingPx is used for editor/thumb (inner padding).
  • bleedPx is used for export (outer bleed margin).
  • If tight and bounds is present: output size becomes bounds-tight, scaled up to fit within max targetW/targetH.

Snapping:

  • Controlled by snappingEnabled.
  • Export default MUST pass snappingEnabled=false (mathematically exact). TODO: future "sharp as possible" export mode can enable snapping.

Implementation

static CanvasViewportPlanResult plan({
  required Size2D artboard,
  required double targetW,
  required double targetH,
  Rect2D? bounds,
  double paddingPx = 0,
  double bleedPx = 0,
  CanvasFit fit = CanvasFit.contain,
  double? minUniformScale,
  double? maxUniformScale,
  bool tight = false,
  bool snappingEnabled = false,
  double pixelRatioForSnapping = 1.0,
}) {
  var outW = targetW;
  var outH = targetH;

  // Tight sizing (export) only makes sense with bounds.
  if (tight && bounds != null) {
    final bw = bounds.width;
    final bh = bounds.height;

    if (bw > 0 && bh > 0) {
      final s = math.min(targetW / bw, targetH / bh);
      outW = bw * s;
      outH = bh * s;
    }
  }

  // Padding path: use computeViewportWithPadding (inner padding).
  // Bleed path: use computeViewport with bleed (outer margin).
  final CanvasViewportTransform t = (paddingPx > 0)
      ? computeViewportWithPadding(
          artboardW: artboard.w,
          artboardH: artboard.h,
          viewportW: outW,
          viewportH: outH,
          bounds: bounds,
          paddingPx: paddingPx,
          fit: fit,
          minUniformScale: minUniformScale,
          maxUniformScale: maxUniformScale,
        )
      : computeViewport(
          artboardW: artboard.w,
          artboardH: artboard.h,
          targetW: outW,
          targetH: outH,
          bounds: bounds,
          bleed: bleedPx,
          fit: fit,
          minUniformScale: minUniformScale,
          maxUniformScale: maxUniformScale,
        );

  final snapped = snappingEnabled
      ? t.snapTranslation(pixelRatioForSnapping)
      : t;

  return CanvasViewportPlanResult(
    transform: snapped,
    outputW: outW,
    outputH: outH,
  );
}