Home

Awesome

Croppr.js

A vanilla JavaScript image cropper that's lightweight, awesome, and has absolutely zero dependencies.

Try it out in the demo →

Installation

Via NPM:

npm install croppr -—save
// CommonJS
var Croppr = require('croppr');

// ES6 import
import Croppr from 'croppr';

Note: Don't forget to bundle or include croppr.css!

Via Bower:

bower install croppr

Then include via script tag below.

Via script tag:

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

Basic Usage

In your HTML document:

<img src="path/to/image.jpg" id="croppr"/>

In your JavaScript file:

var cropInstance = new Croppr('#croppr', {
  // ...options
});

Protip: You can also pass an Element object directly instead of a selector.

To retrieve crop region:

var data = cropInstance.getValue();
// data = {x: 20, y: 20: width: 120, height: 120}

Options

aspectRatio

Constrain the crop region to an aspect ratio.

maxSize

Constrain the crop region to a maximum size.

Note: unit accepts a value of 'px' or '%'. Defaults to 'px'.

minSize

Constrain the crop region to a minimum size.

Note: unit accepts a value of 'px' or '%'. Defaults to 'px'.

startSize

The starting size of the crop region when it is initialized.

Note: unit accepts a value of 'px' or '%'. Defaults to '%'.

onCropStart

A callback function that is called when the user starts modifying the crop region.

onCropStart: function(data) {
  console.log(data.x, data.y, data.width, data.height);
}

onCropMove

A callback function that is called when the crop region changes.

onCropMove: function(data) {
  console.log(data.x, data.y, data.width, data.height);
}

onCropEnd

A callback function that is called when the user stops modifying the crop region.

onCropEnd: function(data) {
  console.log(data.x, data.y, data.width, data.height);
}

onInitialize

A callback function that is called when the Croppr instance is fully initialized.

onInitialize: function(instance) {
  // do things here
}

returnMode

Define how the crop region should be calculated.

Methods

getValue(returnMode?: string)

Returns the value of the crop region. returnMode inherits from options by default. Refer to returnMode for possible values.

var value = cropInstance.getValue();
// value = {x: 21, y: 63: width: 120, height: 120}

var ratio = cropInstance.getValue('ratio');
// value = {x: 0.1, y: 0.3: width: 0.57, height: 0.57}

destroy()

Destroys the Croppr instance and restores the original img element.

setImage(src: string)

Changes the image src. Returns the Croppr instance.

moveTo(x: number, y: number)

Moves the crop region to the specified coordinates. Returns the Croppr instance.

resizeTo(width: number, height: number, origin?: Array)

Resizes the crop region to the specified size. origin is an optional argument that specifies the origin point (in ratio) to resize from in the format of [x, y]. Defaults to [0.5, 0.5] (center). Returns the Croppr instance.

scaleBy(factor: number, origin?: Array)

Scales the crop region by a factor. origin is an optional argument that specifies the origin point (in ratio) to resize from in the format of [x, y]. Defaults to [0.5, 0.5] (center). Returns the Croppr instance.

reset()

Resets the crop region to its original position and size. Returns the Croppr instance.


Build Status

Copyright © 2018 James Ooi. Released under the MIT License.