You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone, I need to crop my image based on my file input, but the cropper is not allowing me to edit the image. I want to know if it is possible to do something like this to update the cropper:
import{Controller}from'@hotwired/stimulus';importCropperfrom'cropperjs';importCropEvent=Cropper.CropEvent;exportdefaultclassCropperControllerextendsController{declarereadonlypublicUrlValue: string;declarereadonlyoptionsValue: object;declareimg: HTMLImageElement;declarecropper: Cropper;staticvalues={publicUrl: String,options: Object,img: HTMLImageElement,cropper: Cropper};connect(){this.initializeCropper();}initializeCropper(){// Remove any existing cropper image and instance before initializingif(this.img){this.img.remove();}if(this.cropper){this.cropper.destroy();}// Create image viewthis.img=document.createElement('img');this.img.classList.add('cropperjs-image');this.img.src=this.publicUrlValue;// The current image URL valueconstparent=this.element.parentNode;if(!parent){thrownewError('Missing parent node for Cropperjs');}parent.appendChild(this.img);// Options for the cropper instanceconstoptions=this.optionsValue;// Dispatch pre-connect eventthis.dispatchEvent('pre-connect',{ options,img: this.img});// Build the cropper with the given image and optionsthis.cropper=newCropper(this.img,options);// Event listener to update the input value on cropthis.img.addEventListener('crop',(event)=>{(this.elementasHTMLInputElement).value=JSON.stringify((eventasCropEvent).detail);});// Dispatch connect eventthis.dispatchEvent('connect',{cropper: this.cropper, options,img: this.img});}// Method to update the image URL and reinitialize the cropperupdateImage(newImageUrl){this.img.src=newImageUrl;// Update the public URL valuethis.initializeCropper();// Reinitialize the cropper with the new image}disconnect(){if(this.cropper){this.cropper.destroy();}if(this.img){this.img.remove();}}// Helper method to dispatch eventsprivatedispatchEvent(name: string,payload: any){this.dispatch(name,{detail: payload,prefix: 'cropperjs'});}}
The text was updated successfully, but these errors were encountered:
I’m not entirely sure about the expected behavior you’re aiming for.
The current CropperJS implementation is designed to crop an existing image, and adapting it to work with non-uploaded images would require quite a few changes to its code. :/
// Method to update the image URL and reinitialize the cropperupdateImage(newImageUrl){this.img.src=newImageUrl;// Update the public URL valuethis.initializeCropper();// Reinitialize the cropper with the new image}
This snippet wouldn’t work as expected because this.img gets reset right after the line // Create image view. I suppose you meant to reference this.publicUrl instead?
If you need this kind of behavior, one possible solution could be to set the initial HTML inside a <template> tag and dynamically create the <div> whenever you have a new imageUrl.
Alternatively, maybe consider a custom controller? You could use the various events dispatched by the UX controller?
Hi everyone, I need to crop my image based on my file input, but the cropper is not allowing me to edit the image. I want to know if it is possible to do something like this to update the cropper:
The text was updated successfully, but these errors were encountered: