Skip to content

Commit

Permalink
feat(cropper): add responsiveArea
Browse files Browse the repository at this point in the history
  • Loading branch information
Enlcxx committed Nov 22, 2020
1 parent 63a172a commit ec96a23
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/lib/image-cropper/image-cropper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ export class ImgCropperConfig {
* default: false
*/
keepAspectRatio?: boolean;
/**
* Whether the cropper area is responsive.
* By default, the width and height of the cropper area is fixed,
* so can use when the cropper area is larger than its container,
* otherwise this will bring problems when cropping.
*/
responsiveArea?: boolean = true;
}

/**
Expand Down Expand Up @@ -381,6 +388,12 @@ export class LyImageCropper implements OnInit, OnDestroy {
}
set config(val: ImgCropperConfig) {
this._config = mergeDeep({}, new ImgCropperConfig(), val);
if (
this._config.round
&& this.config.width !== this.config.height
) {
throw new Error(`${LyImageCropper.и}: Both width and height must be equal when using \`ImgCropperConfig.round = true\``);
}
const maxFileSize = this._config.maxFileSize;
if (maxFileSize) {
this.maxFileSize = maxFileSize;
Expand Down Expand Up @@ -467,6 +480,7 @@ export class LyImageCropper implements OnInit, OnDestroy {
element.removeEventListener('touchstart', this._pointerDown, activeEventOptions);
}

/** Load image with canvas */
private _imgLoaded(imgElement: HTMLImageElement) {
if (imgElement) {
this._img = imgElement;
Expand Down Expand Up @@ -513,6 +527,7 @@ export class LyImageCropper implements OnInit, OnDestroy {
@HostListener('window:resize') _resize$() {
if (this.isLoaded) {
this.updatePosition();
this._updateAreaIfNeeded();
}
}

Expand Down Expand Up @@ -780,8 +795,8 @@ export class LyImageCropper implements OnInit, OnDestroy {
}
x = (areaRect.left - hostRect.left);
y = (areaRect.top - hostRect.top);
x -= (xOrigin! - (this.config.width / 2));
y -= (yOrigin! - (this.config.height / 2));
x -= (xOrigin! - (areaRect.width / 2));
y -= (yOrigin! - (areaRect.height / 2));

this._setStylesForContImg({
x, y
Expand Down Expand Up @@ -905,6 +920,47 @@ export class LyImageCropper implements OnInit, OnDestroy {
);
}

private _updateAreaIfNeeded() {
if (!this._config.responsiveArea) {
return;
}
const rootRect = this._rootRect();
const width = this.config.width;
const minWidth = this.config.minWidth || 0;
const height = this.config.height;
const minHeight = this.config.minHeight || 0;
if (
!(width > rootRect.width
&& width > minWidth
|| height > rootRect.height
&& height > minHeight)
) {
return;
}
const minArea = Math.min(width, height);
const minHost = Math.min(rootRect.width, rootRect.height);
const currentArea = minArea;
const currentScale = this._scal3Fix!;
const newArea = minHost;
const newScale = (currentScale * newArea) / currentArea;
const keepAspectRatio = this.config.round || this.config.keepAspectRatio;

if (keepAspectRatio || width === height) {
this.config.width = this.config.height = minHost;
} else {
if (minArea === width) {
this.config.width = minHost;
this.config.height = (minHost * height) / width;
} else {
this.config.width = (minHost * width) / height;
this.config.height = minHost;
}
}
this._updateMinScale();
this.setScale(newScale);
this._markForCheck();
}

/**
* Load Image from URL
* @deprecated Use `loadImage` instead of `setImageUrl`
Expand All @@ -931,6 +987,10 @@ export class LyImageCropper implements OnInit, OnDestroy {
this.updatePosition(loadConfig.xOrigin, loadConfig.yOrigin);
}
this.rotate(loadConfig.rotation || 0);
this._markForCheck();
Promise.resolve(null).then(() => {
this._updateAreaIfNeeded();
});
}

this.isLoaded = true;
Expand Down

0 comments on commit ec96a23

Please sign in to comment.