
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
dnm-croppr
Advanced tools
A fork of original Croppr.js, a vanilla JavaScript image cropper, with advanced options.
Via NPM:
npm install dnm-croppr -—save
// ES6 import - JS + CSS
import Croppr from 'dnm-croppr';
Via script tag:
<link href="path/to/dnm-croppr.min.css" rel="stylesheet"/>
<script src="path/to/dnm-croppr.min.js"></script>
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}
Constrain the crop region to an aspect ratio.
Number
null
aspectRatio: 1
(Square)Autoplay the video after initialization.
Boolean
false
If maxAspectRatio and aspectRatio aren't null, the crop region is constrained between these two aspect ratios.
Number
null
aspectRatio: 1
(Square)Constrain the crop region to a maximum size.
[width, height, unit?]
null
maxSize: [0.5, 0.5, 'ratio']
(A maximum size of 50% of the image size)Constrain the crop region to a minimum size.
[width, height, unit?]
null
minSize: [20, 20, 'real']
(A minimum width and height of 20px, relative to source image size)If you use dnm-croppr inside a modal, you have to define your modal parent element here.
String
or Element
null
"#myModal"
or an Element
object as document.getElementById("myModal")
Mute the video after initialization.
Boolean
false
Define the container for live preview.
String
or Element
null
"#preview"
or an Element
object as document.getElementById("preview")
The starting size of the crop region when it is initialized.
[width, height, unit?]
[1, 1, 'ratio']
(A starting crop region as large as possible)startSize: [0.5, 0.5]
(A starting crop region of 50% of the image size)The starting position of the crop region when it is initialized.
[width, height, unit?]
null
(Crop region is centered)startSize: [250, 100, "real"]
(A starting crop region positioned at 250 px from left, and 100 px from top, relative to source image size)Note: unit
accepts a value of 'raw', 'ratio' or 'real'. Defaults to 'raw'.
A callback function that is called when the user starts modifying the crop region.
Function
data = {x, y, width, height}
onCropStart: function(data) {
console.log(data.x, data.y, data.width, data.height);
}
A callback function that is called when the user is changing the crop region.
Function
data = {x, y, width, height}
onCropMove: function(data) {
console.log(data.x, data.y, data.width, data.height);
}
A callback function that is called when the modification of the crop region is finished (by the user, or programmatically). onCropEnd is called with methods setImage(), moveTo(), resizeTo(), setValue(), scaleBy(), reset().
Function
data = {x, y, width, height}
onCropEnd: function(data) {
console.log(data.x, data.y, data.width, data.height);
}
A callback function that is called when the Croppr instance is fully initialized.
Function
onInitialize: function(instance, mediaNode) {
// do things here
}
Define how the crop region should be calculated.
String
"real"
"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.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}
Destroys the Croppr instance and restores the original img
element.
Changes the image src. Returns the Croppr instance and media node.
Changes the video src. Returns the Croppr instance and media node.
Moves the crop region to the specified coordinates. Returns the Croppr instance.
Note: If constrain
is false, crop region isn't limited by options. Default value is true.
mode
can be 'raw', 'ratio', 'real'. Default value is 'raw'.
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.
Note: origin
default value is [.5, .5].
If constrain
is false, crop region isn't limited by options. Default value is true.
mode
can be 'raw', 'ratio', 'real'. Default value is 'raw'.
Move and resize the crop region. Returns the Croppr instance.
//Example use of setValue() : The crop region will be moved and resized relatively to the source image size
var cropData = {
x: 150,
y: 50,
width: 350,
height: 200
};
cropInstance.setValue(cropData, true, "real");
Note: If constrain
is false, crop region isn't limited by options. Default value is true.
mode
can be 'raw', 'ratio', 'real'. Default value is 'raw'.
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.
Resets the crop region to its original position and size. Returns the Croppr instance.
Thanks to original author James Ooi. Released under the MIT License.
FAQs
A fork of original Croppr.js, a vanilla JavaScript image cropper, with advanced options.
We found that dnm-croppr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.