bbc/cropperjs

Name: cropperjs

Owner: BBC

Description: JavaScript image cropper.

Forked from: fengyuanchen/cropperjs

Created: 2017-04-13 07:47:50.0

Updated: 2017-10-05 00:39:45.0

Pushed: 2017-04-13 09:03:32.0

Homepage: https://fengyuanchen.github.io/cropperjs

Size: 3312

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Cropper.js

JavaScript image cropper.

Build Status Images

Table of contents
Features
Main
/
cropper.css     ( 5 KB)
cropper.min.css ( 4 KB)
cropper.js      (92 KB)
cropper.min.js  (29 KB)
Getting started
Quick start

Four quick start options are available:

Installation

Include files:

k  href="/path/to/cropper.css" rel="stylesheet">
ipt src="/path/to/cropper.js"></script>
Usage

Initialize with Cropper constructor:

 Wrap the image or canvas element with a block element (container) -->
>
mg id="image" src="picture.jpg">
v>
ss
imit image width to avoid overflow the container */
{
x-width: 100%;

s
image = document.getElementById('image');
cropper = new Cropper(image, {
pectRatio: 16 / 9,
op: function(e) {
console.log(e.detail.x);
console.log(e.detail.y);
console.log(e.detail.width);
console.log(e.detail.height);
console.log(e.detail.rotate);
console.log(e.detail.scaleX);
console.log(e.detail.scaleY);


FAQ

See the FAQ documentation.

Notes Known issues

? back to top

Options

You may set cropper options with new Cropper(image, options). If you want to change the global default options, You may use Cropper.setDefaults(options).

viewMode

Define the view mode of the cropper.

dragMode

Define the dragging mode of the cropper.

aspectRatio

Set the aspect ratio of the crop box. By default, the crop box is free ratio.

data

The previous cropped data if you had stored, will be passed to setData method automatically when built.

preview

Add extra elements (containers) for previewing.

Notes:

responsive

Re-render the cropper when resize the window.

restore

Restore the cropped area after resize the window.

checkCrossOrigin

Check if the current image is a cross-origin image.

If it is, when clone the image, a crossOrigin attribute will be added to the cloned image element and a timestamp will be added to the src attribute to reload the source image to avoid browser cache error.

By adding crossOrigin attribute to image will stop adding timestamp to image url, and stop reload of image.

checkOrientation

Check the current image's Exif Orientation information.

More exactly, read the Orientation value for rotating or flipping the image, and then override the Orientation value with 1 (the default value) to avoid some issues (1, 2) on iOS devices.

Requires Typed Arrays support (IE 10+).

modal

Show the black modal above the image and under the crop box.

guides

Show the dashed lines above the crop box.

center

Show the center indicator above the crop box.

highlight

Show the white modal above the crop box (highlight the crop box).

background

Show the grid background of the container.

autoCrop

Enable to crop the image automatically when initialize.

autoCropArea

A number between 0 and 1. Define the automatic cropping area size (percentage).

movable

Enable to move the image.

rotatable

Enable to rotate the image.

scalable

Enable to scale the image.

zoomable

Enable to zoom the image.

zoomOnTouch

Enable to zoom the image by dragging touch.

zoomOnWheel

Enable to zoom the image by wheeling mouse.

wheelZoomRatio

Define zoom ratio when zoom the image by wheeling mouse.

cropBoxMovable

Enable to move the crop box by dragging.

cropBoxResizable

Enable to resize the crop box by dragging.

toggleDragModeOnDblclick

Enable to toggle drag mode between “crop” and “move” when click twice on the cropper.

minContainerWidth

The minimum width of the container.

minContainerHeight

The minimum height of the container.

minCanvasWidth

The minimum width of the canvas (image wrapper).

minCanvasHeight

The minimum height of the canvas (image wrapper).

minCropBoxWidth

The minimum width of the crop box.

Note: This size is relative to the page, not the image.

minCropBoxHeight

The minimum height of the crop box.

Note: This size is relative to the page, not the image.

build

A shortcut of the “build” event.

built

A shortcut of the “built” event.

cropstart

A shortcut of the “cropstart” event.

cropmove

A shortcut of the “cropmove” event.

cropend

A shortcut of the “cropend” event.

crop

A shortcut of the “crop” event.

zoom

A shortcut of the “zoom” event.

? back to top

Methods

As there is an asynchronous process when load the image, you should call most of the methods after built, except “setAspectRatio”, “replace” and “destroy”.

If a method doesn't need to return any value, it will return the cropper instance (this) for chain composition.

Cropper(image, {
ilt: function () {
// this.cropper[method](argument1, , argument2, ..., argumentN);
this.cropper.move(1, -1);

// Allows chain composition
this.cropper.move(1, -1).rotate(45).scale(1, -1);


crop()

Show the crop box manually.

Cropper(image, {
toCrop: false,
ilt: function () {
// Do something here
// ...

// And then
this.cropper.crop();


reset()

Reset the image and crop box to their initial states.

clear()

Clear the crop box.

replace(url[, onlyColorChanged])

Replace the image's src and rebuild the cropper.

enable()

Enable (unfreeze) the cropper.

disable()

Disable (freeze) the cropper.

destroy()

Destroy the cropper and remove the instance from the image.

move(offsetX[, offsetY])

Move the canvas (image wrapper) with relative offsets.

per.move(1);
per.move(1, 0);
per.move(0, -1);
moveTo(x[, y])

Move the canvas (image wrapper) to an absolute point.

zoom(ratio)

Zoom the canvas (image wrapper) with a relative ratio.

per.zoom(0.1);
per.zoom(-0.1);
zoomTo(ratio)

Zoom the canvas (image wrapper) to an absolute ratio.

per.zoomTo(1); // 1:1 (canvasData.width === canvasData.naturalWidth)
rotate(degree)

Rotate the canvas (image wrapper) with a relative degree.

Requires CSS3 2D Transforms support (IE 9+).

per.rotate(90);
per.rotate(-90);
rotateTo(degree)

Rotate the canvas (image wrapper) to an absolute degree.

scale(scaleX[, scaleY])

Scale the image.

Requires CSS3 2D Transforms support (IE 9+).

per.scale(-1); // Flip both horizontal and vertical
per.scale(-1, 1); // Flip horizontal
per.scale(1, -1); // Flip vertical
scaleX(scaleX)

Scale the abscissa of the image.

scaleY(scaleY)

Scale the ordinate of the image.

getData([rounded])

Output the cropped area position and size data (base on the original image).

A schematic diagram for data's properties

setData(data)

Change the cropped area position and size with new data (base on the original image).

Note: Only available in strict mode.

getContainerData()

Output the container size data.

A schematic diagram for cropper's layers

getImageData()

Output the image position, size and other related data.

getCanvasData()

Output the canvas (image wrapper) position and size data.

imageData = cropper.getImageData();
canvasData = cropper.getCanvasData();

imageData.rotate % 180 === 0) {
nsole.log(canvasData.naturalWidth === imageData.naturalWidth); // true

setCanvasData(data)

Change the canvas (image wrapper) position and size with new data.

getCropBoxData()

Output the crop box position and size data.

setCropBoxData(data)

Change the crop box position and size with new data.

getCroppedCanvas([options])

Get a canvas drawn the cropped image.

After then, you can display the canvas as an image directly, or use HTMLCanvasElement.toDataURL to get a Data URL, or use HTMLCanvasElement.toBlob to get a blob and upload it to server with FormData if the browser supports these APIs.

per.getCroppedCanvas();

per.getCroppedCanvas({
dth: 160,
ight: 90


pload cropped image to server if the browser supports `HTMLCanvasElement.toBlob`
per.getCroppedCanvas().toBlob(function (blob) {
r formData = new FormData();

rmData.append('croppedImage', blob);

 Use `jQuery.ajax` method
ajax('/path/to/upload', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function () {
  console.log('Upload success');
},
error: function () {
  console.log('Upload error');
}
;

setAspectRatio(aspectRatio)

Change the aspect ratio of the crop box.

setDragMode([mode])

Change the drag mode.

Tips: You can toggle the “crop” and “move” mode by double click on the cropper.

? back to top

Events
build

This event fires when a cropper instance starts to load an image.

built

This event fires when a cropper instance has built completely.

cropper;

e.addEventListener('built', function () {
nsole.log(this.cropper === cropper);
 -> true


per = new Cropper(image);
cropstart

This event fires when the canvas (image wrapper) or the crop box starts to change.

e.addEventListener('cropstart', function (e) {
nsole.log(e.detail.originalEvent);
nsole.log(e.detail.action);

cropmove

This event fires when the canvas (image wrapper) or the crop box is changing.

cropend

This event fires when the canvas (image wrapper) or the crop box stops to change.

crop

About these properties, see the getData method.

This event fires when the canvas (image wrapper) or the crop box changed.

zoom

This event fires when a cropper instance starts to zoom in or zoom out its canvas (image wrapper).

e.addEventListener('zoom', function (e) {

 Zoom in
 (e.detail.ratio > e.detail.oldRatio) {
e.preventDefault(); // Prevent zoom in


 Zoom out
 ...

? back to top

No conflict

If you have to use other cropper with the same namespace, just call the Cropper.noConflict static method to revert to it.

ipt src="other-cropper.js"></script>
ipt src="cropper.js"></script>
ipt>
opper.noConflict();
 Code that uses other `Cropper` can follow here.
ript>
Browser support
Contributing

Please read through our contributing guidelines.

Versioning

Maintained under the Semantic Versioning guidelines.

License

MIT © Fengyuan Chen

Related projects

? back to top


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.