v22.18.0 release notes

ImageCropper

ImageCropper crops an image with a fixed crop area over an image that moves, zooms and rotates underneath, with the mouse, a finger or the keyboard, and gives the cropped image as a Blob. Free, without any dependency.

View as .md
import { ImageCropper } from '@exhaustive-ui/core/imagecropper';

ImageCropper takes an image in src: an URL, a File or a Blob. The crop area stays fixed in the middle and the image moves and zooms underneath: drag it with the mouse or a finger, zoom with the wheel, the touchpad or a pinch. The image always covers the crop area. [(crop)] gives the cropped area in pixels of the source image, with its percentages, on every change.

Loading the image
The arrow keys move the crop area over the image, further with Shift. Plus and minus, or Page Up and Page Down, zoom; Home and End zoom out and in fully. The left and right square brackets rotate the image.

aspectRatio is the width of the crop area divided by its height, 4 / 3 by default. With 'free', the crop area has handles on its sides and corners, and Alt with the arrow keys resizes it; it stays centered, so it grows on both sides. minCropSize and maxCropSize limit the cropped area in pixels of the source image, and so the zoom.

16:94:31:1FreeAspect ratio
Loading the image
The arrow keys move the crop area over the image, further with Shift. Plus and minus, or Page Up and Page Down, zoom; Home and End zoom out and in fully. The left and right square brackets rotate the image.

cropShape="round" shows a round crop area, for an avatar. With an aspectRatio other than 1 it is an ellipse. The cropped image is cut to the same shape, with transparent corners: keep a type with transparency, such as PNG or WebP, or pass round: false to getCroppedImage to keep the corners. showGrid hides the rule of thirds grid.

Loading the image
The arrow keys move the crop area over the image, further with Shift. Plus and minus, or Page Up and Page Down, zoom; Home and End zoom out and in fully. The left and right square brackets rotate the image.

zoom and rotation are models, bound two-way with sliders here. The zoom stays between minZoom and maxZoom, 1 and 3 by default, and never below what keeps the crop area covered: a rotated image zooms in by itself. The rotate method turns the image by a number of degrees and keeps the rotation between -180 and 180; flipHorizontal and flipVertical mirror it, and reset brings everything back.

Loading the image
The arrow keys move the crop area over the image, further with Shift. Plus and minus, or Page Up and Page Down, zoom; Home and End zoom out and in fully. The left and right square brackets rotate the image.
Zoom 100%
Rotation 0°

getCroppedDataUrl draws the cropped area, with its rotation and flip, and gives it as a data URL, here at most 160 pixels wide for a live thumbnail updated on every cropChange. onCropComplete is emitted once an interaction ends, when the pointer is released or the wheel and the keys stop: the place for heavier work.

Loading the image
The arrow keys move the crop area over the image, further with Shift. Plus and minus, or Page Up and Page Down, zoom; Home and End zoom out and in fully. The left and right square brackets rotate the image.
Interactions completed: 0

A File picked with FileInput, or FileUpload, goes straight into src: the component creates its object URL and revokes it when the file changes. getCroppedImage gives the result as a Blob, here a JPEG at most 1024 pixels wide, ready to be sent in a FormData.

ImageCropper is not a form control: its value would be the crop rectangle, while a form wants the cropped file, which only exists once getCroppedImage resolves. Set your form control with that Blob, or keep the [(crop)] rectangle and crop on the server.

Drop files here or browse

The empty template replaces the message shown without an image, loading the spinner shown while it loads, and error the message shown when it can not be loaded; the error template receives the source and a reload function. onImageLoad gives the size of the image, and onImageError the failed source.

Choose a photo to crop
The arrow keys move the crop area over the image, further with Shift. Plus and minus, or Page Up and Page Down, zoom; Home and End zoom out and in fully. The left and right square brackets rotate the image.

Screen Reader

The crop area is a focusable element with the application role, so that screen readers pass the arrow keys to it, and the "image cropper" role description. Its name is "Crop area" by default; give it a better one with ariaLabel or ariaLabelledBy. It is described with aria-describedby by the keyboard instructions, which mention resizing when the aspect ratio is free. The image takes its text alternative from alt, empty by default. The grid and the resize handles are hidden from assistive technologies.

A polite live region announces the result of every key: "Crop area at 25% from the left and 40% from the top", "Zoom 150%", "Rotation 90 degrees" or "Crop area 640 by 480 pixels". The loading message is a status, and the error message an alert. Every text can be translated with labels. The arrow keys and the coordinates are physical directions, also in right-to-left layouts, since the image itself is not mirrored. Zoom and rotation changes made with the keyboard or the methods are animated, except when the user prefers reduced motion.

<label id="avatar-label">Profile picture</label>
<p-image-cropper [src]="file" ariaLabelledBy="avatar-label" alt="Your photo" [labels]="{ zoom: 'Zoom à {0} %', rotation: 'Rotation de {0} degrés' }" />

Keyboard Support

KeyFunction
tabMoves focus to the crop area. Clicking or touching the image also focuses it.
arrow keysMove the crop area over the image by keyboardStep pixels, 8 by default; the image moves the other way and stays over the crop area.
shift + arrow keysMove the crop area ten times further.
alt + arrow keysWith a free aspect ratio, resize the crop area: right and up make it wider and taller, left and down narrower and shorter.
+ / page upZooms in by zoomStep, 0.1 by default.
- / page downZooms out by zoomStep.
home / endZooms out fully or in fully.
[ / ]Rotates the image counterclockwise or clockwise by rotationStep, 90 degrees by default.
shift + [ / ]Rotates the image by one degree.