Skip to content

Commit 5a197af

Browse files
authored
JI-5684 fix cropper rotation (#199)
* JI-5684 fix cropper rotation * JI-5684 update crop limit logic * JI-5684 fix scroll into view * JI-5684 remove scroll into view
1 parent 7bda580 commit 5a197af

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

libs/cropper.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function Cropper(options) {
2323
/**
2424
* Set of pointer event handler functions for moving the selector.
2525
*/
26-
2726
const handleMove = () => {
2827
const onPointerDown = (event) => {
2928
event.preventDefault();
@@ -174,21 +173,6 @@ function Cropper(options) {
174173
this.handles[type].onpointerdown = onPointerDown;
175174
}
176175

177-
/**
178-
* Callback for canvas.context.toBlob image retrieval.
179-
*
180-
* @param {Blob} blob Raw image data.
181-
*/
182-
const handleBlob = (blob) => {
183-
const url = URL.createObjectURL(blob);
184-
this.image = new Image();
185-
this.image.onload = () => {
186-
URL.revokeObjectURL(url);
187-
this.loadImage();
188-
}
189-
this.image.src = url;
190-
}
191-
192176
/**
193177
* Shows/hides mask around selector.
194178
*
@@ -305,6 +289,21 @@ function Cropper(options) {
305289
return { width, height };
306290
}
307291

292+
/**
293+
* Callback for canvas.context.toBlob image retrieval.
294+
*
295+
* @param {Blob} blob Raw image data.
296+
*/
297+
const handleBlob = (blob) => {
298+
const url = URL.createObjectURL(blob);
299+
this.image = new Image();
300+
this.image.onload = () => {
301+
URL.revokeObjectURL(url);
302+
this.loadImage();
303+
}
304+
this.image.src = url;
305+
}
306+
308307
/**
309308
* Draws image within canvas.
310309
*/
@@ -334,25 +333,24 @@ function Cropper(options) {
334333
* The output is generated from the mirror canvas based on scaled selector dimensions.
335334
*/
336335
this.crop = () => {
337-
const maxSelectedWidth = this.canvas.width - this.margins.left * 2;
338-
const maxSelectedHeight = this.canvas.height - this.margins.top * 2;
336+
let painted = this.fit(this.image);
339337
let selectedX = this.selector.offsetLeft - this.margins.left;
340338
let selectedY = this.selector.offsetTop - this.margins.top;
341339
let selectedWidth = this.selector.offsetWidth;
342340
let selectedHeight = this.selector.offsetHeight;
343-
if (selectedX < 0) {
344-
selectedWidth += selectedX;
341+
if (this.selector.offsetLeft < this.margins.left) {
345342
selectedX = 0;
343+
selectedWidth -= this.margins.left - this.selector.offsetLeft;
346344
}
347-
if (selectedY < 0) {
348-
selectedHeight += selectedY;
345+
if (this.selector.offsetTop < this.margins.top) {
349346
selectedY = 0;
347+
selectedHeight -= this.margins.top - this.selector.offsetTop;
350348
}
351-
if (selectedX + selectedWidth > maxSelectedWidth) {
352-
selectedWidth = maxSelectedWidth - selectedX;
349+
if (selectedX + selectedWidth > painted.width) {
350+
selectedWidth -= selectedX + selectedWidth - painted.width;
353351
}
354-
if (selectedY + selectedHeight > maxSelectedHeight) {
355-
selectedHeight = maxSelectedHeight - selectedY;
352+
if (selectedY + selectedHeight > painted.height) {
353+
selectedHeight -= selectedY + selectedHeight - painted.height;
356354
}
357355
const ICRatio = this.image.width / (this.canvas.width - this.margins.left * 2);
358356
selectedX = ICRatio * selectedX;

0 commit comments

Comments
 (0)