resizeTo method

Path resizeTo(
  1. Size size, {
  2. bool keepRatio = false,
})

Returns a new Path with the given size. If keepRatio is true the aspect ratio is kept, so that the path fits within the size.

The path is scaled about the origin, like a canvas is, so a path that is centered stays centered and a path that was moved toOrigin stays there, while any other path moves along with its distance to the origin.

A path without a width or a height is not scaled in that direction, since no scale can give it one, unless keepRatio is true: then the scale of the other direction applies to both, so that its distance to the origin still scales along.

Implementation

Path resizeTo(Size size, {bool keepRatio = false}) {
  assert(
    size.width > 0 && size.height > 0,
    'Resizing with invalid size: $size',
  );
  final box = getBounds();
  final hasWidth = box.width > 0;
  final hasHeight = box.height > 0;
  var scaleX = hasWidth ? size.width / box.width : 1.0;
  var scaleY = hasHeight ? size.height / box.height : 1.0;
  if (keepRatio) {
    final uniformScale = hasWidth && hasHeight
        ? min(scaleX, scaleY)
        : (hasWidth ? scaleX : scaleY);
    scaleX = uniformScale;
    scaleY = uniformScale;
  }
  final matrix = Float32List(16)
    ..[0] = scaleX
    ..[5] = scaleY
    ..[10] = 1
    ..[15] = 1;
  return transform32(matrix);
}