v22.18.0 release notes

ImageEditor

ImageEditor edits an image in the browser: crop, rotate, flip, resize, color adjustments, filters and annotations, with undo and redo, zoom and pan, keyboard and touch support, and the result as a Blob. Free, without any dependency.

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

ImageEditor edits an image in the browser, on a canvas and without any dependency: crop, rotate, flip, resize, adjust the colors, apply a filter and draw annotations. The toolbar opens a tool panel beside the image, or in a bottom sheet on narrow containers. The original image is never changed: [(state)] holds the list of edits, which can be undone and redone, saved and applied again later.

Loading the image
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.

0 operations, 0 annotations

src takes an URL, a data URL, a File or a Blob, for instance the file picked with FileInput below. Files and Blobs are decoded with createImageBitmap and their EXIF orientation, so a photo taken in portrait stays upright. Without a source, the stage is a drop zone with an open button, and the open button of the toolbar shows the file dialog of the browser; load() and open() do the same from code.

A remote image can only be edited when its server sends CORS headers: set crossOrigin to anonymous. Without them the browser taints the canvas, and the editor shows a clear error, emits onImageError with the tainted reason and offers to retry. The loading, error and empty templates replace the default messages.

Drop files here or browse
Drop an image here or open a file
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.
<p-image-editor src="https://cdn.example.com/photo.jpg" crossOrigin="anonymous" (onImageError)="onError($event)">
    <ng-template #error let-reason="reason" let-reload="reload">
        <span>{{ reason === 'tainted' ? 'This image can not be edited.' : 'The image could not be loaded.' }}</span>
        <button type="button" (click)="reload()">Retry</button>
    </ng-template>
</p-image-editor>

The crop tool is the ImageCropper component working on a snapshot of the edited image: the crop area moves, resizes with a free ratio and zooms with the mouse, a finger or the keyboard, and Apply adds a crop operation. The default presets are Free, Original, Square, 4:3, 3:2, 16:9, 3:4, 9:16 and Circle, which keeps the inscribed ellipse with transparent corners. cropPresets replaces them with your own ratios; a label that is a key of the labels, such as cropSquare, is translated. [(tool)] opens a tool from code, here the crop tool.

Loading the image
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.

The rotate and flip tool turns the image by quarter turns and flips it horizontally or vertically; the straighten slider adds a fine angle from -45 to 45 degrees, and the image grows to the bounding box of the rotated image with transparent corners, which a crop removes. Consecutive rotations merge into one operation, and a flip done twice cancels itself. The resize tool scales the image to a width and a height, with the aspect ratio kept by default. The annotations follow every geometric operation. The same edits are available as methods: rotate(degrees), flip(direction), crop(rect, shape) and resize(width, height).

Loading the image
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.

The adjust tool has labelled sliders for the brightness, contrast, saturation, exposure, temperature, hue, grayscale, sepia, blur and sharpness, neutral at 0. The moves of one slider make a single step of the undo history. The color adjustments are combined into one color matrix, so that they cost one pass over the pixels whatever their number.

The filters tool shows presets with a thumbnail of the image: None, Vivid, Warm, Cool, Mono, Faded, Vintage, Dramatic and Soft. A preset sets the adjustments, which can then be tuned. filterPresets replaces or extends the list, here with a Night preset, and applyFilter(name) and setAdjustments(values) do the same from code.

Loading the image
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.

The annotate tool draws over the image with a pen, a line, an arrow, a rectangle, an ellipse, a text or a redaction that pixelates a region. Pick a drawing tool and drag on the image; Shift keeps lines at 45 degree steps and rectangles square. The select tool picks an annotation to move it, resize it with its handles, change its color with the ColorPicker, its stroke width, its fill, its text, font and size, or delete it. The panel also lists the annotations, to select or delete them without a pointer. With the keyboard, Enter on the image adds a shape of the chosen kind in the middle of the view.

Annotations are part of the state, in pixels of the edited image, so they can be saved and restored, here an arrow and a label. defaultColor and defaultStrokeWidth set the style of the new ones, and addAnnotation() and removeAnnotation() edit them from code.

Loading the image
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.

toolbar lists the items of the toolbar in order: the tools crop, transform, resize, adjust, filters and annotate, the actions open, undo, redo, reset, zoomOut, zoomIn, fit, actualSize, download and save, and separator. Any other name is a custom item rendered by the toolbaritem template, which receives the name and the editor to call its methods. Custom buttons join the arrow key navigation of the toolbar. An empty array hides the toolbar, to drive the editor with your own controls.

Loading the image
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.

