Gathering detailed insights and metrics for react-image-crop
Gathering detailed insights and metrics for react-image-crop
Gathering detailed insights and metrics for react-image-crop
Gathering detailed insights and metrics for react-image-crop
npm install react-image-crop
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,894 Stars
708 Commits
344 Forks
27 Watching
3 Branches
65 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (86.66%)
SCSS (12.03%)
JavaScript (0.71%)
HTML (0.59%)
Cumulative downloads
Total Downloads
Last day
-2.6%
86,843
Compared to previous day
Last week
2%
447,957
Compared to previous week
Last month
13.8%
1,872,920
Compared to previous month
Last year
25%
20,379,930
Compared to previous year
An image cropping tool for React with no dependencies.
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.
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.
Include the main js module:
1import ReactCrop from 'react-image-crop'
Include either dist/ReactCrop.css
or ReactCrop.scss
.
1import 'react-image-crop/dist/ReactCrop.css' 2// or scss: 3import 'react-image-crop/src/ReactCrop.scss'
1import ReactCrop, { type Crop } from 'react-image-crop' 2 3function CropDemo({ src }) { 4 const [crop, setCrop] = useState<Crop>() 5 return ( 6 <ReactCrop crop={crop} onChange={c => setCrop(c)}> 7 <img src={src} /> 8 </ReactCrop> 9 ) 10}
See the sandbox demo for a more complete example.
1<link href="https://unpkg.com/react-image-crop/dist/ReactCrop.css" rel="stylesheet" /> 2<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
.
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!
1<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:
1const [crop, setCrop] = useState<Crop>() 2 3<ReactCrop crop={crop} onChange={c => setCrop(c)}> 4 <img src={src} /> 5</ReactCrop>
Starting with a preselected crop:
1const [crop, setCrop] = useState<Crop>({ 2 unit: '%', // Can be 'px' or '%' 3 x: 25, 4 y: 25, 5 width: 50, 6 height: 50 7}) 8 9<ReactCrop crop={crop} onChange={c => setCrop(c)}> 10 <img src={src} /> 11</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
.
This isn't part of the library but there is an example over here CodeSandbox Demo.
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/sekoyo/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.
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.
The easiest way is to use the percentage unit:
1crop: { 2 unit: '%', 3 width: 50, 4 height: 50, 5 x: 25, 6 y: 25 7}
Centering an aspect ratio crop is trickier especially when dealing with %
. However two helper functions are provided:
1<ReactCrop crop={crop} aspect={16 / 9}> 2 <img src={src} onLoad={onImageLoad} /> 3</ReactCrop>
makeAspectCrop
to create your desired aspect and then centerCrop
to center it:1function onImageLoad(e) {
2 const { naturalWidth: width, naturalHeight: height } = e.currentTarget
3
4 const crop = centerCrop(
5 makeAspectCrop(
6 {
7 // You don't need to pass a complete crop into
8 // makeAspectCrop or centerCrop.
9 unit: '%',
10 width: 90,
11 },
12 16 / 9,
13 width,
14 height
15 ),
16 width,
17 height
18 )
19
20 setCrop(crop)
21}
Also remember to set your crop using the percentCrop on changes:
1const onCropChange = (crop, percentCrop) => setCrop(percentCrop)
And your aspect
prop should be set to the same value: <ReactCrop aspect={16 / 9} ... />
.
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.
No vulnerabilities found.
No security vulnerabilities found.