Mosaico Cropper now supports both modern JavaScript (no jQuery required) and jQuery (for backward compatibility).
No jQuery required! Use the modern API for better performance and smaller bundle size.
-
Include Files: Add the CSS and JavaScript files to your HTML.
<!-- Mosaico Cropper (no dependencies required) --> <link rel="stylesheet" href="/path/to/dist/jqueryui-mosaico-cropper.min.css"> <script src="/path/to/dist/jqueryui-mosaico-cropper.min.js"></script>
-
Initialize with Modern API:
// ES6 Module import { createMosaicoCropper } from 'mosaico-cropper'; // Create cropper instance const cropper = createMosaicoCropper('#my-image', { width: 400, height: 300, urlAdapter: 'cloudimage' }); // Or using vanilla JavaScript (UMD) const cropper = MosaicoCropper.createMosaicoCropper('#my-image', { width: 400, height: 300, urlAdapter: 'cloudimage' });
-
Advanced Usage:
import { MosaicoCropperPlugin, getMosaicoCropper } from 'mosaico-cropper'; // Class-based approach const cropper = new MosaicoCropperPlugin('#my-image', options); // Method calls cropper.scale(1.5); cropper.cropHeight(300); // Get existing instance const existingCropper = getMosaicoCropper(document.getElementById('my-image')); // Event handling document.getElementById('my-image').addEventListener('mosaicocropperready', (e) => { console.log('Cropper is ready!'); }); // Cleanup cropper.destroy();
If you prefer to use jQuery, the plugin automatically registers itself when jQuery is available.
-
Include Files: Add jQuery and the Mosaico Cropper files.
<!-- Optional: jQuery (only if you want to use jQuery API) --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- Mosaico Cropper --> <link rel="stylesheet" href="/path/to/dist/jqueryui-mosaico-cropper.min.css"> <script src="/path/to/dist/jqueryui-mosaico-cropper.min.js"></script>
-
Initialize with jQuery:
$(function() { $('#my-image').mosaicoCropper({ width: 400, height: 300, urlAdapter: 'cloudimage' }); // Method calls $('#my-image').mosaicoCropper('scale', 1.5); $('#my-image').mosaicoCropper('cropHeight', 300); // Get values const currentScale = $('#my-image').mosaicoCropper('scale'); });
autoClose(boolean, default:true): Whether the cropper should automatically close when losing focusshiftWheel(boolean, default:false): When true, wheel zoom only works when Shift key is pressedautoZoom(boolean, default:undefined): Controls automatic zooming behavior during resize operationsmaxScale(number, default:2): Maximum scale factor for zooming
width(number): Target width for the cropped imageheight(number): Target height for the cropped imagecropX(number): Starting X coordinate for crop areacropY(number): Starting Y coordinate for crop areacropWidth(number): Width of the crop areacropHeight(number): Height of the crop areacropX2(number): End X coordinate for crop area (alternative to cropWidth)cropY2(number): End Y coordinate for crop area (alternative to cropHeight)offsetX(number): X offset for image positioningoffsetY(number): Y offset for image positioningresizeWidth(number): Target width for resize operationsresizeHeight(number): Target height for resize operationsppp(number): Pixels per point ratio for high-DPI displays (automatically calculated)
-
urlAdapter(string or object): Specifies which image service adapter to use. Built-in adapters include:'cloudinary'- Cloudinary image service'cloudimage'- Cloudimage service'imagekit'- ImageKit service'sirv'- Sirv service'gumlet'- Gumlet service'uploadcare'- Uploadcare service'thumbor'- Thumbor service'filestack'- Filestack service'mosaico'- Default Mosaico adapter- Custom object with
fromSrc,toSrc, and optionaldefaultPrefixproperties
-
urlPrefix(string): URL prefix for image transformations -
urlPostfix(string): URL postfix/suffix for image transformations -
urlOriginal(string): Original image URL before transformations
containerSelector(string): CSS selector for the container element that should receive thecropper-croppingclassimgLoadingClass(string): CSS class to add to the image element during loading operations
For custom URL adapters, you can provide an object with the following properties:
$('#my-image').mosaicoCropper({
urlAdapter: {
// Pattern for parsing existing URLs (string or object with method-specific patterns)
fromSrc: "{urlPrefix}/crop/{width}x{height}/{urlOriginal}",
// Template for generating new URLs (string or object with method-specific templates)
toSrc: "{urlPrefix}/crop/{width}x{height}/{urlOriginal}",
// Default prefix for URLs that don't match the fromSrc pattern
defaultPrefix: "https://your-image-service.com/"
}
});Basic Usage:
$('#image').mosaicoCropper({
width: 400,
height: 300,
urlAdapter: 'cloudimage'
});Advanced Configuration:
$('#image').mosaicoCropper({
width: 800,
height: 600,
maxScale: 3,
autoClose: false,
shiftWheel: true,
containerSelector: '.image-container',
imgLoadingClass: 'loading',
urlAdapter: {
fromSrc: "{urlPrefix}/transform/{width}x{height}/{urlOriginal}",
toSrc: "{urlPrefix}/transform/{width}x{height}/{urlOriginal}",
defaultPrefix: "https://cdn.example.com/"
}
});Predefined Crop Area:
$('#image').mosaicoCropper({
width: 400,
height: 300,
cropX: 100,
cropY: 50,
cropWidth: 200,
cropHeight: 150,
urlAdapter: 'cloudinary'
});After initialization, you can call methods on the cropper instance:
// Get current scale
var scale = $('#image').mosaicoCropper('scale');
// Set scale
$('#image').mosaicoCropper('scale', 1.5);
// Get crop height
var height = $('#image').mosaicoCropper('cropHeight');
// Set crop height
$('#image').mosaicoCropper('cropHeight', 400);
// Destroy the cropper
$('#image').mosaicoCropper('destroy');The cropper triggers custom events during operation:
$('#image').on('mosaicocropperready', function(event) {
console.log('Cropper is ready');
});
$('#image').on('mosaicocropperheight', function(event) {
console.log('Crop height changed:', event.detail.value);
});| Version | Bundle Size | Dependencies | Reduction |
|---|---|---|---|
| Modern (No jQuery) | ~35KB | None | Baseline |
| Legacy (With jQuery) | ~85KB | jQuery required | +58% larger |
- ๐ Faster DOM Operations: Native DOM APIs are optimized by browsers
- ๐พ Lower Memory Usage: No jQuery overhead or event delegation layers
- โก Better Tree Shaking: Modern bundlers can eliminate unused code
- ๐ฆ Smaller Parse Time: Less JavaScript to parse and compile
You can migrate gradually:
- Phase 1: Keep using jQuery API while upgrading to the new version
- Phase 2: Start using modern API for new features
- Phase 3: Replace existing jQuery calls with modern API
- Phase 4: Remove jQuery dependency entirely
// Before (jQuery only)
$('#image').mosaicoCropper(options);
// After (Modern API)
const cropper = createMosaicoCropper('#image', options);The modern API works seamlessly with popular frameworks:
// React
import { createMosaicoCropper } from 'mosaico-cropper';
function ImageCropper({ imageRef, options }) {
useEffect(() => {
const cropper = createMosaicoCropper(imageRef.current, options);
return () => cropper.destroy();
}, []);
}
// Vue
import { createMosaicoCropper } from 'mosaico-cropper';
export default {
mounted() {
this.cropper = createMosaicoCropper(this.$refs.image, this.options);
},
beforeDestroy() {
this.cropper?.destroy();
}
}
// Angular
import { createMosaicoCropper } from 'mosaico-cropper';
@Component({...})
export class ImageCropperComponent {
ngAfterViewInit() {
this.cropper = createMosaicoCropper(this.imageEl.nativeElement, this.options);
}
ngOnDestroy() {
this.cropper?.destroy();
}
}See the demo