The edits are drawn again at full resolution for the export: toBlob(type, quality, size) gives a Blob, toDataURL() a data URL, toCanvas() a canvas and download(filename, type) saves a file. The size takes a width, a height, a scale, a maxWidth or a maxHeight; a JPEG gets a white background where the image is transparent.

The save button of the toolbar exports with exportOptions and emits onSave with the Blob, its size and the state, ready for a FormData. Here the result is a JPEG at most 1200 pixels wide.

Loading the image
Plus and minus zoom, 0 fits the image and 1 shows it at its actual size; the arrow keys move a zoomed image. With the annotate tool, Enter adds a shape of the chosen kind, the square brackets select the next or the previous annotation, the arrow keys move it, Alt with an arrow key resizes it, further with Shift, Delete removes it and Escape unselects it. Control Z undoes, Control Shift Z or Control Y redoes.

Screen Reader

The toolbar has the toolbar role and a single tab stop: the arrow keys move between its buttons, which have a name and a tooltip; the tool buttons are toggle buttons with aria-pressed, and unavailable actions are aria-disabled but stay reachable. The image is shown in a focusable stage with the application role and the "image editor" role description, named "Image" by default or by ariaLabel and ariaLabelledBy, and described by a text that gives the size of the edited image, the alt text and the list of annotations, and by the keyboard instructions. The canvas itself is hidden from assistive technologies.

The tool panel is a group named by its title. Its sliders are labelled and give their value, the crop presets, filters and drawing tools are radio groups moved with the arrow keys, and the annotations are listed as buttons. A polite live region announces each change: "Rotated 90°", "Crop 1200 × 800", "Filter Mono", "Arrow added", "Moved to 120, 80", "Undone: Crop" or "Zoom 150%". Every text can be translated with labels, or for the whole application with the imageEditor key of the translation. The editor has no animation of its own, and the cropper animates its keyboard changes only when the user does not prefer reduced motion.

<span id="photo-label">Profile photo</span>
<p-image-editor [src]="file" ariaLabelledBy="photo-label" alt="Your photo" [labels]="{ rotated: 'Rotation de {0}°', undo: 'Annuler' }" />

Toolbar Keyboard Support

KeyFunction
tabMoves focus into the toolbar, on the last focused button, then to the image and the tool panel.
right arrow / left arrowMoves focus to the next or the previous button, in the reading direction, and wraps around.
home / endMoves focus to the first or the last button.
enter / spaceOpens or closes a tool, or runs an action.
escapeIn the tool panel, closes it and moves focus back to its toolbar button.

Image Keyboard Support

KeyFunction
+ / -Zooms in or out.
0 / 1Fits the image in the stage, or shows it at its actual size.
arrow keysMove a zoomed image, or the selected annotation by 8 pixels of the screen, ten times further with shift.
alt + arrow keysResize the selected annotation: right and down make it larger, left and up smaller; they move the end of a line or an arrow.
enterWith the annotate tool, adds a shape of the chosen drawing tool in the middle of the view and selects it.
] / [With the annotate tool, selects the next or the previous annotation.
delete / backspaceDeletes the selected annotation.
escapeUnselects the annotation, or closes the annotate tool.
ctrl / ⌘ + zUndoes the last edit, anywhere in the editor except in a text field.
ctrl / ⌘ + shift + z, ctrl + yRedoes the last undone edit.

Crop Keyboard Support

The crop area of the crop tool is an ImageCropper: the arrow keys move it, further with shift, alt with the arrow keys resizes a free crop, and + and - zoom.

ImageEditor works on the canvas of the browser, without a server and without a dependency, which sets a few limits:

  • the image shown while editing is at most previewMaxSize pixels on its longest side, 2048 by default, so that the sliders stay fluid; the export always uses the full resolution, which can take a moment for very large photos;
  • the largest image is the largest canvas of the browser, about 16384 pixels on a side or 268 million pixels in Chromium and Firefox, and less on iOS Safari;
  • a remote image needs CORS headers, and the exported file has no EXIF data: the orientation is applied, the camera details and the location are dropped;
  • the blur uses the canvas filter of the browser where it exists and a rougher downscale elsewhere, so it can look slightly different between browsers;
  • annotations can be moved, resized, restyled and deleted, but not rotated by the user, they follow the rotations of the image; text has a single font, size and color per annotation;
  • the pen draws with a pointer only, every other annotation can also be added with the keyboard;
  • there is no layer of images, no perspective correction, no curves or levels, and no healing or clone tool;
  • during the server rendering, the editor shows its message and no image, which loads once the page runs in the browser.