drawImageFitted method

void drawImageFitted(
  1. PCanvasImage image,
  2. num dstX,
  3. num dstY,
  4. num dstWidth,
  5. num dstHeight,
)

Draw an image fitting a destiny area (dstX,dstY , dstWidth x dstHeight).

Implementation

void drawImageFitted(
    PCanvasImage image, num dstX, num dstY, num dstWidth, num dstHeight) {
  checkImageLoaded(image);

  if (dstWidth == 0 || dstHeight == 0) return;

  final imgR = image.aspectRatio;
  final r = dstWidth / dstHeight;

  final imageW = image.width;
  final imageH = image.height;

  var srcX = 0;
  var srcY = 0;
  var srcW = imageW;
  var srcH = imageH;

  if (imgR < r) {
    srcW = imageW;
    srcH = (srcW * (1 / r)).toInt();
    srcY = (imageH - srcH) ~/ 2;
  } else if (imgR > r) {
    srcH = imageH;
    srcW = (srcH * r).toInt();
    srcX = (imageW - srcW) ~/ 2;
  }

  drawImageArea(
      image, srcX, srcY, srcW, srcH, dstX, dstY, dstWidth, dstHeight);
}