Skip to content
synccn

Image Cropper

New

Drag and resize a crop box, lock an aspect ratio, round for avatars, nudge with the keyboard and export with cropImage().

Mountains at sunset
…

Installation

$ pnpm dlx shadcn@latest add @synccn/image-cropper

First 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
    string

    Image URL. For cropping to a canvas it must be same-origin or allow CORS.

  • alt
    string

    Default ""

    Text alternative for the image.

  • aspect
    number

    Width divided by height, e.g. 1 for square avatars or 16 / 9. Free when not set.

  • onCropChange
    (area: CropArea) => void

    Called as the crop changes, in the image's own pixels.

  • round
    boolean

    Default false

    Draws the crop as a circle (the result is still a square).

  • className
    string

    Extra 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

npm packages

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