Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

image-editor

<image-editor> lets a visitor crop, rotate, flip, resize and filter an image in the browser, then export it. It reads Vanilla Breeze design tokens and follows the active theme and light or dark mode with no setup.

At a glance

Package@profpowell/image-editor 0.2.0
Elements<image-editor>, the editor. <image-convert>, a smaller element that only converts an image to another format. Each is a separate file.
Renders inShadow DOM
DependenciesNone
Sourcegithub.com/ProfPowell/image-editor
Documentationprofpowell.github.io/image-editor
LicenceMIT
In Vanilla BreezeNot bundled. Load it yourself.

Install

Load the editor from a CDN with one script tag. Add the second tag only if you use <image-convert>.

Or install it from npm and import it in your own script.

Use 0.2.0 or later. Version 0.1.0 could run script hidden in an image's metadata, and 0.2.0 fixes that.

Demo

The editor opens with a photo loaded. Try the crop, rotate and filter tools, then undo. The line under the editor reports the events it fires.

Usage

Load an image and set the export format. The editor is 500 pixels tall unless you set height.

Leave out src to start with an empty drop zone. Content inside the element replaces the default message, and aspect-ratio locks the crop shape.

Read the edited image from script, for example to upload it.

Use <image-convert> when visitors only need to change an image's format. They drop a file, pick a format and download the result.

Reference

Attributes

Attributes of <image-editor>:

AttributeValuesDefaultPurpose
srcURLNoneThe image to load.
modelight, darkFollows the page, then the system settingForces light or dark mode.
widthNumber of pixelsFills its containerEditor width.
heightNumber of pixels500Editor height.
formatpng, jpeg, webp, avifpngThe export format. PNG is used if the browser cannot write the format you ask for.
quality0 to 10.92Export quality for JPEG, WebP and AVIF.
max-historyNumber50The most undo steps kept. History is also limited to 512 MB.
readonlyBooleanOffTurns off editing. The image can still be viewed.
no-toolbarBooleanOffHides the toolbar, for an editor driven from script. The empty drop zone still opens the file chooser.
aspect-ratioWidth and height, such as 16:9NoneLocks the crop to that shape.

Attributes of <image-convert>:

AttributeValuesDefaultPurpose
modelight, darkFollows the page, then the system settingForces light or dark mode.
formatpng, jpeg, webp, avifwebpThe output format selected at the start.
quality0 to 10.85Output quality for JPEG, WebP and AVIF.
acceptA file type list, as on a file inputimage/*The file types the visitor can choose.

Methods and properties

Members of <image-editor>. The attributes above are also available as properties: src, mode, format, quality, maxHistory, readonly, noToolbar and aspectRatio.

MemberPurpose
loadImage(source)Loads an image from a URL, a File or a Blob. Returns a promise.
crop(x, y, w, h), resize(w, h)Crop to a region, or resize, in pixels.
rotate(degrees), flipHorizontal(), flipVertical()Rotate in steps of 90 degrees, or mirror the image.
applyFilter(name, value), resetFilters()Apply or clear filters. Names and ranges: brightness and contrast 0.5 to 2, saturate 0 to 3, blur 0 to 10 pixels, grayscale and sepia 0 to 100 percent.
undo(), redo(), reset()Step through the edit history, or return to the image as loaded.
zoomTo(level), zoomToFit()Set the zoom, where 1 is 100 percent, or fit the image in the workspace.
getBlob(format?, quality?), getDataURL(format?, quality?)Return the edited image as a Blob (in a promise) or as a data URL.
download(filename?, format?, quality?)Saves the edited image through the browser's download.
imageWidth, imageHeight, hasImage, isDirtyRead-only. The image size in pixels, whether an image is loaded, and whether it has been edited.
canUndo, canRedo, historyLength, zoomLevel, currentToolRead-only. The state of the history, the zoom and the open tool.
sourceFormat, exifDataRead-only. The file type of the loaded image and its EXIF metadata (data a camera stores in the file). The metadata comes from the file, so treat it as untrusted.
supportedFormats, supportsFiltersRead-only. The formats this browser can export, and whether it can apply filters. Where it cannot, the Filters tool is hidden and applyFilter() does nothing.

<image-convert> has the properties mode, format, quality, accept and the read-only supportedFormats, and one method, loadFile(file).

Events

EventDetailFires when
image-load{ width, height, src }An image has loaded.
image-load-error{ src, error }An image failed to load.
image-edit{ action, params }An edit is applied.
image-export{ format, quality, size }The image is exported.
tool-change{ tool, previous }A tool is opened or closed.
zoom-change{ level }The zoom changes.
history-change{ canUndo, canRedo, length }The undo history changes.
convert-complete{ sourceFormat, outputFormat, sourceSize, outputSize, filename }<image-convert> has converted and downloaded a file.
convert-error{ filename, error }<image-convert> could not read a file as an image.

CSS custom properties

Every property of the editor starts with --image-editor-. The table groups them by the part they style and lists the rest of each name.

PartProperties, after --image-editor-
Framebg, border-color, border-radius, margin, font-family
Texttext-color, text-muted
Accent and focusaccent-color, on-accent-color, focus-color, slider-thumb
Toolbartoolbar-bg, toolbar-border, toolbar-padding
Buttonsbutton-bg, button-color, button-hover-bg, button-active-bg, button-active-color, button-radius, button-disabled-opacity
Tool options barcontext-bg, context-border
Workspaceworkspace-bg, workspace-check-color
Cropcrop-border, crop-handle-color, crop-grid-color, crop-overlay
Status barstatus-bg, status-color, status-font-size

<image-convert> has nine properties with the prefix --image-convert-: bg, border-color, border-radius, margin, font-family, text-color, text-muted, accent-color and on-accent-color.

See the image-editor API reference for the full detail.

With Vanilla Breeze

The editor reads Vanilla Breeze tokens directly. Custom properties inherit into the shadow DOM, so no setup is needed. Theme and mode changes apply at once, without losing edits.

TokenDrives
--color-surfaceEditor background
--color-surface-raisedToolbar, tool options bar and status bar background
--color-surface-sunkenWorkspace behind the image
--color-border, --border-width-thinBorder color and width
--color-text, --color-text-mutedText; button icons and the status bar
--color-interactive, then --color-primaryAccent color: the active tool, crop border and handles, slider thumbs
--color-text-on-interactive-auto, then --color-text-on-primaryText on accent-colored buttons
--color-hover-bgButton hover background
--focus-ring-color, then --color-focus-ring; --focus-ring-widthFocus outline
--font-sans, --font-size-xs, --font-size-sTypeface and text sizes
--size-3xs to --size-xlPadding, gaps and control sizes
--radius-s, --radius-mCorner radius of buttons and of the editor frame
--duration-fastTransition speed

The fallback order for each value is: the --image-editor-* property if you set it, then the Vanilla Breeze token, then a built-in default. Without Vanilla Breeze the built-in defaults apply.

For light and dark mode the editor looks at data-mode on the nearest ancestor, then the inherited color-scheme, then the system setting. Setting mode="dark" on the editor also sets data-mode on it, so Vanilla Breeze works out its dark tokens for that element. The README notes one limit: Vanilla Breeze 0.5.0 can only scope data-mode="dark" regions, so mode="light" inside a dark page stays dark on that version.

Accessibility

KeyAction
Ctrl or Cmd + ZUndo
Ctrl or Cmd + Shift + ZRedo
EscapeCancel the current tool
EnterApply the crop
Arrow keysMove the crop selection
Shift + arrow keysMove the crop selection by 10 pixels
↑