# Angular Image Editor Component

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.

## Basic

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.

```typescript
import { Component } from '@angular/core';
import { ImageEditorModule, ImageEditorState } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card">
            <p-image-editor src="/demo/galleria/galleria1.jpg" alt="A mountain lake" [(state)]="state" />
            <p class="mt-4 mb-0 text-sm text-muted-color">{{ state?.operations?.length ?? 0 }} operations, {{ state?.annotations?.length ?? 0 }} annotations</p>
        </div>
    `,
    standalone: true,
    imports: [ImageEditorModule]
})
export class ImageeditorBasicDemo {
    state: ImageEditorState | null = null;
}
```

## Loading Images

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.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FileInputModule } from '@exhaustive-ui/core/fileinput';
import { ImageEditorModule } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card flex flex-col gap-4">
            <p-file-input [(ngModel)]="file" accept="image/*" [multiple]="false" />
            <p-image-editor [src]="file" ariaLabel="Your image" />
        </div>
    `,
    standalone: true,
    imports: [FormsModule, FileInputModule, ImageEditorModule]
})
export class ImageeditorLoadingDemo {
    file: File | null = null;
}
```

## Crop

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.

```typescript
import { Component } from '@angular/core';
import { ImageEditorCropPreset, ImageEditorModule } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card">
            <p-image-editor src="/demo/galleria/galleria2.jpg" alt="A forest" tool="crop" [cropPresets]="presets" [toolbar]="['crop', 'separator', 'undo', 'redo', 'reset']" />
        </div>
    `,
    standalone: true,
    imports: [ImageEditorModule]
})
export class ImageeditorCropDemo {
    presets: ImageEditorCropPreset[] = [
        { label: 'cropSquare', ratio: 1 },
        { label: 'Banner 3:1', ratio: 3 },
        { label: 'Story 9:16', ratio: 9 / 16 },
        { label: 'Avatar', ratio: 1, shape: 'circle' }
    ];
}
```

## Transform

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) .

```typescript
import { Component } from '@angular/core';
import { ImageEditorModule } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card">
            <p-image-editor src="/demo/galleria/galleria3.jpg" alt="A coast" tool="transform" [toolbar]="['transform', 'resize', 'separator', 'undo', 'redo', 'reset', 'separator', 'zoomOut', 'zoomIn', 'fit']" />
        </div>
    `,
    standalone: true,
    imports: [ImageEditorModule]
})
export class ImageeditorTransformDemo {
}
```

## Adjustments and Filters

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.

```typescript
import { Component } from '@angular/core';
import { ImageEditorFilterPreset, ImageEditorModule, IMAGEEDITOR_FILTER_PRESETS } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card">
            <p-image-editor src="/demo/galleria/galleria4.jpg" alt="A valley" tool="filters" [filterPresets]="presets" [toolbar]="['adjust', 'filters', 'separator', 'undo', 'redo', 'reset']" />
        </div>
    `,
    standalone: true,
    imports: [ImageEditorModule]
})
export class ImageeditorAdjustmentsDemo {
    presets: ImageEditorFilterPreset[] = [...IMAGEEDITOR_FILTER_PRESETS, { name: 'night', label: 'Night', adjustments: { exposure: -35, temperature: -40, saturation: -20, contrast: 15 } }];
}
```

## Annotations

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.

```typescript
import { Component } from '@angular/core';
import { ImageEditorModule, ImageEditorState } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card">
            <p-image-editor src="/demo/galleria/galleria5.jpg" alt="A lake" tool="annotate" [(state)]="state" defaultColor="#facc15" [toolbar]="['annotate', 'separator', 'undo', 'redo', 'reset', 'separator', 'zoomOut', 'zoomIn', 'fit']" />
        </div>
    `,
    standalone: true,
    imports: [ImageEditorModule]
})
export class ImageeditorAnnotationsDemo {
    state: ImageEditorState = {
        operations: [],
        adjustments: { brightness: 0, contrast: 0, saturation: 0, exposure: 0, temperature: 0, hue: 0, grayscale: 0, sepia: 0, blur: 0, sharpen: 0 },
        annotations: [
            {
                id: 'arrow',
                type: 'arrow',
                points: [
                    { x: 180, y: 120 },
                    { x: 330, y: 250 }
                ],
                color: '#facc15',
                strokeWidth: 8
            },
            { id: 'label', type: 'text', x: 60, y: 60, width: 260, height: 50, rotation: 0, text: 'Our next stop', fontSize: 40, fontFamily: 'system-ui, sans-serif', color: '#ffffff', strokeWidth: 1 }
        ]
    };
}
```

## Toolbar Customization

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.

```typescript
import { Component } from '@angular/core';
import { ImageEditorModule } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card">
            <p-image-editor src="/demo/galleria/galleria6.jpg" alt="A mountain" [toolbar]="['crop', 'transform', 'separator', 'mono', 'warm', 'separator', 'undo', 'redo', 'separator', 'download']">
                <ng-template #toolbaritem let-item let-editor="editor">
                    <button type="button" class="px-3 h-9 rounded-border text-sm hover:bg-emphasis" (click)="editor.applyFilter(item)">{{ item === 'mono' ? 'Mono' : 'Warm' }}</button>
                </ng-template>
            </p-image-editor>
        </div>
    `,
    standalone: true,
    imports: [ImageEditorModule]
})
export class ImageeditorToolbarDemo {
}
```

