All Projects → jamesssooi → Croppr.js

jamesssooi / Croppr.js

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Croppr.js

Keen Slider
The HTML touch slider carousel with the most native feeling
Stars: ✭ 3,097 (+953.4%)
Mutual labels:  native, touch, vanilla
Rskimagecropper
An image cropper / photo cropper for iOS like in the Contacts app with support for landscape orientation.
Stars: ✭ 2,371 (+706.46%)
Mutual labels:  image, crop, cropper
Cropiwa
📐 Configurable Custom Crop widget for Android
Stars: ✭ 2,185 (+643.2%)
Mutual labels:  image, crop, cropper
Tocropviewcontroller
A view controller for iOS that allows users to crop portions of UIImage objects
Stars: ✭ 4,210 (+1331.97%)
Mutual labels:  image, crop, cropper
Silentbox
A lightbox inspired Vue.js component.
Stars: ✭ 196 (-33.33%)
Mutual labels:  image, component
Ddperspectivetransform
🔲 Warp image transformation
Stars: ✭ 186 (-36.73%)
Mutual labels:  image, crop
Igrphototweaks
Drag, Rotate, Scale and Crop
Stars: ✭ 212 (-27.89%)
Mutual labels:  image, crop
Medium Zoom
🔎🖼 A JavaScript library for zooming images like Medium
Stars: ✭ 2,799 (+852.04%)
Mutual labels:  image, vanilla
Gimage
A PHP library for easy image handling. 🖼
Stars: ✭ 148 (-49.66%)
Mutual labels:  image, crop
React Native Perspective Image Cropper
Perform custom crop, resizing and perspective correction 📐🖼
Stars: ✭ 223 (-24.15%)
Mutual labels:  image, crop
React Native Scalable Image
React Native Image component which scales width or height automatically to keep the original aspect ratio
Stars: ✭ 241 (-18.03%)
Mutual labels:  image, component
Flutter image editor
Flutter plugin, support android/ios.Support crop, flip, rotate, color martix, mix image, add text. merge multi images.
Stars: ✭ 181 (-38.44%)
Mutual labels:  image, crop
ngx-cropper
An Angular image plugin, includes upload, cropper, save to server.
Stars: ✭ 14 (-95.24%)
Mutual labels:  crop, cropper
react-drop-n-crop
An opinionated implementation of react-dropzone and react-cropper
Stars: ✭ 17 (-94.22%)
Mutual labels:  crop, cropper
Cropper
Android Library for cropping an image at ease.
Stars: ✭ 21 (-92.86%)
Mutual labels:  crop, cropper
Croperino
📷 A simple image cropping tool that provides gallery or camera help for Native Android (Java)
Stars: ✭ 176 (-40.14%)
Mutual labels:  image, crop
Android Imagecropview
android image crop library
Stars: ✭ 225 (-23.47%)
Mutual labels:  image, crop
pikaso
Seamless and headless HTML5 Canvas library
Stars: ✭ 23 (-92.18%)
Mutual labels:  crop, cropper
Image Focus
A dependency free utility for cropping images based on a focus point ~2.13kB gzipped
Stars: ✭ 134 (-54.42%)
Mutual labels:  image, cropper
React Avatar Editor
Small avatar & profile picture component. Resize and crop uploaded images using a intuitive user interface.
Stars: ✭ 1,846 (+527.89%)
Mutual labels:  image, crop

Croppr.js

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

  • Lightweight (<6kb minified and gzipped)
  • Made only with native, delicious vanilla JS
  • Zero dependencies
  • Supports touch devices!
  • Includes TypeScript typings!

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.

  • Type: Number
  • Default: null
  • Example: aspectRatio: 1 (Square)

maxSize

Constrain the crop region to a maximum size.

  • Type: [width, height, unit?]
  • Default: null
  • Example: maxSize: [50, 50, '%'] (A maximum size of 50% of the image size)

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

minSize

Constrain the crop region to a minimum size.

  • Type: [width, height, unit?]
  • Default: null
  • Example: minSize: [20, 20, 'px'] (A minimum width and height of 20px)

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

startSize

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

  • Type: [width, height, unit?]
  • Default: [100, 100, '%'] (A starting crop region as large as possible)
  • Example: startSize: [50, 50] (A starting crop region of 50% of the image size)

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.

  • Type: Function
  • Arguments: data = {x, y, width, height}
  • Example:
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.

  • Type: Function
  • Arguments: data = {x, y, width, height}
  • Example:
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.

  • Type: Function
  • Arguments: data = {x, y, width, height}
  • Example:
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.

  • Type: Function
  • Arguments: The Croppr instance
  • Example:
onInitialize: function(instance) {
  // do things here
}

returnMode

Define how the crop region should be calculated.

  • Type: String
  • Default: "real"
  • Possible values: "real", "ratio" or "raw"
    • real returns the crop region values based on the size of the image's actual sizes. This ensures that the crop region values are the same regardless if the Croppr element is scaled or not.
    • ratio returns the crop region values as a ratio between 0 to 1. e.g. For example, an x, y position at the center will be {x: 0.5, y: 0.5}.
    • raw returns the crop region values as is based on the size of the Croppr element.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].