docs/api/cropper-selection.md
The CropperSelection interface provides properties and methods for manipulating the layout and presentation of <cropper-selection> elements.
:::live-demo
<cropper-selection></cropper-selection>
:::
:::tip
The default width and height of this element is 0.
:::
:::live-demo
<cropper-canvas background>
<cropper-selection initial-coverage="0.5" outlined></cropper-selection>
</cropper-canvas>
:::
:::live-demo
<cropper-canvas background>
<cropper-selection x="10" y="5" width="160" height="90" outlined></cropper-selection>
</cropper-canvas>
:::
:::live-demo
<cropper-canvas background>
<cropper-selection initial-coverage="0.5" movable resizable zoomable outlined>
<cropper-handle action="move" theme-color="rgba(255, 255, 255, 0.35)"></cropper-handle>
<cropper-handle action="n-resize"></cropper-handle>
<cropper-handle action="e-resize"></cropper-handle>
<cropper-handle action="s-resize"></cropper-handle>
<cropper-handle action="w-resize"></cropper-handle>
<cropper-handle action="ne-resize"></cropper-handle>
<cropper-handle action="nw-resize"></cropper-handle>
<cropper-handle action="se-resize"></cropper-handle>
<cropper-handle action="sw-resize"></cropper-handle>
</cropper-selection>
</cropper-canvas>
:::
Set the dynamic property to true to change as the image changes.
:::live-demo
<cropper-canvas style="height: 360px;" background>
<cropper-image src="/cropperjs/picture.jpg" alt="Picture" rotatable scalable skewable translatable></cropper-image>
<cropper-shade hidden></cropper-shade>
<cropper-handle action="move" plain></cropper-handle>
<cropper-selection initial-coverage="0.5" dynamic movable resizable zoomable>
<cropper-grid role="grid" covered></cropper-grid>
<cropper-crosshair centered></cropper-crosshair>
<cropper-handle action="move" theme-color="rgba(255, 255, 255, 0.35)"></cropper-handle>
<cropper-handle action="n-resize"></cropper-handle>
<cropper-handle action="e-resize"></cropper-handle>
<cropper-handle action="s-resize"></cropper-handle>
<cropper-handle action="w-resize"></cropper-handle>
<cropper-handle action="ne-resize"></cropper-handle>
<cropper-handle action="nw-resize"></cropper-handle>
<cropper-handle action="se-resize"></cropper-handle>
<cropper-handle action="sw-resize"></cropper-handle>
</cropper-selection>
</cropper-canvas>
:::
Set the multiple property to true to support multiple selections on the same image.
:::live-demo
<cropper-canvas style="height: 360px;" background>
<cropper-image src="/cropperjs/picture.jpg" alt="Picture" rotatable scalable skewable translatable></cropper-image>
<cropper-shade hidden></cropper-shade>
<cropper-handle action="select" plain></cropper-handle>
<cropper-selection id="cropperSelection" x="20" y="20" width="40" height="40" movable resizable multiple keyboard>
<cropper-grid role="grid" covered></cropper-grid>
<cropper-crosshair centered></cropper-crosshair>
<cropper-handle action="move" theme-color="rgba(255, 255, 255, 0.35)"></cropper-handle>
<cropper-handle action="n-resize"></cropper-handle>
<cropper-handle action="e-resize"></cropper-handle>
<cropper-handle action="s-resize"></cropper-handle>
<cropper-handle action="w-resize"></cropper-handle>
<cropper-handle action="ne-resize"></cropper-handle>
<cropper-handle action="nw-resize"></cropper-handle>
<cropper-handle action="se-resize"></cropper-handle>
<cropper-handle action="sw-resize"></cropper-handle>
</cropper-selection>
<cropper-selection id="cropperSelection1" x="60" y="60" width="80" height="80" movable resizable multiple keyboard>
<cropper-grid role="grid" covered></cropper-grid>
<cropper-crosshair centered></cropper-crosshair>
<cropper-handle action="move" theme-color="rgba(255, 255, 255, 0.35)"></cropper-handle>
<cropper-handle action="n-resize"></cropper-handle>
<cropper-handle action="e-resize"></cropper-handle>
<cropper-handle action="s-resize"></cropper-handle>
<cropper-handle action="w-resize"></cropper-handle>
<cropper-handle action="ne-resize"></cropper-handle>
<cropper-handle action="nw-resize"></cropper-handle>
<cropper-handle action="se-resize"></cropper-handle>
<cropper-handle action="sw-resize"></cropper-handle>
</cropper-selection>
<cropper-selection id="cropperSelection2" x="140" y="140" width="120" height="120" movable resizable multiple keyboard>
<cropper-grid role="grid" covered></cropper-grid>
<cropper-crosshair centered></cropper-crosshair>
<cropper-handle action="move" theme-color="rgba(255, 255, 255, 0.35)"></cropper-handle>
<cropper-handle action="n-resize"></cropper-handle>
<cropper-handle action="e-resize"></cropper-handle>
<cropper-handle action="s-resize"></cropper-handle>
<cropper-handle action="w-resize"></cropper-handle>
<cropper-handle action="ne-resize"></cropper-handle>
<cropper-handle action="nw-resize"></cropper-handle>
<cropper-handle action="se-resize"></cropper-handle>
<cropper-handle action="sw-resize"></cropper-handle>
</cropper-selection>
</cropper-canvas>
:::
::: details <<< @/.vitepress/components/CropperSelectionExample.vue :::
Inherits properties from its parent, CropperElement, and implements the following properties:
| Name | Type | Default | Options | Description |
|---|---|---|---|---|
| x | number | 0 | - | Indicates the x-axis coordinate of the selection. |
| y | number | 0 | - | Indicates the y-axis coordinate of the selection. |
| width | number | 0 | - | Indicates the width of the selection. |
| height | number | 0 | - | Indicates the height of the selection. |
| aspectRatio | number | NaN | - | Indicates the aspect ratio of the selection, must a positive number. |
| initialAspectRatio | number | NaN | - | Indicates the initial aspect ratio of the selection, must a positive number. |
| initialCoverage | number | NaN | - | Indicates the initial coverage of the selection, must a positive number between 0 (0%) and 1 (100%). |
| dynamic | boolean | false | - | Indicates whether this selection is dynamic and changes as the image changes. |
| movable | boolean | false | - | Indicates whether this element is movable. |
| resizable | boolean | false | - | Indicates whether this element is resizable. |
| zoomable | boolean | false | - | Indicates whether this element is zoomable. |
| multiple | boolean | false | - | Indicates whether multiple selections is supported. |
| keyboard | boolean | false | - | Indicates whether keyboard control is supported. |
| outlined | boolean | false | - | Indicates whether show the outlined or not. |
| precise | boolean | false | - | Indicates whether reserve the precise of the x, y, width, and height properties or not. |
The supported keyboard keys:
Delete or Command + Backspace: Removes the active selection.ArrowLeft: Moves the active selection to the left by 1 pixel.ArrowRight: Moves the active selection to the right by 1 pixel.ArrowUp: Moves the active selection to the top by 1 pixel.ArrowDown: Moves the active selection to the bottom by 1 pixel.+: Zooms in the active selection by 10%.-: Zooms out the active selection by 10%.$center()CropperSelectionAligns the selection to the center of its parent element.
$move(x)$move(x, y)$moveTo(selection.x + x)$moveTo(selection.x + x, selection.y + y)x:
numbery:
numberxCropperSelectionMoves the selection.
$moveTo(x)$moveTo(x, y)x:
numbery:
numberxCropperSelectionMoves the selection to a specific position.
$resize(action)$resize(action, offsetX)$resize(action, offsetX, offsetY)$resize(action, offsetX, offsetY, aspectRatio)action:
string"n-resize", "e-resize", "s-resize", "w-resize", "ne-resize", "nw-resize", "se-resize", and "sw-resize".offsetX:
number0offsetY:
number0aspectRatio:
numberthis.aspectRatioCropperSelectionAdjusts the size of the selection on a specific side or corner.
Syntax:
$zoom(scale)$zoom(scale, x, y)Arguments:
scale:
numberx:
numbery:
numberReturns:
CropperSelectionExample:
cropperSelection.$zoom(0.1); // Zoom in 10%
cropperSelection.$zoom(-0.1); // Zoom out 10%
Zooms the selection. Changes the width and height of the selection in pixels directly at the same time.
$change(x, y)$change(x, y, width, height)$change(x, y, width, height, aspectRatio)x:
numbery:
numberwidth:
numberthis.widthheight:
numberthis.heightaspectRatio:
numberthis.aspectRatioCropperSelectionChanges the position and/or size of the selection.
$reset()CropperSelectionResets the selection to its initial position and size.
$clear()CropperSelectionClears the selection.
$render()CropperSelectionRefreshes the position or size of the selection.
$toCanvas()$toCanvas(options)options:
Objectwidth:
numberheight:
numberbeforeDraw:
FunctionbeforeDraw(context, canvas)context:
CanvasRenderingContext2Dcanvas:
HTMLCanvasElementfunction (context) { context.filter = 'grayscale(100%)'; }PromiseGenerates a real canvas element, with the image (selected area only) drawn into if there is one.
The event is fired when the position or size of the selection is going to change.
truetruetrueObjectnumbernumbernumbernumber<cropper-selection id="selection"></cropper-selection>
<script>
document.querySelector('#selection').addEventListener('change', function (event) {
console.log(event);
});
</script>
There is only one default slot in this element.
You can disable it by setting the
slottableproperty tofalse:html<cropper-selection slottable="false"></cropper-selection>