Image Cropper
React image cropper: drag to pan, wheel and pinch zoom, aspect presets, a round avatar mask and 90 degree rotation, returning the crop in source pixels.
Installation
npx shadcn@latest add @minidev/image-cropper
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/image-cropper.json
Usage
import { ImageCropper } from "@/components/ui/image-cropper"
export function Example() {
return (
<ImageCropper />
)
}
Exports ImageCropper getCroppedBlob
API reference
| Prop | Type | Default |
|---|---|---|
| srcImage URL or data URI. Remote images need CORS headers for getCroppedBlob. | string | SAMPLE_SRC |
| alt | string | "Lumen sample landscape" |
| aspectsAspect presets offered, as "w:h". | Aspect[] | ["1:1", "4:3", "16:9"] |
| defaultAspect | Aspect | "4:3" |
| defaultCircleStart with the circular avatar mask on. | boolean | false |
| allowCircleOffer the circular mask toggle. | boolean | true |
| maxZoomLargest zoom, as a multiple of the size that just covers the frame. | number | 4 |
| showPreviewShow the live preview and crop details beside the frame. | boolean | true |
| onCropCalled with the crop in source pixels once an interaction settles. | (rect: CropRect, info: { aspect: Aspect; circle: boolean }) => void | - |
| fileNameFile name used by the export button. | string | "lumen-cover.png" |
| className | string | - |
When to use
Use it before uploading a cover, avatar or product photo. onCrop reports the crop in source pixels; getCroppedBlob(src, rect, opts) renders it to a Blob for upload. For picking a file without framing use ImageUpload, and for a round avatar picker use AvatarUpload. Keyboard: arrows pan (Shift for bigger steps), + and - zoom, R rotates, 0 resets.
Keyboard interactions
| Key | Action |
|---|---|
| ArrowLeftorArrowRightorArrowUporArrowDown | On the crop area, moves the image 12px. Hold Shift for 48px. |
| Plusor= | On the crop area, zooms in. |
| -or_ | On the crop area, zooms out. |
| R | On the crop area, rotates 90 degrees clockwise. |
| 0 | On the crop area, resets position, zoom and rotation. |
| ArrowLeftorArrowRightorHomeorEnd | In the aspect ratio control, moves the selection between presets. |
Accessibility
- The crop area is a focusable
role="group"witharia-roledescription="image cropper"andaria-label="Crop {alt}". Itsaria-describedbypoints at hidden instructions listing the drag, arrow, zoom, R and 0 controls. - The aspect presets are a
role="radiogroup"with one Tab stop. The circle toggle hasaria-pressed, and the rotate and zoom buttons have labels. - Load and export failures render in
role="alert"so they are announced. The preview image is named "Cropped preview of {alt}". - Under reduced motion every spring has
duration: 0, so pans, zooms and the circle mask change instantly.
Frequently asked questions
How do I upload the cropped image?
Keep the rectangle from onCrop, which fires about 160ms after an interaction settles. Then render it: const blob = await getCroppedBlob(src, rect, { type: "image/webp", width: 800 }) and append the blob to FormData. Pass circle: true for a round avatar with transparent corners.
Why does export fail for images on another domain?
Drawing a cross-origin image taints the canvas unless the server sends CORS headers. The cropper sets crossOrigin="anonymous" for remote URLs, so the server must answer with Access-Control-Allow-Origin. For a file the user picked, pass URL.createObjectURL(file), which always works.
How do I lock it to a square avatar crop?
Pass aspects={["1:1"]}, defaultAspect="1:1" and defaultCircle. Turning the circle on forces 1:1 and disables the other aspects. Set allowCircle={false} to hide the toggle.
Are the crop numbers in screen pixels?
No. x, y, width and height are in source image pixels, measured after rotation (0, 90, 180 or 270), so they stay correct whatever size the frame is shown at.
Dependencies
npm
Built from
Examples
Avatar crop
One square preset with the circle mask locked on, the usual setup for a profile photo picker.
Reading the crop
onCrop fires once an interaction settles with the rectangle in source pixels, ready to send to your server or to getCroppedBlob.