@@ -200,35 +200,11 @@ abstract class ImageCropper {
200200
201201 this . centerSelection ( ) ;
202202
203- // Limit the selection to the canvas boundaries
204- this . cropperSelection ! . addEventListener ( "change" , ( event : CustomEvent ) => {
205- // see https://fengyuanchen.github.io/cropperjs/v2/api/cropper-selection.html#limit-boundaries
206- const cropperCanvasRect = this . cropperCanvas ! . getBoundingClientRect ( ) ;
207- const selection = event . detail as Selection ;
208-
209- const maxSelection : Selection = {
210- x : 0 ,
211- y : 0 ,
212- width : cropperCanvasRect . width ,
213- height : cropperCanvasRect . height ,
214- } ;
215-
216- if ( ! inSelection ( selection , maxSelection ) ) {
217- event . preventDefault ( ) ;
218-
219- // Try to clamp the position to the boundaries.
220- this . cropperSelection ! . $moveTo (
221- clampValue ( selection . x , selection . width , maxSelection . width ) ,
222- clampValue ( selection . y , selection . height , maxSelection . height ) ,
223- ) ;
224- }
225- } ) ;
226-
227- // Limit the selection to the min/max size
228203 this . cropperSelection ! . addEventListener ( "change" , ( event : CustomEvent ) => {
229204 const selection = event . detail as Selection ;
230205 this . cropperCanvasRect = this . cropperCanvas ! . getBoundingClientRect ( ) ;
231206
207+ // Limit the selection to the min/max size.
232208 const selectionRatio = Math . min (
233209 this . cropperCanvasRect . width / this . width ,
234210 this . cropperCanvasRect . height / this . height ,
@@ -246,6 +222,33 @@ abstract class ImageCropper {
246222
247223 if ( width < minWidth || height < minHeight || width > maxWidth || height > maxHeight ) {
248224 event . preventDefault ( ) ;
225+
226+ // Stop the event handling here otherwise the following code would try
227+ // to adjust the position on an invalid size that could potentially
228+ // violate the boundaries.
229+ return ;
230+ }
231+
232+ // Limit the selection to the canvas boundaries.
233+ // see https://fengyuanchen.github.io/cropperjs/v2/api/cropper-selection.html#limit-boundaries
234+ const maxSelection : Selection = {
235+ x : 0 ,
236+ y : 0 ,
237+ width : this . cropperCanvasRect ! . width ,
238+ height : this . cropperCanvasRect ! . height ,
239+ } ;
240+
241+ if ( ! inSelection ( selection , maxSelection ) ) {
242+ event . preventDefault ( ) ;
243+
244+ // Clamp the position to the boundaries of the canvas.
245+ this . cropperSelection ! . x = clampValue ( selection . x , selection . width , maxSelection . width ) ;
246+ this . cropperSelection ! . y = clampValue ( selection . y , selection . height , maxSelection . height ) ;
247+
248+ this . cropperSelection ! . width = selection . width ;
249+ this . cropperSelection ! . height = selection . height ;
250+
251+ this . cropperSelection ! . $render ( ) ;
249252 }
250253 } ) ;
251254 }
0 commit comments