react-easy-crop is a React component designed for cropping images and videos with intuitive drag, zoom, and rotate interactions. It provides precise crop dimensions in both pixels and percentages, supporting various image formats (JPEG, PNG, GIF) via URL or base64 strings, as well as HTML5-supported video formats. The library is currently stable at version 5.5.7 and maintains a regular release cadence, primarily focusing on bug fixes and minor enhancements, as seen in recent patch releases. Key differentiators include its mobile-friendly design and its comprehensive feature set for media manipulation within a React application, offering a simpler alternative to more extensive image editing suites like Pintura while still providing essential cropping functionalities.
npm install react-easy-cropVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic image cropping using a placeholder image, allowing drag and zoom, and then programmatically extracting the cropped portion as a new image blob. It includes a common utility function `getCroppedImage` (not part of the library) to process the raw crop coordinates into a usable image.
Wrap the `<Cropper />` component in a `div` with `style={{ position: 'relative', width: '100%', height: '300px' }}` (adjust dimensions as needed).Verify your bundler (Webpack, Rollup, Vite) is configured to correctly parse and resolve ES Modules. If encountering module resolution errors, consult your bundler's documentation for ESM compatibility settings, especially for packages that provide both CJS and ESM exports.
Implement a separate utility function (like `getCroppedImage` shown in examples) that takes the original image source and `croppedAreaPixels` to draw the cropped section onto an HTML Canvas and export it as a Blob or Data URL.
Before passing an image to the `Cropper`, use a library like `exif-js` or implement a custom solution to detect and correct image orientation by rotating the image on a canvas if necessary. The official examples sometimes include such helper functions for this common scenario.
Ensure the `<Cropper />` component is rendered inside a `div` with `position: 'relative'` and explicit `width` and `height` CSS properties (e.g., `style={{ position: 'relative', width: '100%', height: '300px' }}`).Use `import Cropper from 'react-easy-crop'` for modern ESM-compatible environments. If using CommonJS, use `const Cropper = require('react-easy-crop')`. Review your bundler configuration for ESM compatibility, particularly when upgrading to 5.5.4+.Verify that the `image` prop is a valid, loaded image URL or base64 string. Check the network tab for failed image requests. Ensure any image pre-loading or error handling is in place before passing the image to `Cropper`.
When loading images from external URLs, ensure the image server provides `Access-Control-Allow-Origin` headers, and set the `crossOrigin='anonymous'` attribute on the `Image` element before it loads. If these conditions are not met, the canvas becomes 'tainted' and cannot be exported for security reasons.