Skip to content

Commit

Permalink
Set scaled min and max sizes when aspect ratio is changed - Fixes #443
Browse files Browse the repository at this point in the history
  • Loading branch information
Mawi137 committed Jun 1, 2021
1 parent 5bfd491 commit a538d17
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion projects/demo-app/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<br />
<br />
<button (click)="toggleContainWithinAspectRatio()">{{containWithinAspectRatio?'Fill Aspect Ratio':'Contain Within Aspect Ratio'}}</button>
<button (click)="toggleAspectRatio()">Aspect ratio: {{aspectRatio === 4/3 ? '4/3' : '16/9'}}</button>
<button (click)="resetImage()">Reset image</button>
<br />
<br />
Expand All @@ -23,7 +24,7 @@
[imageURL]="imageURL"
[maintainAspectRatio]="true"
[containWithinAspectRatio]="containWithinAspectRatio"
[aspectRatio]="4 / 3"
[aspectRatio]="aspectRatio"
[resizeToWidth]="128"
[cropperMinWidth]="128"
[onlyScaleDown]="true"
Expand Down
5 changes: 5 additions & 0 deletions projects/demo-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class AppComponent {
canvasRotation = 0;
rotation = 0;
scale = 1;
aspectRatio = 4 / 3;
showCropper = false;
containWithinAspectRatio = false;
transform: ImageTransform = {};
Expand Down Expand Up @@ -115,4 +116,8 @@ export class AppComponent {
rotate: this.rotation
};
}

toggleAspectRatio() {
this.aspectRatio = this.aspectRatio === 4 / 3 ? 16 / 9 : 4 / 3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,18 @@ export class ImageCropperComponent implements OnChanges, OnInit {
.then((res) => this.setLoadedImage(res))
.catch((err) => this.loadImageError(err));
}
if (changes.cropper) {
if (changes.cropper || changes.maintainAspectRatio || changes.aspectRatio) {
this.setMaxSize();
this.setCropperScaledMinSize();
this.setCropperScaledMaxSize();
this.checkCropperPosition(false);
this.doAutoCrop();
if (this.maintainAspectRatio && (changes.maintainAspectRatio || changes.aspectRatio)) {
this.resetCropperPosition();
} else if (changes.cropper) {
this.checkCropperPosition(false);
this.doAutoCrop();
}
this.cd.markForCheck();
}
if (changes.aspectRatio && this.imageVisible) {
this.resetCropperPosition();
}
if (changes.transform) {
this.transform = this.transform || {};
this.setCssTransform();
Expand Down

0 comments on commit a538d17

Please sign in to comment.