Skip to content

Commit 100adfd

Browse files
author
Yan Kochkin
committed
redonefill calculations to keep default crop area behaviour
1 parent 8a4fd03 commit 100adfd

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

lib/src/calculators/calculate_crop_params.dart

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,35 @@ CropFitParams calculateCropParams({
1818
1919
switch (imageFit) {
2020
case CustomImageFit.fitCropSpace:
21-
final cropSize = min(screenWidth, screenHeight);
22-
final cropSizeToPaint = cropSize * cropPercentage;
23-
final defaultScale =
24-
(cropSize * cropPercentage) / max(imageWidth, imageHeight);
21+
final cropSize = min(screenWidth, screenHeight) * cropPercentage;
22+
final cropSizeToPaint = cropSize;
23+
final defaultScale = cropSize / max(imageWidth, imageHeight);
2524

2625
return CropFitParams(cropSize, cropSizeToPaint, defaultScale);
2726

2827
case CustomImageFit.fillCropWidth:
29-
final cropSize = screenWidth;
30-
final cropSizeToPaint = cropSize * cropPercentage;
31-
final defaultScale = (cropSize * cropPercentage) / imageWidth;
28+
final cropSize = screenWidth * cropPercentage;
29+
final cropSizeToPaint = cropSize;
30+
final defaultScale = cropSize / imageWidth;
3231

3332
return CropFitParams(cropSize, cropSizeToPaint, defaultScale);
3433
case CustomImageFit.fillCropHeight:
35-
final cropSize = screenHeight;
36-
final cropSizeToPaint = cropSize * cropPercentage;
37-
final defaultScale = (cropSize * cropPercentage) / imageHeight;
34+
final cropSize = screenHeight * cropPercentage;
35+
final cropSizeToPaint = cropSize;
36+
final defaultScale = cropSize / imageHeight;
3837

3938
return CropFitParams(cropSize, cropSizeToPaint, defaultScale);
4039
case CustomImageFit.fitVisibleSpace:
4140
late final double cropSize;
4241
late final double cropSizeToPaint;
4342
late final double defaultScale;
43+
cropSizeToPaint = min(screenWidth, screenHeight) * cropPercentage;
4444

4545
if (screenHeight < screenWidth) {
4646
cropSize = screenHeight;
47-
cropSizeToPaint = screenWidth * cropPercentage;
4847
defaultScale = screenHeight / imageHeight;
4948
} else {
5049
cropSize = screenWidth;
51-
cropSizeToPaint = cropSize * cropPercentage;
5250
defaultScale = screenWidth / imageWidth;
5351
}
5452

@@ -57,27 +55,26 @@ CropFitParams calculateCropParams({
5755
late final double cropSize;
5856
late final double cropSizeToPaint;
5957
late final double defaultScale;
58+
cropSizeToPaint = min(screenWidth, screenHeight) * cropPercentage;
6059

6160
if (screenHeight > screenWidth) {
6261
cropSize = screenHeight;
63-
cropSizeToPaint = screenWidth * cropPercentage;
6462
defaultScale = screenHeight / imageHeight;
6563
} else {
6664
cropSize = screenWidth;
67-
cropSizeToPaint = cropSize * cropPercentage;
6865
defaultScale = screenWidth / imageWidth;
6966
}
7067

7168
return CropFitParams(cropSize, cropSizeToPaint, defaultScale);
7269
case CustomImageFit.fillVisibleHeight:
7370
final cropSize = screenHeight;
74-
final cropSizeToPaint = screenWidth * cropPercentage;
71+
final cropSizeToPaint = min(screenWidth, screenHeight) * cropPercentage;
7572
final defaultScale = screenHeight / imageHeight;
7673

7774
return CropFitParams(cropSize, cropSizeToPaint, defaultScale);
7875
case CustomImageFit.fillVisiblelWidth:
7976
final cropSize = screenWidth;
80-
final cropSizeToPaint = cropSize * cropPercentage;
77+
final cropSizeToPaint = min(screenWidth, screenHeight) * cropPercentage;
8178
final defaultScale = screenWidth / imageWidth;
8279

8380
return CropFitParams(cropSize, cropSizeToPaint, defaultScale);

lib/src/calculators/calculate_on_crop_params.dart

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,17 @@ OnCropParams caclulateOnCropParams({
5050
if (screenHeight < screenWidth) {
5151
uiSize = screenHeight;
5252
cropSize = imageHeight.toDouble();
53-
translateScale =
54-
cropSize / uiSize / cropPercentage * (screenHeight / screenWidth);
55-
scale = dataScale / cropPercentage * (screenHeight / screenWidth);
5653
} else {
5754
uiSize = screenWidth;
5855
cropSize = imageWidth.toDouble();
59-
translateScale = cropSize / uiSize / cropPercentage;
60-
scale = dataScale / cropPercentage;
6156
}
6257

58+
scale = dataScale / cropPercentage;
59+
translateScale = cropSize / uiSize / cropPercentage;
60+
6361
return OnCropParams(cropSize, translateScale, scale);
6462
case CustomImageFit.fillVisibleSpace:
63+
final heightToWidthRatio = (screenHeight / screenWidth);
6564
late final double uiSize;
6665
late final double cropSize;
6766
late final double translateScale;
@@ -70,30 +69,43 @@ OnCropParams caclulateOnCropParams({
7069
if (screenHeight > screenWidth) {
7170
uiSize = screenHeight;
7271
cropSize = imageHeight.toDouble();
73-
translateScale =
74-
cropSize / uiSize / cropPercentage * (screenHeight / screenWidth);
75-
scale = dataScale / cropPercentage * (screenHeight / screenWidth);
72+
translateScale = cropSize / uiSize / cropPercentage * heightToWidthRatio;
73+
scale = dataScale / cropPercentage * heightToWidthRatio;
7674
} else {
7775
uiSize = screenWidth;
7876
cropSize = imageWidth.toDouble();
79-
translateScale = cropSize / uiSize / cropPercentage;
80-
scale = dataScale / cropPercentage;
77+
translateScale = cropSize / uiSize / cropPercentage / heightToWidthRatio;
78+
scale = dataScale / cropPercentage / heightToWidthRatio;
8179
}
8280

8381
return OnCropParams(cropSize, translateScale, scale);
8482
case CustomImageFit.fillVisibleHeight:
83+
final heightToWidthRatio = (screenHeight / screenWidth);
8584
final uiSize = screenHeight;
8685
final cropSize = imageHeight.toDouble();
87-
final translateScale =
88-
cropSize / uiSize / cropPercentage * (screenHeight / screenWidth);
89-
final scale = dataScale / cropPercentage * (screenHeight / screenWidth);
90-
86+
late final double translateScale;
87+
late final double scale;
88+
if (screenWidth > screenHeight) {
89+
translateScale = cropSize / uiSize / cropPercentage;
90+
scale = dataScale / cropPercentage;
91+
} else {
92+
translateScale = cropSize / uiSize / cropPercentage * heightToWidthRatio;
93+
scale = dataScale / cropPercentage * heightToWidthRatio;
94+
}
9195
return OnCropParams(cropSize, translateScale, scale);
9296
case CustomImageFit.fillVisiblelWidth:
97+
final heightToWidthRatio = (screenHeight / screenWidth);
9398
final uiSize = screenWidth;
9499
final cropSize = imageWidth.toDouble();
95-
final translateScale = cropSize / uiSize / cropPercentage;
96-
final scale = dataScale / cropPercentage;
100+
late final double translateScale;
101+
late final double scale;
102+
if (screenWidth > screenHeight) {
103+
translateScale = cropSize / uiSize / cropPercentage / heightToWidthRatio;
104+
scale = dataScale / cropPercentage / heightToWidthRatio;
105+
} else {
106+
translateScale = cropSize / uiSize / cropPercentage;
107+
scale = dataScale / cropPercentage;
108+
}
97109

98110
return OnCropParams(cropSize, translateScale, scale);
99111
}

0 commit comments

Comments
 (0)