react-image-crop vs cropperjs vs vue-cropperjs
Image Cropping Libraries
react-image-cropcropperjsvue-cropperjsSimilar Packages:

Image Cropping Libraries

Image cropping libraries are essential tools in web development that allow developers to implement image manipulation features, specifically cropping, within their applications. These libraries provide a user-friendly interface for selecting and adjusting image areas, enhancing user experience and functionality in applications that require image uploads or modifications. They often come with various features such as aspect ratio control, zooming, and rotation, making them versatile for different use cases across web applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-image-crop1,045,9854,087112 kB72a year agoISC
cropperjs1,043,44013,813440 kB425 months agoMIT
vue-cropperjs0968-375 years agoMIT

Feature Comparison: react-image-crop vs cropperjs vs vue-cropperjs

Framework Compatibility

  • react-image-crop:

    React Image Crop is specifically designed for React applications. It utilizes React's component lifecycle and state management, providing a more intuitive experience for React developers. This tight integration allows for easier handling of image state and properties.

  • cropperjs:

    CropperJS is a framework-agnostic library, meaning it can be used with any JavaScript framework or even vanilla JavaScript. This makes it highly versatile for developers who want to implement cropping features without being tied to a specific framework.

  • vue-cropperjs:

    Vue CropperJS is built for Vue.js applications, leveraging Vue's reactivity and component system. This ensures that developers can easily bind data and manage state within their Vue components, making it a natural choice for Vue developers.

Customization Options

  • react-image-crop:

    React Image Crop provides a straightforward API for customization, allowing developers to set aspect ratios, crop dimensions, and styles directly through props. This makes it easy to create a tailored cropping interface that fits the application's design.

  • cropperjs:

    CropperJS offers extensive customization options, allowing developers to modify the cropping area, aspect ratios, and various UI elements. It provides a wide range of configuration settings to tailor the cropping experience to specific needs, including zooming, rotating, and cropping guides.

  • vue-cropperjs:

    Vue CropperJS allows for customization through props and slots, enabling developers to adjust the cropping interface and behavior according to their application's requirements. It supports various options for aspect ratios and cropping dimensions.

User Experience

  • react-image-crop:

    React Image Crop focuses on providing a smooth user experience within React applications. It allows users to easily interact with the cropping area, providing visual feedback as they adjust the crop, which is essential for a seamless user experience.

  • cropperjs:

    CropperJS is designed with user experience in mind, offering a responsive and intuitive interface for users to crop images easily. It includes features like drag-and-drop functionality, touch support, and keyboard shortcuts, enhancing the overall usability of the cropping tool.

  • vue-cropperjs:

    Vue CropperJS emphasizes user experience by integrating well with Vue's reactivity. It provides a responsive and interactive cropping interface, allowing users to make adjustments in real-time, which is crucial for applications that rely on image uploads.

Performance

  • react-image-crop:

    React Image Crop is designed to be lightweight and efficient, leveraging React's rendering optimizations. It minimizes unnecessary re-renders, ensuring that cropping operations are fast and responsive, even with complex images.

  • cropperjs:

    CropperJS is optimized for performance, ensuring that image processing is efficient even with large images. It minimizes reflows and repaints during cropping operations, which is critical for maintaining a smooth user experience.

  • vue-cropperjs:

    Vue CropperJS is built to be performant within Vue applications, utilizing Vue's reactivity system to only update the DOM when necessary. This ensures that cropping operations are efficient and do not hinder the overall performance of the application.

Community and Support

  • react-image-crop:

    React Image Crop benefits from a strong community of React developers, with ample resources, tutorials, and documentation available. This community support can be invaluable for troubleshooting and enhancing the cropping experience.

  • cropperjs:

    CropperJS has a robust community and extensive documentation, making it easy for developers to find support and resources. The library is actively maintained, ensuring that it stays up-to-date with the latest web standards and practices.

  • vue-cropperjs:

    Vue CropperJS has a growing community within the Vue ecosystem. While it may not be as extensive as some other libraries, it offers solid documentation and support, making it a reliable choice for Vue developers.

