# Cropper
- Events
- Methods
- Props
- src
- stencilComponent
- stencilProps
- class
- imageClass
- boundariesClass
- backgroundClass
- foregroundClass
- debounce
- stencilSize
- canvas
- checkOrientation
- imageRestriction
- resizeImage
- moveImage
- minWidth
- minHeight
- maxWidth
- maxHeight
- transitions
- roundResult
- autoZoom
- priority
- defaultPosition
- defaultTransforms
- defaultSize
- defaultVisibleArea
- defaultBoundaries
- sizeRestrictionsAlgorithm
# Events
# change
Cropper will emit change event on mounting, resizing the stencil, moving the stencil and changing the image.
The event payload is the object: { coordinates, image, visibleArea, canvas }.
# ready
Cropper will emit ready event when image is successfully loaded.
# error
Cropper will emit error event when image is unsuccessfully loaded.
# Methods
# getResult()
Returns:
- The object:
{ coordinates, image, visibleArea, canvas }
- The object:
Usage:
The method allows you to get the result of cropper programmatically, instead of listening the
changeevent.const { coordinates, image, visibleArea, canvas } = this.$refs.cropper.getResult();See also: Example
# setCoordinates(transform)
Arguments:
{Object|Function|Array} transform
Usage:
The method allows you to set the coordinates programmatically. The transform argument can be:
Object,FunctionorArraythat contains objects or function in the case if you need consequence transforms.The only argument
transformcan be:Object,FunctionorArraythat contains objects or function in the case if you need consequence transforms.#
ObjectIf you just want to set the known coordinates you can pass object to
setCoordinatesmethodcropper.setCoordinates({ width: 32, height: 42, left: 102, top: 74 })#
FunctionBut mostly you need to set coordinates based at current coordinates or image size.
- Center stencil:
cropper.setCoordinates(({ coordinates, imageSize }) => ({ left: imageSize.width/2 - coordinates.width/2, top: imageSize.height/2 - coordinates.height/2 }))- Maximize stencil:
cropper.setCoordinates(({ coordinates, imageSize }) => ({ width: imageSize.width, height: imageSize.height }))#
ArrayFinally, there might be situations where you need to make consequence transforms. For example, resize stencil and then center it.
That can appear to be superfluous, because you can set coordinates and size simultaneosly:
cropper.setCoordinates(({ coordinates, imageSize }) => ({ width: newWidth, height: newHeight, left: imageSize.width/2 - newWidth.width/2, top: imageSize.height/2 - newHeight.height/2 }))But there is a catch,
setCoordinatesmethod respects limitations, so the new width might be different thannewWidthin this example.So the right way is do multiple consequence transforms:
cropper.setCoordinates([ ({ coordinates, imageSize }) => ({ width: newWidth, height: newHeight, }), // There will be coordinates after first transformation ({ coordinates, imageSize }) => ({ left: imageSize.width/2 - coordinates.width/2, top: imageSize.height/2 - coordinates.height/2 }), ])See also: Example
# refresh()
Usage:
This method refreshes the cropper. This method is called on every window resize and can be useful if the external container width is changed, but window's size isn't changed.
See also: Example
# zoom(factor, center)
Arguments:
{Number} factor{Object} [center]
Usage:
This methods is used to scale visible area relative to its scale.
The first parameter
factoris the number, that represents scale factor (i.e.1.1to resize to110%,0.8to resize to80%).The second parameter
centeris the object{ left, top }.See also: Example
# move(left, top)
Arguments:
{Number} left{Number} top
Usage: This methods is used to translate visible area relative to its position. The parameters
leftandtopdetermine the relative shift at left and top.See also: Example
# rotate(angle)
Arguments:
{Number} angle
Usage:
This methods is used to rotate the image to a specific angle in degrees.
See also: Example
# flip(horizontal, vertical)
Arguments:
{Boolean} horizontal{Boolean} vertical
Usage:
This methods is used to flip the image horizontally and/or vertically.
See also: Example
# reset()
Usage:
This method resets the cropper to the initial state.
# Props
# src
Type:
String | NullDefault:
nullDetails:
The link to cropping image or the image itself in base64 format
# stencilComponent
Type:
ComponentDefault:
RectangleStencilDetails:
The stencil component. For globally registered component just pass their name here, otherwise pass the component’s options object.
See also: Example
# stencilProps
Type:
ObjectDefault:
{}Details:
The props that will be passed to the stencil component. The usual scenario is passing aspect ratio props here.
# class
Type:
StringDetails:
The optional class for the entire cropper
# imageClass
Type:
StringDetails:
The optional class for the cropper image
# boundariesClass
Type:
StringDetails:
The optional class for the boundaries. Probably you should not use this prop.
# backgroundClass
Type:
StringDetails:
The optional class for the background that is placed under the cropper image.
# foregroundClass
Type:
StringDetails:
The optional class for the foreground that is placed above the cropper image, but under the stencil.
# debounce
Type:
NumberDefault:
500Details:
The time before
changeevent will be emitted after moving or resizing stencil.
# stencilSize
Type:
Object | FunctionDetails:
The size of the stencil in pixels (not relative to an image).
#
ObjectIf you just want to set the known size you can pass object to
stencilSizeprop:{ width: 100, height: 100, }#
FunctionIf you need to set the stencil size based on the boundaries size, you should pass a function. For example:
({ boundaries }) => { return { width: boundaries.width - 100, height: boundaries.height - 100, } }See also: Example
# canvas
Type:
Boolean | ObjectDefault:
trueDetails:
This prop indicates if canvas should be used in cropper and can be used also to pass some options to it.
Options:
width- the width of the canvas (it's equal to setting ofminWidthequal tomaxWidth)height- the height of the canvas (it's equal to setting ofminHeightequal tomaxHeight)minWidth- the minimum width of the canvas (by default0)minHeight- the minimum height of the canvas (by default0)maxWidth- the maximum width of the canvas (by defaultInfinity)maxHeight- the maximum height of the canvas (by defaultInfinity)maxArea- the maximum width × height of the canvas (it may be useful to correspond browser limitations (opens new window))imageSmoothingEnabled- the context property (opens new window) that determines whether scaled images are smoothed (by default,true)imageSmoothingQuality- the context property (opens new window) that determines the smooth quality (by default,'high')fillColor- the color to fill alpha background (by defaulttransparent)
If you need the coordinates only you should set
canvastofalseto optimize performance and prevent the setting ofcrossoriginattribute to image.See also: Example
# checkOrientation
Type:
BooleanDefault:
trueDetails:
The flag that indicates if EXIF orientation should be checked
# imageRestriction
Type:
StringDefault:
'fit-area'Details:
This parameter sets different restrictions of an image position:
fill-areafill area by image and prevents resizing and moving the image beyond the areafit-areafit image to area and prevents resizing and moving the image beyond the area as much as possible (example)stencilprevents resizing and moving the image beyond the stencilnoneallows free resizing and moving the image
See also: Example
# resizeImage
Type:
Boolean | ObjectDefault:
trueDetails:
This prop is either boolean flag or object settings. The default object used when
trueis passed toresizeImage:{ touch: true, wheel: { ratio: 0.1 }, adjustStencil: true }The
touchfield checks if image should be resized by a pinch gesture.The
wheelfield is either boolean flag or object. The object currently support only one option:ratio, i.e. speed of resizing by wheel.The particular attention should be focused on
adjustStencilparameter. It enables or disables the adjusting of the stencil size on resize image. It makes cropper more convenient especially when you have the limitations of width / height, but you probably shouldn't use it if you have fixed stencil, because it will change its size.See also: Example
# moveImage
Type:
Boolean | ObjectDefault:
trueDetails:
This prop is either boolean flag or object settings. The default object:
{ touch: true, mouse: true }The fields:
touchis a flag that checks if image should be moved by a touchmouseis a flag that checks if image should be moved by a mouse
# minWidth
Type:
NumberDetails:
The minimum width of the cropped coordinates in pixels
# minHeight
Type:
NumberDetails:
The minimum height of the cropped coordinates in pixels
# maxWidth
Type:
NumberDetails:
The maximum width of the cropped coordinates in pixels
# maxHeight
Type:
NumberDetails:
The maximum height of the cropped coordinates in pixels
# transitions
Type:
BooleanDefault:
trueDetails:
This flag indicates if transitions should be enabled. The transitions are used during auto-zoom, rotate image, flip image, using
zoomandmovemethods.
# roundResult
Type:
BooleanDefault:
trueDetails:
This flag indicates if the coordinates should be rounded when you getting the result.
# autoZoom
Type:
BooleanDefault:
falseDetails:
This flag indicates if auto-zoom should be enabled.
If the auto-zoom algorithm was not redefined, there are two algorithms used.
If
stencilSizeis not defined, the default simple auto-zoom algorithm will be used. It resizes and move visible area to when you setting the coordinates bysetCoordinatesmethod, to prevent overlap the coordinates and the visible area.If
stencilSizeis defined, the default fixed auto-zoom algorithm will be used. It adapts coordinates and visible area to fit thestencilSizerestrictions.
See also: Example
# priority
Type:
StringDefault:
'coordinates'Details:
Rule of thumb
If you set the default coordinates it's better to set
'coordinates', if you set the default visible area it's better to set'visible-area'.It can be either
'coordinates'or'visible-area'. It sets the priority of initialization default values.#
'coordinates'The coordinates will be initialized first, but
defaultSizeanddefaultPositionalgorithms will know nothing about visible area.#
'visible-area'The visible area will be initialized first, but
defaultVisibleAreaalgorithm will know nothing about coordinates.
# defaultPosition
Type:
Object | FunctionDetails:
It's either an object or static function.
#
ObjectThe object should correspond the following scheme:
{ left: 142, top: 73 }#
FunctionThe static function should accept the only argument, the object with following fields:
visibleArea:{ left, top, width, height }ornullif the coordinates has priority.imageSize:{ width, height },coordinates:{ width, height }
It should return an object with
leftandtopfields, i.e. default position of the stencil (relative to original image size)({ visibleArea, coordinates, imageSize }) => { return { left: imageSize.width / 2 - coordinates.width / 2, top: imageSize.height / 2 - coordinates.height / 2, } }See also: Example
# defaultTransforms
Type:
Object | FunctionDetails:
It's either an object or static function.
#
ObjectThe object should correspond the following scheme:
{ rotate: 90, flip: { horizontal: true, vertical: false, } }#
FunctionIt should return an object like the object above:
() => { return { rotate: 90, flip: { horizontal: true, vertical: false, } } }See also: Example
# defaultSize
Type:
Object | FunctionDetails:
It's either an object or static function.
#
ObjectThe object should correspond the following scheme:
{ width: 142, height: 73 }#
FunctionThe static function should accept the only argument, the object with the following fields:
visibleArea:{ left, top, width, height }ornullif the coordinates has priority.,imageSize:{ width, height },stencilRatio:{ minimum, maximum },sizeRestrictions:{ minWidth, minHeight, maxWidth, maxHeight }
It should return an object with
heightandwidthfields, i.e. default size of the stencil (relative to original image size)({ visibleArea, imageSize, stencilRatio, sizeRestrictions }) => { return { width: imageSize.width, height: imageSize.height, } }See also: Example
# defaultVisibleArea
Type:
Object | FunctionDetails:
It's either an object or static function.
#
ObjectRemember!
The visible area always have the aspect ratio as boundaries, so if they will not be the same, cropper will resize the visible area vertically to fix it.
The object should correspond the following scheme:
{ width: 200, height: 200, left: 0, top: 0 }#
FunctionThe static function that should accept the only argument, the object with the following fields:
coordinates:{ left, top, width, height }ornullif the visible area has priority,boundaries:{ width, height },imageSize:{ width, height },
It should return an object with the coordinates of visible area:
({ coordinates, boundaries, imageSize }) => { return { left: coordinates.left - 50, top: coordinates.top - 50, width: coordinates.width + 100, height: coordinates.height + 100, } }See also: Example
# defaultBoundaries
Type:
String | FunctionDefault:
'fill'Details:
It's either an string or static function.
# 'String'
There are available two string values:
'fill', boundaries should fill the cropper'fit', boundaries should have the same aspect ratio as the image and should be fitted to the cropper
# 'Function'
The static function that accepts the only argument, the object with following fields:
cropper(DOM Element)imageSize({ width, height }),
It should return the object with
heightandwidthfields, i.e. width and height of the area.({ cropper, imageSize }) => { return { width: cropper.clientWidth, height: cropper.clientHeight, } }
# sizeRestrictionsAlgorithm
Type:
FunctionDefault: pixels restrictions algorithm.
Details:
The static function that accepts the only argument, the object with following fields:
minWidth,minHeight,maxWidth,maxHeightimageSize({ width, height})
It should return the object with restrictions for stencil. For example something like this:
({ minWidth, minHeight, maxWidth, maxHeight, imageSize }) => { return { maxWidth: imageSize.width * (maxWidth || 0) / 100, maxHeight: imageSize.height * (maxHeight || 0) / 100, minWidth: imageSize.width * (minWidth || 100) / 100, minHeight: imageSize.height * (minHeight || 100) / 100, } }The example above change the interpretation of
minWidth,minHeight,maxWidth, andmaxHeightprops. Now they set the limitations in percents of the image.