Image Cropper
NewDrag and resize a crop box, lock an aspect ratio, round for avatars, nudge with the keyboard and export with cropImage().
Installation
$ pnpm dlx shadcn@latest add @synccn/image-cropperFirst time in this project? Connect synccn with one command.
Usage
import { ImageCropper, cropImage } from "@/components/ui/image-cropper";<ImageCropper src={url} aspect={1} round onCropChange={setArea} />API reference
Generated from the component's TypeScript types, so it always matches the code.
ImageCropper
Drag a box over an image to crop it: move, resize from the corners, lock an aspect ratio, round for avatars, and nudge with the arrow keys. Gives you pixel coordinates, plus a cropImage() helper that returns the result as a data URL.
- srcrequired
stringImage URL. For cropping to a canvas it must be same-origin or allow CORS.
- alt
stringDefault
""Text alternative for the image.
- aspect
numberWidth divided by height, e.g. 1 for square avatars or 16 / 9. Free when not set.
- onCropChange
(area: CropArea) => voidCalled as the crop changes, in the image's own pixels.
- round
booleanDefault
falseDraws the crop as a circle (the result is still a square).
- className
stringExtra classes for the root element.
cropImage
Crops an image to a data URL with a canvas.
Takes no props.
Dependencies
Read from the component's imports when the site builds. The CLI installs all of these for you.
Helpers
- lib/utilscn (merges class names)
npm packages
- clsx for cn()
- tailwind-merge for cn()
Keyboard
- ←or→or↑or↓Move the crop box
- Alt+ArrowResize it
- Shift+ArrowMove in bigger steps
Accessibility
- The crop box is a focusable slider that describes its size and position.
From the source
Read from this component's code when the site builds, so it can't drift.
- ARIA roles: slider
- ARIA attributes: aria-label, aria-valuetext
- Keys handled: ArrowUp, ArrowDown, ArrowLeft, ArrowRight
- Shows a visible focus ring for keyboard users