From 393bb939a99f0e0493b46f6e4594f8a747c770ba Mon Sep 17 00:00:00 2001 From: Chooyan Date: Sat, 12 Mar 2022 01:54:13 +0900 Subject: [PATCH] fix: calculate initial scale to cover cropping area if fixArea == true (#63) --- lib/src/calculator.dart | 13 ++++++++ lib/src/crop.dart | 73 ++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/lib/src/calculator.dart b/lib/src/calculator.dart index d043aec..7f06b9d 100644 --- a/lib/src/calculator.dart +++ b/lib/src/calculator.dart @@ -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); @@ -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; @@ -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; diff --git a/lib/src/crop.dart b/lib/src/crop.dart index 09b33d0..03ae973 100644 --- a/lib/src/crop.dart +++ b/lib/src/crop.dart @@ -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 @@ -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); }