To use the cropper, wrap the Cropper component in a container element that has position: relative and a stable size (width and height). The Cropper component uses position: absolute to fill its parent container.
Key props used in the basic implementation:
image: The source image (URL or base64).crop: The current crop state (object with x and y).zoom: The current zoom level.aspect: The aspect ratio of the crop area.onCropChange: Callback function to update the crop state.onCropComplete: Callback function called when cropping is finished, providing croppedArea and croppedAreaPixels.onZoomChange: Callback function to update the zoom state.
import { useState } from 'react'
import Cropper from 'react-easy-crop'
export default function Demo({ image }) {
const [crop, setCrop] = useState({ x: 0, y: 0 })
const [zoom, setZoom] = useState(1)
function onCropComplete(croppedArea, croppedAreaPixels) {
console.log(croppedArea, croppedAreaPixels)
}
return (
<div style={{ position: 'relative', width: 400, height: 300 }}>
<Cropper
image={image}
crop={crop}
zoom={zoom}
aspect={4 / 3}
onCropChange={setCrop}
onCropComplete={onCropComplete}
onZoomChange={setZoom}
/>
</div>
)
}