Image Cropper
Crop-before-upload control: drag and resize a crop rectangle, lock its aspect ratio, zoom, and export the crop at the image's natural resolution.
<arc-image-cropper>Overview
Image Cropper shows an image letterboxed in a fixed-height stage with a crop rectangle over it. People drag the rectangle to move it and pull its eight handles (four corners, four edges) to resize it. Everything outside the rectangle is darkened, and rule-of-thirds lines sit inside it. A zoom slider under the stage scales the image around its center from 1x to 4x while the rectangle stays put.
aspect (width/height, such as 1 for a square avatar or 16/9) locks the rectangle to that ratio through every drag, resize, zoom and key press. Leave it at 0 for a free-form crop. The rectangle always stays inside the image and never gets smaller than 32px.
Three methods read the result. getCrop() returns { x, y, width, height } in the image's natural pixels, with letterboxing and zoom already accounted for. getCroppedBlob(type, quality) and getCroppedDataUrl(type, quality) draw the crop to an offscreen canvas at natural resolution. Export only works when src is same-origin or served with CORS headers. A cross-origin image taints the canvas, and the methods throw an error that says so. arc-crop-change fires with the same natural-pixel coordinates, at most once per animation frame during a drag.
Guidelines
Do
- Set `aspect="1"` for avatars so the exported crop is always square
- Use an object URL (`URL.createObjectURL(file)`) as `src` when cropping a file just picked in `arc-file-upload`
- Listen for `arc-crop-change` to show a live preview or save the crop coordinates
- Call `getCroppedBlob()` at upload time to send the cropped region at natural resolution
- Serve remote images from the same origin or with CORS headers so export works
Don't
- Don't read crop coordinates from the rectangle on screen. `getCrop()` converts them to natural image pixels
- Don't shrink `height` far below its 320px default. The rectangle and handles need room
- Don't use it to frame an image for display. Use CSS `object-fit` or an aspect-ratio container
Features
- Handles have touch-sized hit areas
- Keyboard: focus the rectangle, then arrows move it 2px and Shift+arrows resize it from the bottom-right corner
- Position and size are announced in a polite live region when a key is released
- A skeleton shimmer shows while the image loads, and an inline error if it fails
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-image-cropper
src="/uploads/avatar-original.jpg"
aspect="1"
height="320"
></arc-image-cropper>
<script>
const cropper = document.querySelector('arc-image-cropper');
cropper.addEventListener('arc-crop-change', (e) => {
console.log('Crop (natural px):', e.detail);
});
// At upload time — natural-resolution export:
async function upload() {
const blob = await cropper.getCroppedBlob('image/jpeg', 0.9);
const body = new FormData();
body.append('avatar', blob, 'avatar.jpg');
await fetch('/api/avatar', { method: 'POST', body });
}
</script>import { useRef } from 'react';
import { ImageCropper } from '@arclux/arc-ui-react';
function AvatarEditor({ src }: { src: string }) {
const ref = useRef<HTMLElementTagNameMap['arc-image-cropper']>(null);
const save = async () => {
const blob = await ref.current!.getCroppedBlob('image/jpeg', 0.9);
// ...upload blob
};
return (
<>
<ImageCropper
ref={ref}
src={src}
aspect={1}
height={320}
onArcCropChange={(e) => console.log(e.detail)}
/>
<button onClick={save}>Save avatar</button>
</>
);
}<script setup>
import { ref } from 'vue';
import { ImageCropper } from '@arclux/arc-ui-vue';
const cropper = ref(null);
async function save() {
const blob = await cropper.value?.getCroppedBlob('image/jpeg', 0.9);
// ...upload blob
}
</script>
<template>
<ImageCropper
ref="cropper"
src="/uploads/avatar-original.jpg"
:aspect="1"
:height="320"
@arc-crop-change="(e) => console.log(e.detail)"
/>
<button @click="save">Save avatar</button>
</template><script>
import { ImageCropper } from '@arclux/arc-ui-svelte';
let cropper;
async function save() {
const blob = await cropper.getCroppedBlob('image/jpeg', 0.9);
// ...upload blob
}
</script>
<ImageCropper
bind:this={cropper}
src="/uploads/avatar-original.jpg"
aspect={1}
height={320}
on:arc-crop-change={(e) => console.log(e.detail)}
/>
<button on:click={save}>Save avatar</button>import { Component, ElementRef, ViewChild } from '@angular/core';
import { ImageCropper } from '@arclux/arc-ui-angular';
@Component({
imports: [ImageCropper],
template: `
<arc-image-cropper
#cropper
src="/uploads/avatar-original.jpg"
[aspect]="1"
[height]="320"
(arcCropChange)="onCropChange($event)"
></arc-image-cropper>
<button (click)="save()">Save avatar</button>
`,
})
export class AvatarEditorComponent {
@ViewChild('cropper', { read: ElementRef }) cropper!: ElementRef;
onCropChange(e: CustomEvent) {
console.log(e.detail);
}
async save() {
const blob = await this.cropper.nativeElement.getCroppedBlob('image/jpeg', 0.9);
// ...upload blob
}
}API
srcstring''- Image URL, object URL, or data URL to crop. Must be same-origin or CORS-enabled for canvas export.
heightnumber320- Fixed stage height in pixels. The image is letterboxed to fit.
aspectnumber0- Crop aspect ratio as width/height (e.g.
1,16/9).0allows free-form cropping. zoomnumber1- Image zoom factor, clamped to 1-4 on the property as well as on the slider. Scales the image around its center; also settable via the built-in slider.
Methods
getCrop(){ x: number, y: number, width: number, height: number } | null- Current crop in natural image pixels, or null before the image has loaded.
getCroppedBlob(type?, quality?)type?: string, quality?: numberPromise<Blob>- Export the crop as a Blob at natural resolution.
getCroppedDataUrl(type?, quality?)type?: string, quality?: numberPromise<string>- Export the crop as a data URL at natural resolution.
Events
arc-crop-change- Fired when the crop changes (drag, resize, keyboard, zoom, stage resize).
event.detailis{ x, y, width, height }in natural image pixels, debounced to animation frames.