CropperImage
The CropperImage
interface provides properties and methods for manipulating the layout and presentation of <cropper-image>
elements.
Examples
Basic
TIP
The default width and height of this element is 0
.
With image source
Limit boundaries
Details
<template>
<div class="cropper-container">
<form>
<fieldset>
<legend>Image Fit:</legend>
<input
id="inputImageFitContain"
v-model="imageFit"
type="radio"
name="imageFit"
value="contain"
>
<label for="inputImageFitContain">contain</label>
<input
id="inputImageFitCover"
v-model="imageFit"
type="radio"
name="imageFit"
value="cover"
>
<label for="inputImageFitCover">cover</label>
<input
id="inputImageFitNone"
v-model="imageFit"
type="radio"
name="imageFit"
value="none"
>
<label for="inputImageFitNone">none</label>
</fieldset>
</form>
<cropper-canvas
ref="cropperCanvas"
:key="imageFit"
background
>
<cropper-image
ref="cropperImage"
:src="src"
alt="Picture"
rotatable
scalable
skewable
translatable
@transform="onCropperImageTransform"
/>
<cropper-handle
action="move"
plain
/>
</cropper-canvas>
</div>
</template>
<script lang="ts">
import type CropperCanvas from '@cropper/element-canvas';
import type CropperImage from '@cropper/element-image';
const { BASE_URL } = import.meta.env;
export default {
name: 'CropperImageExample',
data() {
return {
src: `${BASE_URL}picture.jpg`,
imageFit: 'contain',
};
},
methods: {
onCropperImageTransform(event: CustomEvent) {
const cropperCanvas = this.$refs.cropperCanvas as CropperCanvas;
if (!cropperCanvas || this.imageFit === 'none') {
return;
}
const cropperImage = this.$refs.cropperImage as CropperImage;
const cropperCanvasRect = cropperCanvas.getBoundingClientRect();
// 1. Clone the cropper image.
const cropperImageClone = cropperImage.cloneNode() as CropperImage;
// 2. Apply the new matrix to the cropper image clone.
cropperImageClone.style.transform = `matrix(${event.detail.matrix.join(', ')})`;
// 3. Make the cropper image clone invisible.
cropperImageClone.style.opacity = '0';
// 4. Append the cropper image clone to the cropper canvas.
cropperCanvas.appendChild(cropperImageClone);
// 5. Compute the boundaries of the cropper image clone.
const cropperImageRect = cropperImageClone.getBoundingClientRect();
// 6. Remove the cropper image clone.
cropperCanvas.removeChild(cropperImageClone);
if (
(this.imageFit === 'contain' && (
(
cropperImageRect.top > cropperCanvasRect.top
&& cropperImageRect.right < cropperCanvasRect.right
)
|| (
cropperImageRect.right < cropperCanvasRect.right
&& cropperImageRect.bottom < cropperCanvasRect.bottom
)
|| (
cropperImageRect.bottom < cropperCanvasRect.bottom
&& cropperImageRect.left > cropperCanvasRect.left
)
|| (
cropperImageRect.left > cropperCanvasRect.left
&& cropperImageRect.top > cropperCanvasRect.top
)
))
|| (this.imageFit === 'cover' && (
cropperImageRect.top > cropperCanvasRect.top
|| cropperImageRect.right < cropperCanvasRect.right
|| cropperImageRect.bottom < cropperCanvasRect.bottom
|| cropperImageRect.left > cropperCanvasRect.left
))
) {
event.preventDefault();
}
},
},
};
</script>
<style lang="scss" scoped>
.cropper-container {
border: 1px solid var(--vp-c-divider);
border-radius: 0.375rem;
margin-bottom: 1rem;
margin-top: 1rem;
padding: 1.25rem 1.5rem;
fieldset {
border: 1px solid var(--vp-c-divider);
border-radius: 0.375rem;
margin-bottom: 1rem;
padding: 0.25rem 0.75rem 0.75rem 0.75rem;
> input {
margin: 0 0.25rem 0 0;
transform: translateY(-0.5px);
vertical-align: middle;
}
> label {
margin-right: 0.5rem;
}
}
cropper-canvas {
height: 320px;
}
}
</style>
Properties
Inherits properties from its parent, CropperElement
, and implements the following properties:
Name | Type | Default | Options | Description |
---|---|---|---|---|
initial-center-size | string | "contain" | "contain" , "cover" | Indicates the initial size of the image when aligned with the center of its parent element. |
rotatable | boolean | false | - | Indicates whether this element is rotatable. |
scalable | boolean | false | - | Indicates whether this element is scalable. |
skewable | boolean | false | - | Indicates whether this element is skewable. |
slottable | boolean | false | - | Indicates whether this element is slottable. |
translatable | boolean | false | - | Indicates whether this element is translatable. |
The built-in <img>
element will inherit the following attributes by default:
alt
crossorigin
decoding
importance
loading
referrerpolicy
sizes
src
srcset
Methods
$ready
Syntax:
$ready()
$ready(callback)
Arguments:
callback
:- Type:
Function
- The callback to execute after successfully loading the image.
- Type:
Returns:
- Type:
Promise
- A promise that resolves to the image element.
- Type:
Example:
jsconst cropperImage = new CropperImage(); cropperImage.$ready((image) => { console.log(image.naturalWidth, image.naturalHeight); }); cropperImage.src = '/cropperjs/v2/picture.jpg';
Defers the callback to execute after successfully loading the image.
$center
- Syntax:
$center()
$center(size)
- Arguments:
size
:- Type:
string
- Options:
"contain"
, and"cover"
. - The size of the image.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Aligns the image to the center of its parent element.
$move
- Syntax:
$move(x)
$move(x, y)
- Arguments:
x
:- Type:
number
- The moving distance in the horizontal direction.
- Type:
y
:- Type:
number
- Default:
x
- The moving distance in the vertical direction.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Moves the image.
$moveTo
- Syntax:
$moveTo(x)
$moveTo(x, y)
- Arguments:
x
:- Type:
number
- The new position in the horizontal direction.
- Type:
y
:- Type:
number
- Default:
x
- The new position in the vertical direction.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Moves the image to a specific position.
$rotate
- Syntax:
$rotate(angle)
- Arguments:
angle
:- Type:
number | string
- The rotation angle (in radians). The default unit is
rad
.
- Type:
x
:- Type:
number
- Default: The center of the image in the horizontal.
- The rotation origin in the horizontal.
- Type:
y
:- Type:
number
- Default: The center of the image in the vertical.
- The rotation origin in the vertical.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
- Examples:
$rotate(0.8)
$rotate('0.8rad')
$rotate('45deg')
$rotate('50grad')
$rotate('0.1turn')
$rotate('90deg', 0, 0)
Rotates the image. It is similar to CSS function rotate() or CanvasRenderingContext2D.rotate().
$zoom
Syntax:
$zoom(scale)
$zoom(scale, x, y)
Arguments:
scale
:- Type:
number
- The zoom factor. Positive numbers for zooming in, and negative numbers for zooming out.
- Type:
x
:- Type:
number
- Default: The center of the image in the horizontal.
- The zoom origin in the horizontal.
- Type:
y
:- Type:
number
- Default: The center of the image in the vertical.
- The zoom origin in the vertical.
- Type:
Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Examples:
jscropperImage.$zoom(0.1); // Zoom in 10% cropperImage.$zoom(-0.1); // Zoom out 10% cropperImage.$zoom(0.1, 0, 0); // Zoom in from the top-left corner cropperImage.$zoom(-0.1, 0, 0); // Zoom out from the top-left corner
Zooms the image.
$scale
Syntax:
$scale(x)
$scale(x, y)
Arguments:
x
:- Type:
number
- The scaling factor in the horizontal direction.
- Type:
y
:- Type:
number
- Default:
x
- The scaling factor in the vertical direction.
- Type:
Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Examples:
jscropperImage.$scale(1.1); // Zoom in 10% cropperImage.$scale(0.9); // Zoom out 10% cropperImage.$scale(-1); // Flip both the horizontal and vertical directions cropperImage.$scale(-1, 1); // Flip the horizontal direction cropperImage.$scale(1, -1); // Flip the vertical direction
Scales the image. It is similar to CSS function scale() or CanvasRenderingContext2D.scale().
$skew
- Syntax:
$skew(x)
$skew(x, y)
- Arguments:
x
:- Type:
number | string
- The skewing angle in the horizontal direction. The default unit is
rad
.
- Type:
y
:- Type:
number | string
- Default:
x
- The skewing angle in the vertical direction. The default unit is
rad
.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
- Examples:
$skew(0.8)
$skew('0.8rad')
$skew('45deg')
$skew('50grad')
$skew('0.1turn')
$skew(0, 0.8)
Skews the image. It is similar to CSS function skew().
$translate
- Syntax:
$translate(x)
$translate(x, y)
- Arguments:
x
:- Type:
number
- The translating distance in the horizontal direction.
- Type:
y
:- Type:
number
- Default:
x
- The translating distance in the vertical direction.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Translates the image. It is similar to CSS function translate() or CanvasRenderingContext2D.translate().
$transform
- Syntax:
$transform(a, b, c, d, e, f)
- Arguments:
a
:- Type:
number
- The scaling factor in the horizontal direction.
- Type:
b
:- Type:
number
- The skewing angle in the vertical direction.
- Type:
c
:- Type:
number
- The skewing angle in the horizontal direction.
- Type:
d
:- Type:
number
- The scaling factor in the vertical direction.
- Type:
e
:- Type:
number
- The translating distance in the horizontal direction.
- Type:
f
:- Type:
number
- The translating distance in the vertical direction.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Transforms the image. It is similar to CSS function matrix() or CanvasRenderingContext2D.transform().
$setTransform
- Syntax:
$setTransform(a, b, c, d, e, f)
$setTransform(a)
- Arguments:
a
:- Type:
number | Array
- The scaling factor in the horizontal direction, or the transformation matrix.
- Type:
b
:- Type:
number
- The skewing angle in the vertical direction.
- Type:
c
:- Type:
number
- The skewing angle in the horizontal direction.
- Type:
d
:- Type:
number
- The scaling factor in the vertical direction.
- Type:
e
:- Type:
number
- The translating distance in the horizontal direction.
- Type:
f
:- Type:
number
- The translating distance in the vertical direction.
- Type:
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Resets (overrides) the current transform to the specific identity matrix, and then invokes a transform described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context. It is similar to CanvasRenderingContext2D.setTransform().
$getTransform
- Syntax:
$getTransform()
- Returns:
- Type:
Array
- The current transformation matrix of the element.
- Type:
Retrieves the current transformation matrix being applied to the element. It is similar to CanvasRenderingContext2D.getTransform().
$resetTransform
- Syntax:
$resetTransform()
- Alternatives:
$setTransform(1, 0, 0, 1, 0, 0)
$setTransform([1, 0, 0, 1, 0, 0])
- Returns:
- Type:
CropperImage
- The element instance for chaining.
- Type:
Resets the current transform to the initial identity matrix. It is similar to CanvasRenderingContext2D.resetTransform().
Events
transform
- Event:
- event.bubbles:
true
- event.cancelable:
true
- event.composed:
true
- event.detail:
- Type:
Object
- The transform information of the image.
- Type:
- event.detail.matrix:
- Type:
Array
- The new (next) matrix object.
- Type:
- event.detail.oldMatrix:
- Type:
Array
- The old (current) matrix object.
- Type:
- event.bubbles:
The event is fired when the transform
CSS property of the element is going to change.
Slots
There are no available slots in this element.
You can enable the default slot by setting the
slottable
property totrue
:html<cropper-image slottable></cropper-image>