scaleCanvasToViewBox function

bool scaleCanvasToViewBox(
  1. Matrix4 matrix,
  2. Size desiredSize,
  3. Rect viewBox,
  4. Size pictureSize,
)

Scales a matrix to the given viewBox based on the desiredSize of the widget.

Returns true if the supplied matrix was modified.

Implementation

bool scaleCanvasToViewBox(
  Matrix4 matrix,
  Size desiredSize,
  Rect viewBox,
  Size pictureSize,
) {
  if (desiredSize == viewBox.size) {
    return false;
  }
  final double scale = math.min(
    desiredSize.width / viewBox.width,
    desiredSize.height / viewBox.height,
  );
  final Size scaledHalfViewBoxSize = viewBox.size * scale / 2.0;
  final Size halfDesiredSize = desiredSize / 2.0;
  final Offset shift = Offset(
    halfDesiredSize.width - scaledHalfViewBoxSize.width,
    halfDesiredSize.height - scaledHalfViewBoxSize.height,
  );
  matrix
    ..translate(shift.dx, shift.dy)
    ..scale(scale, scale);
  return true;
}