Skip to content

Commit ed23104

Browse files
refactor(ui): simplify crop constraints
1 parent b51a232 commit ed23104

File tree

1 file changed

+16
-34
lines changed
  • invokeai/frontend/web/src/features/editImageModal/lib

1 file changed

+16
-34
lines changed

invokeai/frontend/web/src/features/editImageModal/lib/editor.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ import Konva from 'konva';
44
import type { KonvaEventObject } from 'konva/lib/Node';
55
import { objectEntries } from 'tsafe';
66

7-
type CropConstraints = {
8-
minWidth?: number;
9-
minHeight?: number;
10-
maxWidth?: number;
11-
maxHeight?: number;
12-
aspectRatio?: number;
13-
};
14-
157
export type CropBox = {
168
x: number;
179
y: number;
@@ -119,10 +111,8 @@ export class Editor {
119111
private isCropping = false;
120112
private config: EditorConfig = DEFAULT_CONFIG;
121113

122-
private cropConstraints: CropConstraints = {
123-
minWidth: this.config.MIN_CROP_DIMENSION,
124-
minHeight: this.config.MIN_CROP_DIMENSION,
125-
};
114+
private aspectRatio: number | null = null;
115+
126116
private callbacks: EditorCallbacks = {};
127117
private cropBox: CropBox | null = null;
128118

@@ -718,18 +708,10 @@ export class Editor {
718708
return;
719709
}
720710

721-
let { newX, newY, newWidth, newHeight } = this.cropConstraints.aspectRatio
711+
let { newX, newY, newWidth, newHeight } = this.aspectRatio
722712
? this._resizeCropBoxWithAspectRatio(handleName, handleRect)
723713
: this._resizeCropBoxFree(handleName, handleRect);
724714

725-
// Apply general constraints
726-
if (this.cropConstraints.maxWidth) {
727-
newWidth = Math.min(newWidth, this.cropConstraints.maxWidth);
728-
}
729-
if (this.cropConstraints.maxHeight) {
730-
newHeight = Math.min(newHeight, this.cropConstraints.maxHeight);
731-
}
732-
733715
this.updateCropBox({
734716
x: newX,
735717
y: newY,
@@ -754,8 +736,8 @@ export class Editor {
754736
const handleX = handleRect.x() + handleRect.width() / 2;
755737
const handleY = handleRect.y() + handleRect.height() / 2;
756738

757-
const minWidth = this.cropConstraints.minWidth ?? this.config.MIN_CROP_DIMENSION;
758-
const minHeight = this.cropConstraints.minHeight ?? this.config.MIN_CROP_DIMENSION;
739+
const minWidth = this.config.MIN_CROP_DIMENSION;
740+
const minHeight = this.config.MIN_CROP_DIMENSION;
759741

760742
// Update dimensions based on handle type
761743
if (handleName.includes('left')) {
@@ -779,18 +761,18 @@ export class Editor {
779761
};
780762

781763
private _resizeCropBoxWithAspectRatio = (handleName: HandleName, handleRect: Konva.Rect) => {
782-
if (!this.konva?.image.image || !this.cropConstraints.aspectRatio || !this.cropBox) {
764+
if (!this.konva?.image.image || !this.aspectRatio || !this.cropBox) {
783765
throw new Error('Crop box, image, or aspect ratio not found');
784766
}
785767
const imgWidth = this.konva.image.image.width();
786768
const imgHeight = this.konva.image.image.height();
787-
const ratio = this.cropConstraints.aspectRatio;
769+
const ratio = this.aspectRatio;
788770

789771
const handleX = handleRect.x() + handleRect.width() / 2;
790772
const handleY = handleRect.y() + handleRect.height() / 2;
791773

792-
const minWidth = this.cropConstraints.minWidth ?? this.config.MIN_CROP_DIMENSION;
793-
const minHeight = this.cropConstraints.minHeight ?? this.config.MIN_CROP_DIMENSION;
774+
const minWidth = this.config.MIN_CROP_DIMENSION;
775+
const minHeight = this.config.MIN_CROP_DIMENSION;
794776

795777
// Early boundary check for aspect ratio mode
796778
const atLeftEdge = this.cropBox.x <= 0;
@@ -1218,9 +1200,9 @@ export class Editor {
12181200
}
12191201
};
12201202

1221-
setCropAspectRatio = (ratio: number | undefined) => {
1203+
setCropAspectRatio = (ratio: number | null) => {
12221204
// Update the constraint
1223-
this.cropConstraints.aspectRatio = ratio;
1205+
this.aspectRatio = ratio;
12241206

12251207
if (!this.konva?.image.image || !this.cropBox) {
12261208
return;
@@ -1230,7 +1212,7 @@ export class Editor {
12301212
const currentHeight = this.cropBox.height;
12311213
const currentArea = currentWidth * currentHeight;
12321214

1233-
if (ratio === undefined) {
1215+
if (ratio === null) {
12341216
// Just removed the aspect ratio constraint, no need to adjust
12351217
return;
12361218
}
@@ -1258,8 +1240,8 @@ export class Editor {
12581240
}
12591241

12601242
// Apply minimum size constraints
1261-
const minWidth = this.cropConstraints.minWidth ?? this.config.MIN_CROP_DIMENSION;
1262-
const minHeight = this.cropConstraints.minHeight ?? this.config.MIN_CROP_DIMENSION;
1243+
const minWidth = this.config.MIN_CROP_DIMENSION;
1244+
const minHeight = this.config.MIN_CROP_DIMENSION;
12631245

12641246
if (newWidth < minWidth) {
12651247
newWidth = minWidth;
@@ -1289,8 +1271,8 @@ export class Editor {
12891271
});
12901272
};
12911273

1292-
getCropAspectRatio = (): number | undefined => {
1293-
return this.cropConstraints.aspectRatio;
1274+
getCropAspectRatio = (): number | null => {
1275+
return this.aspectRatio;
12941276
};
12951277

12961278
// Utility

0 commit comments

Comments
 (0)