From e84c4f2b4383c0057cc59dfd2d1306de0bde090a Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Mon, 23 Nov 2020 20:20:37 +0100 Subject: [PATCH] Constrain crop selection to image dimensions. --- public/js/crop_editor.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/public/js/crop_editor.js b/public/js/crop_editor.js index 9fb700e..9f4a6ca 100644 --- a/public/js/crop_editor.js +++ b/public/js/crop_editor.js @@ -227,8 +227,12 @@ class CropEditor { this.source_x.value = Math.ceil(Math.min(this.dragStartX, this.dragEndX) * scaleFactor); this.source_y.value = Math.ceil(Math.min(this.dragStartY, this.dragEndY) * scaleFactor); - this.crop_width.value = Math.ceil(Math.abs(this.dragEndX - this.dragStartX) * scaleFactor); - this.crop_height.value = Math.ceil(Math.abs(this.dragEndY - this.dragStartY) * scaleFactor); + + let width = Math.ceil(Math.abs(this.dragEndX - this.dragStartX) * scaleFactor); + this.crop_width.value = Math.min(width, this.original_image.naturalWidth - this.source_x.value); + + let height = Math.ceil(Math.abs(this.dragEndY - this.dragStartY) * scaleFactor); + this.crop_height.value = Math.min(height, this.original_image.naturalHeight - this.source_y.value); this.positionBoundary(); }