## Export and Upload

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.

```typescript
import { DecimalPipe } from '@angular/common';
import { Component, DestroyRef, inject, signal } from '@angular/core';
import { ImageEditorExportOptions, ImageEditorModule, ImageEditorSaveEvent } from '@exhaustive-ui/core/imageeditor';

@Component({
    template: `
        <div class="card flex flex-col gap-4">
            <p-image-editor
                src="/demo/galleria/galleria7.jpg"
                alt="A river"
                [exportOptions]="exportOptions"
                filename="edited-photo"
                [toolbar]="['crop', 'transform', 'filters', 'separator', 'undo', 'redo', 'separator', 'download', 'save']"
                (onSave)="onSave($event)"
            />
            @if (result(); as saved) {
                <div class="flex flex-wrap items-start gap-4">
                    <img [src]="saved.url" alt="The saved image" class="max-w-full rounded-border" style="width: 12rem" />
                    <span class="text-sm">{{ saved.width }} × {{ saved.height }} JPEG, {{ saved.size / 1024 | number: '1.0-1' }} KB</span>
                </div>
            }
        </div>
    `,
    standalone: true,
    imports: [DecimalPipe, ImageEditorModule]
})
export class ImageeditorExportDemo {
    exportOptions: ImageEditorExportOptions = { type: 'image/jpeg', quality: 0.9, maxWidth: 1200 };

    result = signal<{ url: string; size: number; width: number; height: number } | null>(null);

    constructor() {
        inject(DestroyRef).onDestroy(() => this.revoke());
    }

    onSave(event: ImageEditorSaveEvent) {
        this.revoke();
        this.result.set({ url: URL.createObjectURL(event.blob), size: event.blob.size, width: event.width, height: event.height });
        // const body = new FormData();
        // body.append('photo', event.blob, 'photo.jpg');
        // body.append('edits', JSON.stringify(event.state));
    }

    private revoke() {
        const current = this.result();
        if (current) URL.revokeObjectURL(current.url);
    }
}
```

## Accessibility

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.

## Known Limitations

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.

## Pass Through Options

| Name | Type | Description |
|------|------|-------------|
| root | PassThroughOption<HTMLElement, I> | Used to pass attributes to the root's DOM element. |
| toolbar | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the toolbar. |
| toolbarButton | PassThroughOption<HTMLButtonElement, I> | Used to pass attributes to a button of the toolbar. |
| toolbarSeparator | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to a separator of the toolbar. |
| zoomLevel | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the zoom level shown in the toolbar. |
| body | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the area below the toolbar, holding the stage and the tool panel. |
| stage | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the focusable stage that shows the image. |
| canvas | PassThroughOption<HTMLCanvasElement, I> | Used to pass attributes to the canvas. |
| panel | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the tool panel, a side panel on wide screens and a bottom sheet on small ones. |
| panelHeader | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the header of the tool panel. |
| panelTitle | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the title of the tool panel. |
| panelContent | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the content of the tool panel. |
| section | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to a group of fields in the tool panel. |
| label | PassThroughOption<HTMLElement, I> | Used to pass attributes to a label in the tool panel. |
| button | PassThroughOption<HTMLButtonElement, I> | Used to pass attributes to a button of the tool panel. |
| option | PassThroughOption<HTMLButtonElement, I> | Used to pass attributes to a choice of a group, such as a crop preset, a filter or an annotation type. |
| layers | PassThroughOption<HTMLUListElement, I> | Used to pass attributes to the list of annotations. |
| layer | PassThroughOption<HTMLLIElement, I> | Used to pass attributes to an item of the list of annotations. |
| loading | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the loading message. |
| error | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the error message. |
| empty | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the message shown without an image, which is also a drop zone. |

