Image Cropper

Choose the part of an image to keep: a box moved and resized over it, or a frame it moves under. The crop is a box in the image's own pixels, with a quarter turn and a flip — two-way, and a file with cropImage().

MediaExperimental

Usage

Usage

import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScButton,  ScFrame,  type ScImageCrop,  ScImageCropper,  ScImageCropperFlip,  ScImageCropperGrid,  ScImageCropperHandle,  ScImageCropperImage,

Anatomy

The viewport, the image and the selection are needed; the rest is yours to add. The handles sit in the viewport beside the selection — each a control of its own — and show in box mode only.

import {  ScImageCropper, ScImageCropperViewport, ScImageCropperImage,  ScImageCropperSelection, ScImageCropperGrid, ScImageCropperHandle,  ScImageCropperZoom, ScImageCropperRotate, ScImageCropperFlip,  ScImageCropperPreview, ScImageCropperPreviewImage, cropImage,} from '@semantic-components/ui';
<div scImageCropper aria-label="Photo" [(crop)]="crop">  <div scImageCropperViewport class="h-80">    <img scImageCropperImage src="…" alt="…" />    <div scImageCropperSelection aria-label="Crop" valueText="…">      <div scImageCropperGrid></div>    </div>    <div scImageCropperHandle="ne" aria-label="Top right corner"></div>    …  </div>  <input scImageCropperZoom aria-label="Zoom" />  <button scImageCropperRotate>…</button>  <button scImageCropperFlip>…</button>  <div scImageCropperPreview>    <img scImageCropperPreviewImage alt="…" />  </div></div>

The crop

[(crop)] is { x, y, width, height, rotation?, flipX?, flipY? }, in the pixels of the image as shown — turned, then mirrored. It is written once the image loads: centred, at four fifths of the largest crop the ratio allows (all the frame in frame mode). Set it to restore a saved one; set it to null to start again. Then hand it to cropImage(src, crop, options) for a file.

Zoom is the crop's size: 1 is the largest box at its ratio, maxZoom (10) the smallest — or less, where minWidth and minHeight come first.

Accessibility

  • The root is a group you name. Until the image loads it is busy (aria-busy) and the selection hidden.
  • The selection and each handle are sliders: the selection is one tab stop, then each handle. Name them, and say where the crop is with valueText ("{width} by {height} pixels, at {x}, {y}"), its numbers written in the locale.
  • ←→↑↓ move the selection, or a handle's edge (an edge takes only the arrows along it): one screen pixel, Shift ten. + and − zoom about the crop's middle; R turns the image a quarter, Shift + R back.
  • Dragging: Shift on a handle keeps the ratio when it is free; Escape puts the crop back where the drag started. In frame mode the wheel zooms at the pointer and two fingers pinch.
  • The zoom is a real slider, read as a percentage. Disabled, the selection and handles leave the tab order and the controls are off.
  • The image is not mirrored right to left: a photo has no direction.

Examples

Aspect ratio

import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import {  ScFrame,  ScImageCropper,  ScImageCropperFlip,  ScImageCropperGrid,  ScImageCropperHandle,  ScImageCropperImage,  ScImageCropperSelection,  ScImageCropperViewport,

Circle

import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScButton,  ScFrame,  type ScImageCrop,  ScImageCropper,  ScImageCropperHandle,  ScImageCropperImage,  ScImageCropperPreview,  ScImageCropperPreviewImage,

Frame

import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScButton,  ScFrame,  type ScImageCrop,  ScImageCropper,  ScImageCropperImage,  ScImageCropperPreview,  ScImageCropperPreviewImage,  ScImageCropperRotate,

Grid

import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScImageCropper,  ScImageCropperGrid,  ScImageCropperImage,  ScImageCropperSelection,  ScImageCropperViewport,  ScImageCropperZoom,} from '@semantic-components/ui';

Controlled

560 × 420 at 520, 140
import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScButton,  ScFrame,  type ScImageCrop,  ScImageCropper,  ScImageCropperHandle,  ScImageCropperImage,  ScImageCropperSelection,  ScImageCropperViewport,

Disabled

import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScImageCropper,  ScImageCropperHandle,  ScImageCropperImage,  ScImageCropperRotate,  ScImageCropperSelection,  ScImageCropperViewport,  ScImageCropperZoom,

RTL

import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScImageCropper,  ScImageCropperGrid,  ScImageCropperHandle,  ScImageCropperImage,  ScImageCropperRotate,  ScImageCropperSelection,

API reference

Each part takes class, merged over the style (cn: a later class wins).

PartSelectorDescription
ScImageCropper[scImageCropper] Holds the crop (two-way) and what moves it; a group you name. data-mode, data-shape, data-loading, data-dragging, data-disabled. exportAs scImageCropper: zoom(), mostZoom(), loading(), dragging(), ready(), setZoom(), rotate(), flip(), focus().
ScImageCropperViewport[scImageCropperViewport] The area the image shows in: give it a height. In frame mode it pans (drag), zooms (wheel, at the pointer) and pinches.
ScImageCropperImageimg[scImageCropperImage] Your image, with its src and alt; placed, turned and mirrored by the crop. From another site it needs CORS to be cropped.
ScImageCropperSelection[scImageCropperSelection] The crop: a slider (its value the x), one tab stop. Name it; valueText with {x}, {y}, {width}, {height}. Dragged to move in box mode; the shade around it.
ScImageCropperGriddiv[scImageCropperGrid] Guide lines in the selection: columns and rows (3: thirds), faint, clearer while dragged; restyled from its class.
ScImageCropperHandle[scImageCropperHandle] Resizes from an edge or corner: n, ne, e, se, s, sw, w, nw. In the viewport, box mode only. A slider (width, or height for n and s): name it; valueText.
ScImageCropperZoominput[scImageCropperZoom] The zoom as the style’s slider: 1 to mostZoom(), read as a percentage in the locale. Name it.
ScImageCropperRotatebutton[scImageCropperRotate] A quarter turn: direction clockwise (default) or counterclockwise. The style’s button (outline, icon); your icon and name.
ScImageCropperFlipbutton[scImageCropperFlip] Mirrors: direction horizontal (default) or vertical. The style’s button (outline, icon); your icon and name.
ScImageCropperPreview[scImageCropperPreview] A live preview: size and shape it (size-12 rounded-full), the crop covers it.
ScImageCropperPreviewImageimg[scImageCropperPreviewImage] The cropper’s image in the preview; write its alt.
InputTypeDefaultDescription
cropScImageCrop | nullnull Two-way. Written when the image loads; set it to restore one, null to start again.
mode'box' | 'frame''box' A box moved and resized over the image, or a frame the image moves under.
aspectRationumber | nullnull Width / height; free when null. A change refits the crop.
shape'rectangle' | 'circle''rectangle' The selection's look; pass it to cropImage too.
minWidth, minHeightnumber1 The least crop, in image pixels.
maxZoomnumber10 How far in: 1 is the largest crop.
disabledbooleanfalse Nothing moves.
InputTypeDefaultDescription
typestring'image/png' image/jpeg, image/webp…: what the browser writes.
qualitynumber— 0 to 1, for JPEG and WebP.
width, heightnumberthe crop's One alone: the other follows at the crop’s ratio.
shape'rectangle' | 'circle''rectangle' A circle: an ellipse, its corners transparent.
namestring'image.png' The file's name; a File source gives its own.