Skip to content

Commit

Permalink
Clamp the values when the selection violates the boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdesign committed Dec 19, 2024
1 parent 8cfb42e commit fb8cdd0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
40 changes: 29 additions & 11 deletions ts/WoltLabSuite/Core/Component/Image/Cropper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ function inSelection(selection: Selection, maxSelection: Selection): boolean {
);
}

function clampValue(position: number, length: number, availableLength: number): number {
if (position < 0) {
return 0;
}

if (position + length > availableLength) {
return Math.floor(availableLength - length);
}

return Math.floor(position);
}

abstract class ImageCropper {
readonly configuration: CropperConfiguration;
readonly file: File;
Expand Down Expand Up @@ -203,6 +215,12 @@ abstract class ImageCropper {

if (!inSelection(selection, maxSelection)) {
event.preventDefault();

// Try to clamp the position to the boundaries.
this.cropperSelection!.$moveTo(
clampValue(selection.x, selection.width, maxSelection.width),
clampValue(selection.y, selection.height, maxSelection.height),
);
}
});

Expand All @@ -216,17 +234,17 @@ abstract class ImageCropper {
this.cropperCanvasRect.height / this.height,
);

const minWidth = this.minSize.width * selectionRatio;
const maxWidth = this.cropperCanvasRect.width;
const minHeight = minWidth / this.configuration.aspectRatio;
const maxHeight = maxWidth / this.configuration.aspectRatio;

if (
selection.width < minWidth ||
selection.height < minHeight ||
selection.width > maxWidth ||
selection.height > maxHeight
) {
// Round all values to integers to avoid dealing with the wonderful world
// of IEEE 754 numbers.
const minWidth = Math.round(this.minSize.width * selectionRatio);
const maxWidth = Math.round(this.cropperCanvasRect.width);
const minHeight = Math.round(minWidth / this.configuration.aspectRatio);
const maxHeight = Math.round(maxWidth / this.configuration.aspectRatio);

const width = Math.round(selection.width);
const height = Math.round(selection.height);

if (width < minWidth || height < minHeight || width > maxWidth || height > maxHeight) {
event.preventDefault();
}
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fb8cdd0

Please sign in to comment.