shadcn.io is an independent project built on shadcn/ui
Components
Free
Image Cropper for Ark UI
A crop box over a photo that people drag, resize and zoom, with a locked aspect ratio or a circle for avatars. For React projects on Ark UI.
On this page9 sections
An image cropper lays a movable, resizable box over a photo, dims everything outside it, and lets people zoom and pan the picture underneath. Reach for it before an upload, when an avatar, a cover or a product shot has to fit a set shape.
It only marks the region: the file stays as it is until your code exports the crop. For a closer look at a picture with nothing saved, use Image Zoom.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
The frame fills the width it's given and takes the photo's own proportions once the photo loads, so it has no height before that.
Accessibility
The selection is a focusable slider labeled Crop selection area (rectangle), or (circle) for a round crop, and a screen reader hears its position and size as it moves, such as Position X 120px, Y 40px. Size 200px by 150px, with a hint about the keys below. The root is a group named Image cropper whose description reports the zoom and the crop, and the photo is hidden from assistive technology, so its alt is never read. The handles answer the pointer only. With fixedCropArea, the arrow keys pan the photo under the box and Alt with an arrow does nothing. The words are English, and translations replaces them.
KeyAction
Arrow Up
Moves the crop selection upward by the configured nudge step. Hold Shift for the nudgeStepShift value or Ctrl/Cmd for nudgeStepCtrl.
Arrow Down
Moves the crop selection downward by the configured nudge step. Hold Shift for the nudgeStepShift value or Ctrl/Cmd for nudgeStepCtrl.
Arrow Left
Moves the crop selection to the left by the configured nudge step. Hold Shift for the nudgeStepShift value or Ctrl/Cmd for nudgeStepCtrl.
Arrow Right
Moves the crop selection to the right by the configured nudge step. Hold Shift for the nudgeStepShift value or Ctrl/Cmd for nudgeStepCtrl.
Alt+Arrow Up
Resizes the crop vertically from the bottom handle, reducing the height. Hold Shift or Ctrl/Cmd for the larger nudge steps.
Alt+Arrow Down
Resizes the crop vertically from the bottom handle, increasing the height. Hold Shift or Ctrl/Cmd for the larger nudge steps.
Alt+Arrow Left
Resizes the crop horizontally from the right handle, reducing the width. Hold Shift or Ctrl/Cmd for the larger nudge steps.
Alt+Arrow Right
Resizes the crop horizontally from the right handle, increasing the width. Hold Shift or Ctrl/Cmd for the larger nudge steps.
+
Zooms in on the image. The = key performs the same action on keyboards where both symbols share a key.
-
Zooms out of the image. The _ key performs the same action on keyboards where both symbols share a key.
Tab
Moves focus to the crop selection, the cropper's one stop in the tab order.
Demos
Export the crop
The cropper's machine comes from useImageCropper and goes to ImageCropperRootProvider, so the Crop button outside it can call getCroppedImage. Here it asks for a data URL at most 256 pixels wide and shows it in a round preview. Leave output out to get a Blob for an upload instead.
Aspect ratio
aspectRatio set to 16 / 9 keeps the box widescreen through every drag of a handle, and it starts 80 percent as wide as the frame. Lock the ratio when the crop fills a slot of known shape, such as a blog cover or a video poster. Leave it out and people can draw any rectangle.
Circle crop
cropShape set to circle rounds the box and holds it at one to one, whatever aspectRatio says, which is the shape a round avatar needs. The circle is only the mask on screen: the image you export is the square around it, so show it in a round frame such as an Avatar or round its corners when you save it.
Controlled zoom
The zoom level lives in the demo's state, passed in as zoom and updated from the zoom-change callback whenever the wheel, a pinch or the plus and minus keys move it. The two buttons under the photo step it by a quarter, and they stop at the same 1 and 3 that minZoom and maxZoom give the cropper, so the state and the photo never drift apart.
Fixed crop area
fixedCropArea pins the box, hides its handles and turns every drag into a pan of the photo underneath, so people choose what lands in the frame instead of drawing it. Use it when the output shape is decided, such as a profile photo or a banner. The arrow keys pan the photo here too, and the wheel and the plus and minus keys still zoom.
Initial crop
initialCrop places the first box at an x of 100 and a y of 80, 200 by 150 pixels, measured from the top-left corner of the frame on screen rather than in the photo's own pixels. Use it to reopen a crop someone saved earlier. It only lines up again when the frame is the same size as when it was saved.
Min max size
minWidth and minHeight of 80, with maxWidth at 280 and maxHeight at 160, keep the box between a usable square and a size well inside the frame, so on a wide screen it starts at that maximum. Every value is in pixels on screen. Without them the floor is 40 by 40 and there is no ceiling.
Zoom limits
minZoom at 0.5 and maxZoom at 2 let the photo shrink to half the frame and grow to twice its size, in place of the defaults of 1 and 5. Below 1 the frame shows empty space around the photo, which suits a picture that should fit whole inside the crop. Keep the maximum low when a deep zoom would export a crop too small to use.
FAQ
How do I get the cropped image as a file from Image Cropper?
Call getCroppedImage on the cropper's API. It draws the selected region at the photo's own resolution onto a canvas and resolves to a PNG Blob, or to a data URL if you ask for one through output, and it takes type, quality and maxSize for a JPEG or WebP at a capped size.
To reach the API from a button outside the cropper, create the machine with useImageCropper and pass it as value to ImageCropperRootProvider in place of ImageCropper, as the Export the crop demo does. Inside the cropper, useImageCropperContext returns the same API. A photo from another origin needs crossOrigin set to anonymous and CORS headers from its server, or the canvas is blocked and the result is null.
How do I lock the aspect ratio of an Image Cropper?
Set aspectRatio on ImageCropper to width divided by height: 1 for a square, 16 / 9 for a widescreen cover, 4 / 5 for a portrait post. Every drag of a handle then keeps that shape, as the Aspect ratio demo shows, and cropShape set to circle always holds one to one.
Without aspectRatio the box resizes freely. Holding Shift while dragging a handle keeps its current shape, and holding Alt resizes it around its center.
How do I read the crop coordinates from an Image Cropper?
Pass onCropChange, which receives an object whose crop holds x, y, width and height each time the box moves.
Those are pixels of the frame on screen, measured from its top-left corner, and zooming or panning the photo doesn't change them. initialCrop takes the same shape to restore a saved box, as the Initial crop demo does. For the region in the photo's own pixels, which a server needs to crop the original, use getCropData from the cropper's API, which accounts for zoom, pan, rotation and flip.
How do I control Image Cropper zoom in React?
Hold the level in state, pass it as zoom and update it in onZoomChange, which receives an object whose zoom is the new level. Your own buttons then share one number with the wheel, a pinch and the plus and minus keys, as in the Controlled zoom demo.
The cropper keeps zoom between minZoom and maxZoom, 1 and 5 unless you set them, and moves it by zoomStep, 0.1, for each wheel event or key press. Clamp your buttons to the same range: a value past a limit is clamped, and when that leaves the zoom where it was, no change is reported and your state drifts from the photo.
Why does my photo look stretched in the Image Cropper?
A class on ImageCropper that fixes the frame's shape, such as aspect-video or a height, is the usual cause. The photo always fills the frame on both axes, because the cropper sets object-fit to fill inline, so a frame of other proportions stretches it.
Remove that class and the frame takes the photo's own proportions once it loads. If the layout needs a fixed shape, give it the ratio of the files people upload. A stretched frame also bends the export: a square drawn on screen comes out as a rectangle, since each axis maps back to the photo separately.
How do I crop a photo before uploading it with Image Cropper?
Take the chosen file from a File Upload or a file input, turn it into a URL with URL.createObjectURL, and pass that URL as src on ImageCropperImage. A local object URL counts as the page's own origin, so exporting the crop needs no CORS setup.
On submit, export the region with getCroppedImage and append the Blob to a FormData. Call URL.revokeObjectURL when the cropper goes away, so the browser can free the file.
How do I rotate or flip the image in Image Cropper?
Hold the angle in state and pass it as rotation, in degrees, such as 90 after a Rotate button is pressed, and pass flip as an object with horizontal and vertical set to true or false. Neither has controls of its own, so the buttons are yours. Use defaultRotation and defaultFlip instead when the cropper should only start that way.
The export follows both, so getCroppedImage returns the region turned and mirrored as it looks on screen.
How do I set a minimum or maximum crop size in Image Cropper?
Set minWidth, minHeight, maxWidth and maxHeight on ImageCropper. The minimums are 40 unless you set them and there is no maximum, and the first box is clamped into the range, as the Min max size demo shows.
The limits are pixels of the frame on screen, so they don't promise a resolution in the exported file. Check the size from getCropData when that matters. To fix the size entirely, add fixedCropArea: the box stays put and people pan and zoom the photo under it.
API Reference
ImageCropper
<div>data-slot="image-cropper"
PropType
flip?object
The controlled flip state of the image.
Type
{ horizontal: boolean; vertical: boolean }
defaultFlip?object
The initial flip state to apply to the image.
Type
{ horizontal: boolean; vertical: boolean }
Default
{ horizontal: false, vertical: false }
onFlipChange?(details: FlipChangeDetails) → void
Callback fired when the flip state changes.
Type
(details: FlipChangeDetails) → void
Parameters
details – { flip: FlipState }
Returns
void
rotation?number
The controlled rotation of the image in degrees (0 - 360).
defaultRotation?number
The initial rotation to apply to the image in degrees.
The aspect ratio to maintain for the crop area (width / height).
For example, an aspect ratio of 16 / 9 will maintain a width to height ratio of 16:9.
If not provided, the crop area can be freely resized.
cropShape?"rectangle" | "circle"
The shape of the crop area.
Type
"rectangle" | "circle"
Default
"rectangle"
dir?"ltr" | "rtl"
The document's text/writing direction.
Type
"ltr" | "rtl"
Default
"ltr"
fixedCropArea?boolean
Whether the crop area is fixed in size and position.
The aspect ratio to maintain for the crop area (width / height).
For example, an aspect ratio of 16 / 9 will maintain a width to height ratio of 16:9.
If not provided, the crop area can be freely resized.
cropShape?"rectangle" | "circle"
The shape of the crop area.
Type
"rectangle" | "circle"
Default
"rectangle"
fixedCropArea?boolean
Whether the crop area is fixed in size and position.
Function to rotate the image by a relative amount in degrees.
Parameters
degrees – number
Returns
void
rotationnumber
The current rotation of the image in degrees.
setCrop(crop: Rect) → void
Function to set the crop area in viewport coordinates, constrained like initialCrop
(min/max size, aspect ratio and viewport bounds). Zoom, rotation, flip and pan are kept.
Does nothing when fixedCropArea is set or before the viewport is measured.