How to Choose: react-image-crop vs cropperjs vs vue-cropperjs

  • react-image-crop:

    Select React Image Crop if you are building a React application and want a library that seamlessly integrates with React's component-based architecture. It provides a simple API and is designed specifically for React, making it easier to manage state and props.

  • cropperjs:

    Choose CropperJS if you need a standalone, highly customizable image cropping solution that can be integrated into any web project regardless of the framework. It offers a rich set of features and flexibility for developers looking for a pure JavaScript solution.

  • vue-cropperjs:

    Opt for Vue CropperJS if you are developing a Vue.js application and require a cropping solution that leverages Vue's reactive data binding. This package is tailored for Vue, ensuring smooth integration and a familiar development experience.

README for react-image-crop

React Image Crop

An image cropping tool for React with no dependencies.

React Image Crop on NPM

CodeSandbox Demo

ReactCrop GIF

Table of Contents

  1. Features
  2. Installation
  3. Usage
  4. Example
  5. CDN
  6. Props
  7. FAQ
    1. How can I generate a crop preview in the browser?
    2. How to correct image EXIF orientation/rotation?
    3. How to filter, rotate and annotate?
    4. How can I center the crop?
  8. Contributing / Developing

Features

  • Responsive (you can use pixels or percentages).
  • Touch enabled.
  • Free-form or fixed aspect crops.
  • Fully keyboard accessible (a11y).
  • No dependencies/small footprint (<5KB gzip).
  • Min/max crop size.
  • Crop anything, not just images.

If React Crop doesn't cover your requirements then take a look at Pintura (our sponsor). It features cropping, rotating, filtering, annotation, and lots more.

Learn more about Pintura here

Installation

npm i react-image-crop --save
yarn add react-image-crop
pnpm add react-image-crop

This library works with all modern browsers. It does not work with IE.

Usage

Include the main js module:

import ReactCrop from 'react-image-crop'

Include either dist/ReactCrop.css or ReactCrop.scss.

import 'react-image-crop/dist/ReactCrop.css'
// or scss:
import 'react-image-crop/src/ReactCrop.scss'

Example

import ReactCrop, { type Crop } from 'react-image-crop'

function CropDemo({ src }) {
  const [crop, setCrop] = useState<Crop>()
  return (
    <ReactCrop crop={crop} onChange={c => setCrop(c)}>
      <img src={src} />
    </ReactCrop>
  )
}

See the sandbox demo for a more complete example.

CDN

<link href="https://unpkg.com/react-image-crop/dist/ReactCrop.css" rel="stylesheet" />
<script src="https://unpkg.com/react-image-crop/dist/index.umd.cjs"></script>

Note when importing the script globally using a <script> tag access the component with ReactCrop.Component.

Props

onChange: (crop: PixelCrop, percentCrop: PercentCrop) => void

A callback which happens for every change of the crop (i.e. many times as you are dragging/resizing). Passes the current crop state object.

Note you must implement this callback and update your crop state, otherwise nothing will change!

<ReactCrop crop={crop} onChange={(crop, percentCrop) => setCrop(crop)} />

crop and percentCrop are interchangeable. crop uses pixels and percentCrop uses percentages to position and size itself. Percent crops are resistant to image/media resizing.

crop?: Crop

Starting with no crop:

const [crop, setCrop] = useState<Crop>()

<ReactCrop crop={crop} onChange={c => setCrop(c)}>
  <img src={src} />
</ReactCrop>

Starting with a preselected crop:

const [crop, setCrop] = useState<Crop>({
  unit: '%', // Can be 'px' or '%'
  x: 25,
  y: 25,
  width: 50,
  height: 50
})

<ReactCrop crop={crop} onChange={c => setCrop(c)}>
  <img src={src} />
</ReactCrop>

⚠️ You must ensure the crop is in bounds and correct to the aspect ratio if manually setting. Aspect ratios can be tricky when using %. You can make use of centerCrop and makeAspectCrop helpers. See How can I center the crop? or the CodeSanbox Demo for examples.