## Theming

### CSS Classes

| Class | Description |
|-------|-------------|
| p-imageeditor | Class name of the root element |
| p-imageeditor-toolbar | Class name of the toolbar |
| p-imageeditor-toolbar-button | Class name of a toolbar button |
| p-imageeditor-toolbar-separator | Class name of a toolbar separator |
| p-imageeditor-zoom-level | Class name of the zoom level |
| p-imageeditor-body | Class name of the area below the toolbar |
| p-imageeditor-stage | Class name of the stage |
| p-imageeditor-canvas | Class name of the canvas |
| p-imageeditor-cropper | Class name of the cropper of the crop tool |
| p-imageeditor-panel | Class name of the tool panel |
| p-imageeditor-panel-header | Class name of the header of the tool panel |
| p-imageeditor-panel-title | Class name of the title of the tool panel |
| p-imageeditor-panel-content | Class name of the content of the tool panel |
| p-imageeditor-section | Class name of a group of fields |
| p-imageeditor-label | Class name of a label |
| p-imageeditor-button | Class name of a button of the tool panel |
| p-imageeditor-option | Class name of a choice of a group |
| p-imageeditor-filter | Class name of a filter preset |
| p-imageeditor-layers | Class name of the list of annotations |
| p-imageeditor-layer | Class name of an item of the list of annotations |
| p-imageeditor-loading | Class name of the loading message |
| p-imageeditor-error | Class name of the error message |
| p-imageeditor-empty | Class name of the message shown without an image |

### Design Tokens

