Skip to content

Commit 1ee5314

Browse files
committed
Added area preservation to aspect ratio selection
1 parent 533f757 commit 1ee5314

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

src/components/panel/right/CropPanel.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import Slider from '../../ui/Slider';
2424
import { TEXT_COLOR_KEYS, TextColors, TextVariants, TextWeights } from '../../../types/typography';
2525
import { useEditorStore } from '../../../store/useEditorStore';
2626
import { useEditorActions } from '../../../hooks/useEditorActions';
27-
import { calculateCenteredCrop } from '../../../utils/cropUtils';
27+
import { calculateAreaPreservingCrop, calculateCenteredCrop } from '../../../utils/cropUtils';
28+
import { Crop } from 'react-image-crop';
2829

2930
const BASE_RATIO = 1.618;
3031
const ORIGINAL_RATIO = 0;
@@ -243,13 +244,26 @@ export default function CropPanel() {
243244

244245
const applyAspectRatio = useCallback(
245246
(newAspectRatio: number | null) => {
246-
const newCrop =
247-
selectedImage?.width && selectedImage?.height
248-
? calculateCenteredCrop(selectedImage.width, selectedImage.height, orientationSteps, newAspectRatio, rotation)
249-
: null;
247+
if (newAspectRatio === null) {
248+
setAdjustments((prev: Adjustments) => ({ ...prev, aspectRatio: null }));
249+
return;
250+
}
251+
let newCrop: Crop | null = null;
252+
if (selectedImage?.width && selectedImage?.height) {
253+
newCrop =
254+
calculateAreaPreservingCrop(
255+
selectedImage.width,
256+
selectedImage.height,
257+
orientationSteps,
258+
newAspectRatio,
259+
rotation,
260+
adjustments.crop,
261+
) ??
262+
calculateCenteredCrop(selectedImage.width, selectedImage.height, orientationSteps, newAspectRatio, rotation);
263+
}
250264
setAdjustments((prev: Adjustments) => ({ ...prev, aspectRatio: newAspectRatio, crop: newCrop }));
251265
},
252-
[selectedImage, orientationSteps, rotation, setAdjustments],
266+
[selectedImage, orientationSteps, rotation, adjustments.crop, setAdjustments],
253267
);
254268

255269
useEffect(() => {

0 commit comments

Comments
 (0)