Constrain aspect ratio conservation to image boundaries as well.

This commit is contained in:
Aaron van Geffen 2020-12-30 12:56:17 +01:00
parent 6ddf518294
commit 5c4a075231

View File

@ -292,10 +292,23 @@ class CropEditor {
let targetAspectRatio = current.crop_width / current.crop_height;
if (Math.abs(currentAspectRatio - targetAspectRatio) > 0.001) {
// Landscape?
if (targetAspectRatio > 1.0) {
this.crop_height.value = Math.ceil(this.crop_width.value / targetAspectRatio);
} else {
this.crop_width.value = Math.ceil(this.crop_height.value * targetAspectRatio);
let height = Math.ceil(this.crop_width.value / targetAspectRatio);
if (parseInt(this.source_y.value) + height > this.original_image.naturalHeight) {
height = this.original_image.naturalHeight - this.source_y.value;
}
this.crop_width.value = height * targetAspectRatio;
this.crop_height.value = height;
}
// Portrait?
else {
let width = Math.ceil(this.crop_height.value * targetAspectRatio);
if (parseInt(this.source_x.value) + width > this.original_image.naturalWidth) {
width = this.original_image.naturalWidth - this.source_x.value;
}
this.crop_width.value = width;
this.crop_height.value = width / targetAspectRatio;
}
}
}