Pro unlocks unlimited templates48:00:00Get Pro →

Image Cropper

Pan, zoom, and crop images in the browser with react-easy-crop. SonoJS wraps the stage and zoom slider; export a Blob via getCroppedImg.

Gallery

Square avatar crop with zoom slider and getCroppedImgUrl preview.

Loading…

Installation

CLI
npx @sonojs/ui@latest add image-cropper
Also needs
npx @sonojs/ui@latest add slider
npm i react-easy-crop

Usage

image-cropper.tsx
import * as React from "react"
import {
  DEMO_IMAGE,
  ImageCropper,
  getCroppedImg,
} from "@/components/image-cropper"

export function Example() {
  const [pixels, setPixels] = React.useState<Area | null>(null)

  return (
    <ImageCropper
      src={DEMO_IMAGE}
      aspect={1}
      onCropComplete={(_, area) => setPixels(area)}
    />
  )
}

Props

PropTypeDefaultDescription
srcstring-Image URL or object URL to crop.
aspectnumber1Crop aspect ratio (width / height).
cropShape"rect" · "round""rect"Rectangle or circular crop mask.
zoomnumber-Controlled zoom level.
defaultZoomnumber1Initial zoom when uncontrolled.
onZoomChange(zoom: number) => void-Zoom change callback.
minZoom / maxZoomnumber1 / 3Zoom bounds for the slider and gestures.
onCropComplete(area, pixels) => void-Fires when a crop gesture ends. Use `pixels` with `getCroppedImg`.
showZoomSliderbooleantrueShow the SonoJS zoom `Slider` under the stage.
showGridbooleantrueRule-of-thirds grid overlay.
disabledbooleanfalseDisable pan, zoom, and the slider.
classNamestring-Merged onto the root wrapper.
stageClassNamestring-Merged onto the crop stage (default `h-72`).

Helpers

PropTypeDefaultDescription
getCroppedImg(src, pixels, options?) => Promise<Blob · null>-Canvas crop to a Blob (PNG by default).
getCroppedImgUrl(src, pixels, options?) => Promise<string · null>-Object URL for previews. Revoke when done.

Related

  • Dropzone, pick files before cropping
  • Dialog, modal crop for avatars
  • Slider, zoom control under the stage