getCropRect method

Rect? getCropRect(
  1. Rect viewportCropRect
)

Implementation

Rect? getCropRect(Rect viewportCropRect) {
  if (_imageRenderSize.isEmpty) return null;

  final Matrix4 transform = transformationController.value;
  final Matrix4 inverse = Matrix4.tryInvert(transform) ?? Matrix4.identity();

  final Offset topLeft = MatrixUtils.transformPoint(
    inverse,
    viewportCropRect.topLeft,
  );
  final Offset bottomRight = MatrixUtils.transformPoint(
    inverse,
    viewportCropRect.bottomRight,
  );

  final Rect localCropRect = Rect.fromPoints(topLeft, bottomRight);

  final double scaleX = _originalImage.width / _imageRenderSize.width;
  final double scaleY = _originalImage.height / _imageRenderSize.height;

  final Rect result = Rect.fromLTWH(
    localCropRect.left * scaleX,
    localCropRect.top * scaleY,
    localCropRect.width * scaleX,
    localCropRect.height * scaleY,
  );

  final Rect imageBounds = Rect.fromLTWH(
    0,
    0,
    _originalImage.width.toDouble(),
    _originalImage.height.toDouble(),
  );

  return result.intersect(imageBounds);
}