Skip to content

Commit

Permalink
fix: calculate initial scale to cover cropping area if fixArea == true (
Browse files Browse the repository at this point in the history
  • Loading branch information
chooyan-eng authored Mar 11, 2022
1 parent bf2ad4c commit 393bb93
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
13 changes: 13 additions & 0 deletions lib/src/calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ abstract class _Calculator {
Rect initialCropRect(
Size screenSize, Rect imageRect, double aspectRatio, double sizeRatio);

/// calculates initial scale of image to cover _CropEditor
double scaleToCover(Size screenSize, Rect imageRect);

/// calculates ratio of [targetImage] and [screenSize]
double screenSizeRatio(image.Image targetImage, Size screenSize);

Expand Down Expand Up @@ -263,6 +266,11 @@ class _HorizontalCalculator extends _Calculator {
);
}

@override
double scaleToCover(Size screenSize, Rect imageRect) {
return screenSize.height / imageRect.height;
}

@override
double screenSizeRatio(image.Image targetImage, Size screenSize) {
return targetImage.width / screenSize.width;
Expand Down Expand Up @@ -300,6 +308,11 @@ class _VerticalCalculator extends _Calculator {
);
}

@override
double scaleToCover(Size screenSize, Rect imageRect) {
return screenSize.width / imageRect.width;
}

@override
double screenSizeRatio(image.Image targetImage, Size screenSize) {
return targetImage.height / screenSize.height;
Expand Down
73 changes: 40 additions & 33 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,41 +210,43 @@ class _CropEditorState extends State<_CropEditor> {

// scale
if (_pointerNum >= 2) {
final nextScale = _baseScale * detail.scale;

late double baseHeight;
late double baseWidth;
final ratio = _targetImage!.height / _targetImage!.width;

if (_isFitVertically) {
baseHeight = MediaQuery.of(context).size.height;
baseWidth = baseHeight / ratio;
} else {
baseWidth = MediaQuery.of(context).size.width;
baseHeight = baseWidth * ratio;
}
_applyScale(_baseScale * detail.scale);
}
}

// width
final newWidth = baseWidth * nextScale;
final leftPositionDelta = (newWidth - _imageRect.width) * 0.5;

// height
final newHeight = baseHeight * nextScale;
final topPositionDelta = (newHeight - _imageRect.height) * 0.5;

// apply
setState(() {
final newLeft = _imageRect.left - leftPositionDelta;
final newTop = _imageRect.top - topPositionDelta;
_imageRect = Rect.fromLTRB(
newLeft,
newTop,
newLeft + newWidth,
newTop + newHeight,
);
_scale = nextScale;
});
void _applyScale(double nextScale) {
late double baseHeight;
late double baseWidth;
final ratio = _targetImage!.height / _targetImage!.width;

if (_isFitVertically) {
baseHeight = MediaQuery.of(context).size.height;
baseWidth = baseHeight / ratio;
} else {
baseWidth = MediaQuery.of(context).size.width;
baseHeight = baseWidth * ratio;
}

// width
final newWidth = baseWidth * nextScale;
final leftPositionDelta = (newWidth - _imageRect.width) * 0.5;

// height
final newHeight = baseHeight * nextScale;
final topPositionDelta = (newHeight - _imageRect.height) * 0.5;

// apply
setState(() {
final newLeft = _imageRect.left - leftPositionDelta;
final newTop = _imageRect.top - topPositionDelta;
_imageRect = Rect.fromLTRB(
newLeft,
newTop,
newLeft + newWidth,
newTop + newHeight,
);
_scale = nextScale;
});
}

@override
Expand Down Expand Up @@ -315,6 +317,11 @@ class _CropEditorState extends State<_CropEditor> {

_imageRect = calculator.imageRect(screenSize, imageRatio);

if (widget.fixArea) {
final initialScale = calculator.scaleToCover(screenSize, _imageRect);
_applyScale(initialScale);
}

_resizeWith(widget.aspectRatio, widget.initialArea);
}

Expand Down

0 comments on commit 393bb93

Please sign in to comment.