Cropme is a customizable and easy to use javascript image cropper plugin.
Support:
dist/
├── cropme.css
├── cropme.min.css (compressed)
├── cropme.js (UMD)
└── cropme.min.js (UMD, compressed)
npm
npm install cropme
Download
Download the project and extract it.\ then put the dist/cropme.min.css and the dist/cropme.min.js in you project.
<link rel="stylesheet" href="https://github.com/shpontex/cropme/blob/master/path-to/cropme.min.css">
<script src="https://github.com/shpontex/cropme/raw/master/path-to/cropme.min.js"></script>
CDN
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cropme@latest/dist/cropme.min.css">
<script src="https://cdn.jsdelivr.net/npm/cropme@latest/dist/cropme.min.js"></script>
new Cropme(element, options);
element (HTMLElement
, required): *the cropper wrapping HTML element, can be a <div>
or <img>
tag.
options (Object
, optional): The configuration options, see Options.
Vanilla javascript
<div id="container"></div>
<script>
var element = document.getElementById('container');
var cropme = new Cropme(element);
cropme.bind({
url: 'images/naruto.jpg'
});
</script>
<!-- or use image tag -->
<img src="https://github.com/shpontex/cropme/raw/master/images/naruto.jpg" id="myImage" />
<script>
var element = document.getElementById('myImage');
new Cropme(element);
</script>
JQuery
<div id="container"></div>
<script>
var example = $('#container').cropme();
example.cropme('bind', {
url: 'images/naruto.jpg'
});
</script>
<!-- or use image tag -->
<img src="https://github.com/shpontex/cropme/raw/master/images/naruto.jpg" id="myImage" />
<script>
$('#myImage').cropme();
</script>
container
int|string
, default: 300
): the outer container widthint
, default: 300
): the outer container height// Fixed container
container: {
width: 500,
height: 400
}
// responsive container
container: {
width: '100%',
height: 400
}
viewport
int
, default: 100
): the viewport widthint
, default: 100
): the viewport heightstring
, default: square
, available: circle
): the viewport frame formobject
): the viewport frame borderbool
, default: true
): toggle the borderint
, default: 2
): the border widthstring
, unit: hex, rgba, hsl
, default: #fff
): the border colorviewport: {
width: 100,
height: 100,
type: 'circle',
border: {
enable: true,
width: 2,
color: '#fff'
}
}
zoom
,number
, default: 0.01
): minimum zoomnumber
, default: 3
): maximum zoombool
, default: true
): enable or disable the zoom featurebool
, default: true
): enable or disable mouse wheel zoombool
, default: false
): toggle the slider inputzoom: {
min: 0.01,
max: 3,
enable: true,
mouseWheel: true,
slider: false
}
rotation
bool
, default: true
): enable or disable the rotationbool
, default: false
): toggle the slider inputstring
, default: right
, available: right, left
): the slider input positionrotation: {
enable: true,
slider: false,
position: 'right'
}
string
, default: viewport
,available: image
, viewport
)\
image: the transform origin is the image center\
viewport: the transform origin is the viewport center{
transformOrigin: 'viewport'
}
string
, default: null
): the class of the container{
customClass: 'my-custom-class'
}
Binds an image and return a promise after the image is loaded.
The bind()
method expects an Object
containing:
String
int
,the x translation coordinate).int
,the y translation coordinate).\
The image is translated from its origin.\
If not specified, the image is centered horizontaly and verticaly.float
,The scale of the image, 1 is the original image size),\
If not specified, the image will takes the container's height and scale automatically.int
,The rotation of the image by an angle in degree around its origin).object
,The x and y coordonate of the image transform origin),\
if origin is set, the transformOrigin
option will be override and set to viewport
,\
since image
option means that the transform origin is the center of the image,\
in that case origin
is not required.var container = $('#container').cropme();
container.cropme('bind', {
url: 'images/naruto.jpg',
position: {
x: 230,
y: -30,
scale: 1.3,
angle: 35,
origin: {
x: 623.26,
y: 1150
}
},
});
// If you want to do some changes directly after binding the image
container.cropme('bind', {
url: 'images/naruto.jpg',
})
.then(function(){
//example
container.cropme('rotate')
});
Rotate the image to the given angle.
number
degree
var myImage = $('#myImage').cropme();
myImage.cropme('rotate', 90);
Returns a promise with the cropped image.
As a parameter, the crop()
function can receive:
Object
containing:
String
base64
base64
, blob
int
number
scale
has a value, the width
get ignored.String
image/png
number
0.92
(0.80
for image/webp
mimetype when output is blob).0
and 1
indicating image quality.\
Works only with image/jpeg
or image/webp
(formats that use lossy
compression).A String
specifying the exportation format (base64
or blob
)
For more information about mimetype
and quality
arguments: \
→ toBlob()
and toDataURL()
HTMLCanvasElement documentation.
Calling crop()
without parameters returns a base64 image with the viewport size.
var myImage = $('#myImage').cropme();
// string
myImage.cropme('crop', 'blob')
.then(function(output) {
// here you can use the blob output
});
// object
myImage.cropme('crop', {
type: 'base64',
width: 800
}).then(function(output) {
// here you can use the base64 output
});
// no parameter
myImage.cropme('crop')
.then(function(output) {
// here you can use the base64 output
});
Returns an object specifying the image position\ When you create a new cropme you can bind the image with this position object.
var myImage = $('#myImage').cropme();
var position = myImage.cropme('position');
Output: Object
{
x: 230,
y: -30,
scale: 1.3,
angle: 35,
origin: {
x: 623.26,
y: 1150
}
}
Reload the cropme instance with a new parameters
var myImage = $('#myImage').cropme({
container: {
width: 300,
height: 200
}
});
myImage.cropme('reload', {
container: {
width: 455,
height: 600
},
viewport: {
width: 150,
height: 240,
border: {
enable: true,
width: 5,
color: '#f00'
}
}
});
Destroy the cropme instance
var myImage = $('#myImage').cropme();
myImage.cropme('destroy');
Thank you for your contribution to this project.
Fork the project then
npm install && npm run watch