You can override the default Cropper JavaScript module behavior by executing a script that replaces the standard event handlers. For example, to implement custom zoom constraints (preventing zoom outside a specific ratio), you can override window.cropper.onZoom.
Example: Overriding onZoom in JavaScript
window.overrideOnZoomCropperEvent = (minZoomRatio, maxZoomRatio) => {
window.cropper.onZoom = function (imageObject, event, correlationId) {
const jSEventData = this.getJSEventData(event, correlationId);
const isApplyPreventZoomMinRatio = (minZoomRatio != null) && (minZoomRatio > event.detail.ratio);
const isApplyPreventZoomMaxRatio = (maxZoomRatio != null) && (event.detail.ratio > maxZoomRatio);
if (isApplyPreventZoomMinRatio || isApplyPreventZoomMaxRatio) {
event.preventDefault();
}
else {
imageObject.invokeMethodAsync('CropperIsZoomed', jSEventData);
}
};
};
To apply these overrides from your C# code, use the IJSRuntime:
await JSRuntime!.InvokeVoidAsync("window.overrideCropperJsInteropModule", MinZoomRatio, MaxZoomRatio);