aspect?: number

The aspect ratio of the crop, e.g. 1 for a square or 16 / 9 for landscape. Omit/pass undefined for a free-form crop.

minWidth?: number

A minimum crop width, in pixels.

minHeight?: number

A minimum crop height, in pixels.

maxWidth?: number

A maximum crop width, in pixels.

maxHeight?: number

A maximum crop height, in pixels.

keepSelection?: boolean

If true is passed then selection can't be disabled if the user clicks outside the selection area.

disabled?: boolean

If true then the user cannot resize or draw a new crop. A class of ReactCrop--disabled is also added to the container for user styling.

locked?: boolean

If true then the user cannot create or resize a crop, but can still drag the existing crop around. A class of ReactCrop--locked is also added to the container for user styling.

className?: string

A string of classes to add to the main ReactCrop element.

style?: React.CSSProperties

Inline styles object to be passed to the image wrapper element.

onComplete?: (crop: PixelCrop, percentCrop: PercentCrop) => void

A callback which happens after a resize, drag, or nudge. Passes the current crop state object.

percentCrop is the crop as a percentage. A typical use case for it would be to save it so that the user's crop can be restored regardless of the size of the image (for example saving it on desktop, and then using it on a mobile where the image is smaller).

onDragStart?: (e: PointerEvent) => void

A callback which happens when a user starts dragging or resizing. It is convenient to manipulate elements outside this component.

onDragEnd?: (e: PointerEvent) => void

A callback which happens when a user releases the cursor or touch after dragging or resizing.

renderSelectionAddon?: (state: ReactCropState) => React.ReactNode

Render a custom element inside the crop selection.

ruleOfThirds?: boolean

Show rule of thirds lines in the cropped area. Defaults to false.

circularCrop?: boolean

Show the crop area as a circle. If your aspect is not 1 (a square) then the circle will be warped into an oval shape. Defaults to false.

FAQ

How can I generate a crop preview in the browser?

This isn't part of the library but there is an example over here CodeSandbox Demo.

How to correct image EXIF orientation/rotation?

You might find that some images are rotated incorrectly. Unfortunately this is a browser wide issue not related to this library. You need to fix your image before passing it in.

You can use the following library to load images, which will correct the rotation for you: https://github.com/blueimp/JavaScript-Load-Image/

You can read an issue on this subject here: https://github.com/dominictobias/react-image-crop/issues/181

If you're looking for a complete out of the box image editor which already handles EXIF rotation then consider using Pintura.

How to filter, rotate and annotate?

This library is deliberately lightweight and minimal for you to build features on top of. If you wish to perform more advanced image editing out of the box then consider using Pintura.

Pintura Demo

How can I center the crop?

The easiest way is to use the percentage unit:

crop: {
  unit: '%',
  width: 50,
  height: 50,
  x: 25,
  y: 25
}

Centering an aspect ratio crop is trickier especially when dealing with %. However two helper functions are provided:

  1. Listen to the load event of your media to get its size:
<ReactCrop crop={crop} aspect={16 / 9}>
  <img src={src} onLoad={onImageLoad} />
</ReactCrop>
  1. Use makeAspectCrop to create your desired aspect and then centerCrop to center it:
function onImageLoad(e) {
  const { naturalWidth: width, naturalHeight: height } = e.currentTarget

  const crop = centerCrop(
    makeAspectCrop(
      {
        // You don't need to pass a complete crop into
        // makeAspectCrop or centerCrop.
        unit: '%',
        width: 90,
      },
      16 / 9,
      width,
      height
    ),
    width,
    height
  )

  setCrop(crop)
}

Also remember to set your crop using the percentCrop on changes:

const onCropChange = (crop, percentCrop) => setCrop(percentCrop)

And your aspect prop should be set to the same value: <ReactCrop aspect={16 / 9} ... />.

Contributing / Developing

To develop run pnpm install && pnpm dev and open the localhost server in your browser. Update code and it will reload. When you're ready, open a pull request.