| Token | CSS Variable | Description |
|-------|--------------|-------------|
| imageeditor.background | --p-imageeditor-background | Background of the toolbar and the panel |
| imageeditor.border.color | --p-imageeditor-border-color | Border color of the component |
| imageeditor.border.radius | --p-imageeditor-border-radius | Border radius of the component |
| imageeditor.color | --p-imageeditor-color | Text color of the component |
| imageeditor.height | --p-imageeditor-height | Height of the component |
| imageeditor.transition.duration | --p-imageeditor-transition-duration | Duration of the transitions |
| imageeditor.disabled.opacity | --p-imageeditor-disabled-opacity | Opacity of a disabled toolbar button |
| imageeditor.toolbar.padding | --p-imageeditor-toolbar-padding | Padding of the toolbar |
| imageeditor.toolbar.gap | --p-imageeditor-toolbar-gap | Gap between the items of the toolbar |
| imageeditor.toolbar.button.size | --p-imageeditor-toolbar-button-size | Size of a toolbar button |
| imageeditor.toolbar.button.border.radius | --p-imageeditor-toolbar-button-border-radius | Border radius of a toolbar button |
| imageeditor.toolbar.button.color | --p-imageeditor-toolbar-button-color | Color of a toolbar button |
| imageeditor.toolbar.button.hover.color | --p-imageeditor-toolbar-button-hover-color | Color of a hovered toolbar button |
| imageeditor.toolbar.button.hover.background | --p-imageeditor-toolbar-button-hover-background | Background of a hovered toolbar button |
| imageeditor.toolbar.button.active.color | --p-imageeditor-toolbar-button-active-color | Color of the button of the open tool |
| imageeditor.toolbar.button.active.background | --p-imageeditor-toolbar-button-active-background | Background of the button of the open tool |
| imageeditor.toolbar.button.icon.size | --p-imageeditor-toolbar-button-icon-size | Size of the icons |
| imageeditor.toolbar.button.focus.ring.width | --p-imageeditor-toolbar-button-focus-ring-width | Focus ring width of a toolbar button |
| imageeditor.toolbar.button.focus.ring.style | --p-imageeditor-toolbar-button-focus-ring-style | Focus ring style of a toolbar button |
| imageeditor.toolbar.button.focus.ring.color | --p-imageeditor-toolbar-button-focus-ring-color | Focus ring color of a toolbar button |
| imageeditor.toolbar.button.focus.ring.offset | --p-imageeditor-toolbar-button-focus-ring-offset | Focus ring offset of a toolbar button |
| imageeditor.toolbar.button.focus.ring.shadow | --p-imageeditor-toolbar-button-focus-ring-shadow | Focus ring shadow of a toolbar button |
| imageeditor.panel.width | --p-imageeditor-panel-width | Width of the side panel |
| imageeditor.panel.padding | --p-imageeditor-panel-padding | Padding of the panel |
| imageeditor.panel.gap | --p-imageeditor-panel-gap | Gap between the groups of the panel |
| imageeditor.panel.sheet.max.height | --p-imageeditor-panel-sheet-max-height | Maximum height of the bottom sheet on small containers |
| imageeditor.panel.sheet.shadow | --p-imageeditor-panel-sheet-shadow | Shadow of the bottom sheet |
| imageeditor.label.color | --p-imageeditor-label-color | Color of the labels |
| imageeditor.label.font.size | --p-imageeditor-label-font-size | Font size of the labels |
| imageeditor.label.font.weight | --p-imageeditor-label-font-weight | Font weight of the labels |
| imageeditor.value.color | --p-imageeditor-value-color | Color of the slider values and the hints |
| imageeditor.value.font.size | --p-imageeditor-value-font-size | Font size of the slider values and the hints |
| imageeditor.option.padding | --p-imageeditor-option-padding | Padding of a choice |
| imageeditor.option.border.radius | --p-imageeditor-option-border-radius | Border radius of a choice and a button |
| imageeditor.option.border.color | --p-imageeditor-option-border-color | Border color of a choice and a button |
| imageeditor.option.background | --p-imageeditor-option-background | Background of a choice and a button |
| imageeditor.option.color | --p-imageeditor-option-color | Color of a choice and a button |
| imageeditor.option.hover.background | --p-imageeditor-option-hover-background | Background of a hovered choice or button |
| imageeditor.option.checked.background | --p-imageeditor-option-checked-background | Background of a checked choice |
| imageeditor.option.checked.color | --p-imageeditor-option-checked-color | Color of a checked choice |
| imageeditor.option.checked.border.color | --p-imageeditor-option-checked-border-color | Border color of a checked choice |
| imageeditor.option.focus.ring.width | --p-imageeditor-option-focus-ring-width | Focus ring width of a choice or a button |
| imageeditor.option.focus.ring.style | --p-imageeditor-option-focus-ring-style | Focus ring style of a choice or a button |
| imageeditor.option.focus.ring.color | --p-imageeditor-option-focus-ring-color | Focus ring color of a choice or a button |
| imageeditor.option.focus.ring.offset | --p-imageeditor-option-focus-ring-offset | Focus ring offset of a choice or a button |
| imageeditor.option.focus.ring.shadow | --p-imageeditor-option-focus-ring-shadow | Focus ring shadow of a choice or a button |
| imageeditor.primary.button.background | --p-imageeditor-primary-button-background | Background of the apply button |
| imageeditor.primary.button.color | --p-imageeditor-primary-button-color | Color of the apply button |
| imageeditor.primary.button.hover.background | --p-imageeditor-primary-button-hover-background | Background of the hovered apply button |
| imageeditor.stage.background | --p-imageeditor-stage-background | Background around the image |
| imageeditor.stage.focus.ring.width | --p-imageeditor-stage-focus-ring-width | Focus ring width of the stage |
| imageeditor.stage.focus.ring.style | --p-imageeditor-stage-focus-ring-style | Focus ring style of the stage |
| imageeditor.stage.focus.ring.color | --p-imageeditor-stage-focus-ring-color | Focus ring color of the stage |
| imageeditor.stage.focus.ring.offset | --p-imageeditor-stage-focus-ring-offset | Focus ring offset of the stage |
| imageeditor.stage.focus.ring.shadow | --p-imageeditor-stage-focus-ring-shadow | Focus ring shadow of the stage |
| imageeditor.message.gap | --p-imageeditor-message-gap | Gap of the messages |
| imageeditor.message.padding | --p-imageeditor-message-padding | Padding of the messages |
| imageeditor.message.color | --p-imageeditor-message-color | Text color of the messages |
| imageeditor.message.font.size | --p-imageeditor-message-font-size | Font size of the messages |
| imageeditor.message.drag.border.color | --p-imageeditor-message-drag-border-color | Border color of the drop zone while an image is dragged over it |
| imageeditor.error.icon.color | --p-imageeditor-error-icon-color | Color of the error icon |

