Press "Enter" to skip to content

Feature-Rich Image With Pure JavaScript | Cropper.js

Cropper.js is a pure JavaScript version of the jQuery Image Cropper plugin that provides feature-rich image crop functionality on any image.

Main options:

  • A friendly touch.
  • Canvas powered by HTML5.
  • Image rotate, zoom, flip, and move.
  • Via the browser.
  • Tons of options, styles, and events.

How to make use of it:

Install & download the Cropper.js library.

# NPM
$ npm install cropperjs --save

Import the next JavaScript and CSS information into the HTML file.

<link href="/path/to/cropper.min.css" rel="stylesheet">
<script src="/path/to/cropper.min.js"></script>

Wrap your picture or canvas factor right into a container factor.

<div>
  <img id="image" src="image-to-crop.jpg">
</div>

Enable the JavaScript picture cropper on the picture.

var image = document.getElementById('image');
var myCropper = new Cropper(image, {
    // options here
  }
});

All customization choices with default values.

var image = document.getElementById('image');
var myCropper = new Cropper(image, {

    // The view mode of the cropper
    viewMode: 0, // 0, 1, 2, 3

    // The dragging mode of the cropper
    dragMode: DRAG_MODE_CROP, // 'crop', 'move' or 'none'

    // The initial aspect ratio of the crop box
    initialAspectRatio: NaN,

    // The aspect ratio of the crop box
    aspectRatio: NaN,

    // An object with the previous cropping result data
    data: null,

    // A selector for adding extra containers to preview
    preview: '',

    // Re-render the cropper when resize the window
    responsive: true,

    // Restore the cropped area after resize the window
    restore: true,

    // Check if the current image is a cross-origin image
    checkCrossOrigin: true,

    // Check the current image's Exif Orientation information
    checkOrientation: true,

    // Show the black modal
    modal: true,

    // Show the dashed lines for guiding
    guides: true,

    // Show the center indicator for guiding
    center: true,

    // Show the white modal to highlight the crop box
    highlight: true,

    // Show the grid background
    background: true,

    // Enable to crop the image automatically when initialize
    autoCrop: true,

    // Define the percentage of automatic cropping area when initializes
    autoCropArea: 0.8,

    // Enable to move the image
    movable: true,

    // Enable to rotate the image
    rotatable: true,

    // Enable to scale the image
    scalable: true,

    // Enable to zoom the image
    zoomable: true,

    // Enable to zoom the image by dragging touch
    zoomOnTouch: true,

    // Enable to zoom the image by wheeling mouse
    zoomOnWheel: true,

    // Define zoom ratio when zoom the image by wheeling mouse
    wheelZoomRatio: 0.1,

    // Enable to move the crop box
    cropBoxMovable: true,

    // Enable to resize the crop box
    cropBoxResizable: true,

    // Toggle drag mode between "crop" and "move" when click twice on the cropper
    toggleDragModeOnDblclick: true,

    // Size limitation
    minCanvasWidth: 0,
    minCanvasHeight: 0,
    minCropBoxWidth: 0,
    minCropBoxHeight: 0,
    minContainerWidth: 200,
    minContainerHeight: 100,

    // Shortcuts of events
    ready: null,
    cropstart: null,
    cropmove: null,
    cropend: null,
    crop: null,
    zoom: null
    
  }
});

API strategies.

var image = document.getElementById('image');
var myCropper = new Cropper(image);

// enables the crop box
myCropper.enable();

// disables the crop box
myCropper.disable();

// destroys the crop box
myCropper.destroy();

// shows the crop box
myCropper.crop();

// resets the cropper
myCropper.reset();

// clears the crop box
myCropper.clear();

// replaces the image source
myCropper.replace(url, hasSameSize);

// moves the image wrapper
myCropper.move(X, Y);

// moves to a specific point
myCropper.moveTo(X, Y);

// zooms the image
myCropper.zoom(ratio);

// zooms to
myCropper.zoomTo(ratio, pivot);

// rotates the image
myCropper.rotate(degree);

// rotates to
myCropper.rotateTo(degree);

// scales the image
myCropper.scale(scaleX, scaleY);
myCropper.scaleX(scaleX);
myCropper.scaleY(scaleY);

// gets the cropper data
myCropper.getData(rounded);

// sets the cropper data
myCropper.setData(data);

// gets container's data
myCropper.getContainerData();

// gets image data
myCropper.getImageData();

// gets canvas data
myCropper.getCanvasData();

// sets canvas data
myCropper.setCanvasData(data);

// gets data of crop box
myCropper.getCropBoxData();

// set box data;
myCropper.setCropBoxData(data);

/*
  width: the destination width of the output canvas.
  height: the destination height of the output canvas.
  minWidth: the minimum destination width of the output canvas, the default value is 0.
  minHeight: the minimum destination height of the output canvas, the default value is 0.
  maxWidth: the maximum destination width of the output canvas, the default value is Infinity.
  maxHeight: the maximum destination height of the output canvas, the default value is Infinity.
  fillColor: a color to fill any alpha values in the output canvas, the default value is transparent.
  imageSmoothingEnabled: set to change if images are smoothed (true, default) or not (false).
  imageSmoothingQuality: set the quality of image smoothing, one of "low" (default), "medium", or "high".
*/
myCropper.getCroppedCanvas(options);

// sets aspect ratio
myCropper.setAspectRatio(aspectRatio);

// sets drag mode: 'none', 'crop', 'move'
myCropper.setDragMode([mode])

Event handlers.

/*  Fires when a cropper instance starts to load a image.
 *
 *  event.detail.originalEvent:
 *  Type: Event
 *  Props: mousedown, touchstart and pointerdown
 *
 *  event.detail.action:
 *  Type: String
 *  Props:
 *  'crop': create a new crop box
 *  'move': move the canvas (image wrapper)
 *  'e': resize the east side of the crop box
 ** 'w': resize the west side of the crop box
 *  's': resize the south side of the crop box
 *  'n': resize the north side of the crop box
 *  'se': resize the southeast side of the crop box
 *  'sw': resize the southwest side of the crop box
 *  'ne': resize the northeast side of the crop box
 *  'nw': resize the northwest side of the crop box
 *  'all': move the crop box (all directions)
 */
image.addEventListener('cropstart', (event) => {
  console.log(event.detail.originalEvent);
  console.log(event.detail.action);
});

// Fires when the crop box is changing.
image.addEventListener('cropmove', (event) => {
  console.log(event.detail.originalEvent);
  console.log(event.detail.action);
});

// Fires when the crop box stops to change.
image.addEventListener('cropend', (event) => {
  console.log(event.detail.originalEvent);
  console.log(event.detail.action);
});

// Fires when cropping
image.addEventListener('cropend', (event) => {
  console.log(event.detail.x);
  console.log(event.detail.y);
  console.log(event.detail.width);
  console.log(event.detail.height);
  console.log(event.detail.rotate);
  console.log(event.detail.scaleX);
  console.log(event.detail.scaleY);
});

// Fires when zooming
image.addEventListener('zoom', (event) => {
  console.log(event.detail.originalEvent);
  console.log(event.detail.oldRatio);
  console.log(event.detail.ratio);
});

Feature-rich Image Cropper, Cropper.js Plugin/Github


See Demo And Download

Official Website(fengyuanchen): Click Here

This superior jQuery/javascript plugin is developed by fengyuanchen. For extra Advanced Usages, please go to the official website.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *