Image Cropper
Choose the part of an image to keep by moving and resizing a selection, then read it back as a canvas or a blob.
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
src | string | — | URL of the image to crop. Replacing it selects the centred 80% of the new image, unless crop changes too. |
alt | string | — | Alternative text of the image. |
crossorigin | string | undefined | undefined | CORS mode of the image request (anonymous, use-credentials); needed to read a cross-origin image back with toCanvas/toBlob. |
aspect-ratio | number | 0 | Locks the selection's width-to-height ratio: 1, 16/9, 4:3 or 1.5. Unset or 0 leaves it free. |
shape | ImageCropperShape | rectangle | ellipse rounds the selection and makes toCanvas/toBlob clip to it (transparent corners in a PNG). |
crop | CropRect | undefined | undefined | The selection, in the image's natural pixels (JSON in markup). Set by the cropper once the image loads when left unset; kept inside the image and refitted around its centre when it is off aspect-ratio. |
disabled | boolean | false | Locks the selection: no dragging, no keyboard, not focusable. |
label | string | — | Accessible name of the selection. Defaults to "Crop area" in the nearest lang (English, or a language the app adds). |
Slots
| Name | Description |
|---|---|
placeholder | Shown while there is no src; put an upload control here. |
Events
| Name | Type | Description |
|---|---|---|
crop-input | CustomEvent<{ crop: CropRect }> | Fired while the reader changes the selection (every step of a drag). Not fired for changes made by script. Does not bubble. |
crop-change | CustomEvent<{ crop: CropRect }> | Fired when the reader commits a change: the pointer is released or a key moved the selection. Does not bubble. |
load | Event | The image has loaded (re-dispatched from the inner image). |
error | Event | The image failed to load (re-dispatched from the inner image). |
CSS parts
Shadow DOM styling hooks used with ::part(name). Prefer CSS custom properties when they cover the change.
| Name | Description |
|---|---|
frame | Box around the image; centres it and draws the background behind transparent pixels. Without a src it wraps the placeholder slot instead (no fallback content). |
image | The image being cropped. |
selection | The selected area: its outline, guides and handles. |
handle | Each resize handle. Edge handles are hidden while aspect-ratio is set. |
CSS custom properties
Grouped by semantic target and state from --c2-<component>__<target>__<state>--<property>. Click a group to collapse it; click a name to copy it.
| Name | Type | Default | Description |
|---|---|---|---|
--c2-image-cropper__image--max-height | pixel | 360px | Largest height the image renders at; it is scaled down to fit the width as well. |
--c2-image-cropper__frame--min-height | pixel | 160px | Height of the frame while there is no image. |
--c2-image-cropper__frame--background | background | #f4f4f5 | Shows through transparent pixels and around an image narrower than the host. |
--c2-image-cropper__frame--border | border | 1px solid #e4e4e7 | |
--c2-image-cropper__frame--border-radius | border-radius | 8px | |
--c2-image-cropper__frame--padding | pixel | 0px | Room around the image, so the handles on its edges stay inside the frame. |
--c2-image-cropper__overlay--color | color | rgba(0, 0, 0, 0.55) | Dims the image outside the selection. |
--c2-image-cropper__selection--border | border | 1px solid #ffffff | |
--c2-image-cropper__selection__focus--outline | outline | 2px solid rgba(2, 101, 220, 0.4) | Drawn around the selection while it is keyboard-focused. |
--c2-image-cropper__guides--display | display | block | none hides the rule-of-thirds lines. |
--c2-image-cropper__guides--color | color | rgba(255, 255, 255, 0.5) | |
--c2-image-cropper__handle--size | pixel | 12px | |
--c2-image-cropper__handle--background | color | #ffffff | |
--c2-image-cropper__handle--border | border | 1px solid #a1a1aa | |
--c2-image-cropper__handle--border-radius | border-radius | 999px | |
--c2-image-cropper__handle--box-shadow | box-shadow | 0 1px 2px rgba(0, 0, 0, 0.3) | |
Text and languages
The text the component shows, in English. It shows another language when the app adds one with addTranslations, under image-cropper, and the nearest lang asks for it; any message not translated stays in English. {0}, {1}… stand for the values a message carries. Languages guide.
| Key | English | Values |
|---|---|---|
label | Crop area | — |
roleDescription | crop selection | — |
instructions | Arrow keys move the selection, Shift + Arrow keys move it further. Alt + Arrow keys resize it, plus and minus scale it. | — |
status | {0} × {1} pixels at {2}, {3} | {0} width {1} height {2} x {3} y |