Skip to content

ImageCropper ​

Move / resize the crop frame, drag the image to reframe it, and zoom or rotate inside the stage, then confirm to get the result. Use it standalone (avatar / cover cropping) or as an upload pre-step: pick → crop → upload.

Basic usage (free ratio) ​

Drag the crop frame to move it and its corners to resize; drag anywhere outside the frame to pan the image; the toolbar zooms, rotates and resets. Confirming dispatches oas-crop.

Free ratio (drag + zoom + rotate)

Adjust, then click “Crop”

Panning and framing ​

The image can be panned: press and drag on the stage outside the crop frame (dragging inside the frame still moves the frame), or use Alt + arrow keys (Shift steps 10px). Panning always honours the image covers the crop frame — going past the allowed range is clamped back, so the stage never shows its background.

Scrolling the wheel over the stage zooms anchored at the cursor (the content under the cursor stays put). Wheel handling is tiered: trackpad two-finger scrolls (pixel deltas) zoom continuously — one 25% step per 100px of travel, clamped to 50% per event, so small movements no longer jump a whole step; a classic mouse-wheel notch is ~100px (deltaY=100 platforms land exactly one 25% step, deltaY=120 platforms ≈30% per notch, both within the per-event clamp); Firefox line-mode wheels keep the discrete 25% step too. Ctrl / ⌘ + wheel is the browser page-zoom gesture and is left alone. The zoom slider and + / − buttons zoom around the stage centre (25% per click).

Drag to pan + wheel zoom (16:9)

Drag outside the frame to pan, wheel to zoom

Fixed ratio ​

aspect-ratio locks the crop frame ratio: the frame adapts proportionally to the container, and corner dragging keeps the ratio.

aspect-ratio=1 (square avatar)

1:1

aspect-ratio=16/9 (cover)

Output options ​

max-zoom caps the zoom factor (shared by the slider and the + / − buttons), max-output-size caps the long edge of the exported image, and output-type picks the format (image/png / image/jpeg / image/webp). In the instances below the left one stops at 2×, while the right one exports JPEG with a 640px long-edge cap.

max-zoom=2 · max-output-size=640 · output-type=image/jpeg

Click “Crop” to see the export type and size

Load failure ​

When the image behind src cannot be loaded, oas-error is dispatched and the stage shows an error message instead of a broken image.

Unavailable src (error state)

Waiting for load…

Working with the upload chain ​

js
const cropper = document.querySelector('oas-image-cropper')
cropper.addEventListener('oas-crop', async (e) => {
  if (!e.detail.blob) return
  const file = new File([e.detail.blob], 'avatar.png', { type: 'image/png' })
  const form = new FormData()
  form.append('avatar', file)
  await fetch('/api/avatar', { method: 'POST', body: form })
})

Or call the methods directly: await cropper.toBlob('image/jpeg', 0.9) / cropper.toDataURL().

Events and keyboard ​

The crop frame is focusable: arrow keys move it by 1px, Shift + arrow keys by 10px; Alt + arrow keys pan the image (Shift steps 10px). The zoom slider and toolbar buttons are native controls.

oas-change / oas-crop

Waiting for interaction…

EventWhendetail
oas-changeAfter zoom / rotate / crop-frame adjustment / image pan{ zoom, rotation, frame, offset }
oas-crop“Crop” is clicked{ blob, zoom, rotation, frame, offset } (blob is null when canvas is unavailable)
oas-loadImage loaded{ width, height }
oas-errorImage failed to load{ src }

Disabled ​

disabled

API ​

oas-image-cropper ​

Attributes ​

AttributeDescriptionTypeDefault
aspect-ratioCrop frame ratio: 1 / 16/9 / 0.75 and so on; absent or 0 = free ratiostring—
disabledDisables all interaction (toolbar disabled, crop frame not focusable)boolean—
max-output-sizeMax output long-edge pixels (default 1024); invalid values fall backstring—
max-zoomMax zoom factor (default 3); invalid values fall backstring—
output-typeOutput image type, default image/png (image/jpeg / image/webp also valid)stringimage/png
srcImage source (http(s) / blob: / data:image/* / relative; executable protocols resolve to empty)string—

Property (JS property; see each row for attribute reflection) ​

PropertyDescriptionTypeDefault
offsetCurrent image offset { x, y } (pixels from the stage centre, read-only; already clamped by the “image covers the crop frame” constraint)CropOffset—
rotationCurrent rotation in degrees (normalized 0–360, read-only; any angle can be set via rotate())number0
zoomCurrent zoom (starts at 1). Writes apply immediately but dispatch no event — oas-change only reports user interaction, so echoing el.zoom back from the handler cannot recursenumber1

Methods ​

MethodSignatureDescription
setZoomsetZoom(value: number): voidSet zoom (clamped to [1, max-zoom], dispatches oas-change only when the value actually changes)
rotaterotate(delta: number): voidRotate by a delta (default +90°; accepts any angle, dispatches oas-change)
resetreset(): voidRestore initial zoom / rotation / image offset / crop frame (dispatches oas-change)
toBlobtoBlob(type?: string, quality?: number): Promise<Blob | null>Export the crop as a Blob (null when canvas is unavailable)
toDataURLtoDataURL(type?: string, quality?: number): stringExport the crop as a dataURL (empty string when canvas is unavailable)

Events ​

EventDescription
oas-changeAfter zoom / rotate / crop-frame adjustment / image pan, detail: { zoom, rotation, frame, offset }
oas-cropCrop confirmed, detail: { blob, zoom, rotation, frame, offset }; blob is null when canvas is unavailable
oas-errorImage failed to load, detail: { src }
oas-loadImage loaded, detail: { width, height }

Released under the MIT OR Apache-2.0 License.