Comments (4)
How the app works now, I reckon you can...
- Deep clone the transform and cropper (cropper position) inputs after you have cropped your first img in the array. If there's problems with your cropper input not being updated to the latest values, cropping triggers the ImageCroppedEvent that sends an object with data including the cropperPosition. Make sure to deep clone these variables so the property values aren't updated later on. They are objects. {...obj} works.
- Load the next img in the array.
- Wait till the cropper is ready.
- Send in the copied transform and cropper inputs. If mantainAspect ratio was true before, make it false. Having it as true triggers a "new" cropper, overriding the cropper you send in.
- Crop or use auto crop and repeat with the next img.
If you want to see the cropper load the next img showing the state of the previous img, keep the cropper visibly hidden till you get the cropperReady event, pass in the previous cropper and transform and then change it back to visible.
I haven't tried this. But looking at how the app is now it should work. Happy to look at it if it doesn't work and you set up an example. Best of luck :)
from ngx-image-cropper.
How the app works now, I reckon you can...
- Deep clone the transform and cropper (cropper position) inputs after you have cropped your first img in the array. If there's problems with your cropper input not being updated to the latest values, cropping triggers the ImageCroppedEvent that sends an object with data including the cropperPosition. Make sure to deep clone these variables so the property values aren't updated later on. They are objects. {...obj} works.
- Load the next img in the array.
- Wait till the cropper is ready.
- Send in the copied transform and cropper inputs. If mantainAspect ratio was true before, make it false. Having it as true triggers a "new" cropper, overriding the cropper you send in.
- Crop or use auto crop and repeat with the next img.
If you want to see the cropper load the next img showing the state of the previous img, keep the cropper visibly hidden till you get the cropperReady event, pass in the previous cropper and transform and then change it back to visible.
I haven't tried this. But looking at how the app is now it should work. Happy to look at it if it doesn't work and you set up an example. Best of luck :)
Thank you for your help. Now my app is working, but there's a small issue. The new cropper doesn't work in cropperReady, but the transform works. After adding a setTimeout, the new cropper works well. I have provided code in the reply below.
Anyway, it does work now! (^◡^)
from ngx-image-cropper.
import {Component, ViewChild} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';
import {CropperPosition, ImageCropperComponent, ImageTransform} from 'ngx-image-cropper';
@Component({
selector: 'app-root',
template: `
<button (click)="loadImage()">Load Image</button>
<button (click)="zoomIn()">Zoom In</button>
<button (click)="zoomOut()">Zoom Out</button>
<button (click)="crop()">Run Crop Gif</button>
<div class="cropper-wrapper">
<image-cropper
#imageCropperRef
[imageURL]="imageUrl"
[autoCrop]="false"
[maintainAspectRatio]="false"
[(transform)]="transform"
[cropper]="cropper"
(cropperReady)="cropperReady()"
></image-cropper>
</div>
<ng-container *ngIf="!loading">
<div *ngFor="let frame of croppedImages">
<img [src]="frame" />
</div>
</ng-container>
`,
styles: [
`
image-cropper {
max-height: 50vh;
}
.cropper-wrapper {
min-height: 300px;
position: relative;
}
`,
],
})
export class AppComponent {
@ViewChild('imageCropperRef') imageCropperRef?: ImageCropperComponent;
croppedImages: any[] = [];
imageUrl?: string;
imageUrls = ['assets/cat_1.jpg', 'assets/cat_2.jpg', 'assets/cat_3.jpg'];
loadCursor = 0;
scale = 1;
transform: ImageTransform = {
translateUnit: 'px',
};
cropper: CropperPosition = {
x1: 0,
y1: 0,
x2: 160,
y2: 160,
};
loading = false;
cacheTransform: ImageTransform = {
translateUnit: 'px',
};
cacheCropper: CropperPosition = {
x1: 0,
y1: 0,
x2: 160,
y2: 160,
};
constructor(private sanitizer: DomSanitizer) {}
loadImage() {
if (this.loadCursor < this.imageUrls.length) {
this.imageUrl = this.imageUrls[this.loadCursor];
}
}
addCropped() {
const event = this.imageCropperRef?.crop('base64'); // Using Blob will return a Promise.
if (event) {
const url = this.sanitizer.bypassSecurityTrustUrl(event.objectUrl || event.base64 || '');
this.croppedImages.push(url);
}
}
cropperReady() {
if (this.loading) {
this.transform = {...this.cacheTransform};
this.cropper = {...this.cacheCropper};
setTimeout(() => {
// If there is no setTimeout, the changes to the cropper will not take effect.
this.addCropped();
this.loadNextImage();
}, 0);
}
}
loadNextImage() {
this.loadCursor++;
if (this.loadCursor >= this.imageUrls.length) {
this.loadCursor = 0;
this.loading = false;
}
this.loadImage();
}
zoomOut() {
this.scale -= 0.1;
this.transform = {
...this.transform,
scale: this.scale,
};
}
zoomIn() {
this.scale += 0.1;
this.transform = {
...this.transform,
scale: this.scale,
};
}
crop() {
this.loading = true;
this.croppedImages = [];
this.cacheTransform = {...this.transform};
this.cacheCropper = {...this.cropper};
this.addCropped();
this.loadNextImage();
}
}
from ngx-image-cropper.
not sure why that's happening but happy you found a solution for it
from ngx-image-cropper.
Related Issues (20)
- How to change the default cropper position? HOT 2
- Feature request: Image moving speed definition HOT 1
- base 64 output is empty HOT 6
- crop() should be a Promise HOT 2
- The 'imageCropper' property has no initializer and is definitely not assigned in the constructor. HOT 1
- Feature request: add cropping svg format HOT 1
- Image is not updated in the Cropper only when i uses Drag and Drop functionality HOT 2
- Output as SVG HOT 1
- Support for outputformat AVIF HOT 2
- Not displaying IONIC angular HOT 1
- HammerJs and Change Detection HOT 1
- Disabling image smoothing algorithm HOT 4
- How to disable this warn: Could not find HammerJS - Pinch Gesture won't work
- Usage of `data:` regarding Content Security Policy (CSP) HOT 1
- Feature request - please help HOT 1
- Transparent background is converted to Black when using jpeg format HOT 1
- Feature Request: Pinch Zoom image HOT 2
- move image into zone crop HOT 1
- Give radius for round cropper HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-image-cropper.