5.0.6 • Published 23 days ago

react-easy-crop v5.0.6

Weekly downloads
74,801
License
MIT
Repository
github
Last release
23 days ago

react-easy-crop

A React component to crop images with easy interactions

version Monthly downloads gzip size All Contributors Build Status MIT License PRs Welcome

react-easy-crop Demo

Demo

Check out the examples:

Features

  • Supports drag and zoom interactions
  • Provides crop dimensions as pixels and percentages
  • Supports any images format (JPEG, PNG, even GIF) as url or base 64 string
  • Mobile friendly

Installation

yarn add react-easy-crop

or

npm install react-easy-crop --save

Basic usage

The Cropper is styled with position: absolute to take the full space of its parent. Thus, you need to wrap it with an element that uses position: relative or the Cropper will fill the whole page.

import Cropper from 'react-easy-crop'

class App extends React.Component {
  state = {
    image: 'your-image-url or as base64',
    crop: { x: 0, y: 0 },
    zoom: 1,
    aspect: 4 / 3,
  }

  onCropChange = crop => {
    this.setState({ crop })
  }

  onCropComplete = (croppedArea, croppedAreaPixels) => {
    console.log(croppedArea, croppedAreaPixels)
  }

  onZoomChange = zoom => {
    this.setState({ zoom })
  }

  render() {
    return (
      <Cropper
        image={this.state.image}
        crop={this.state.crop}
        zoom={this.state.zoom}
        aspect={this.state.aspect}
        onCropChange={this.onCropChange}
        onCropComplete={this.onCropComplete}
        onZoomChange={this.onZoomChange}
      />
    )
  }
}

Props

PropTypeRequiredDescription
imagestringThe image to be cropped.
crop{ x: number, y: number }Position of the image. { x: 0, y: 0 } will center the image under the cropper.
zoomnumberZoom of the image between minZoom and maxZoom. Defaults to 1.
aspectnumberAspect of the cropper. The value is the ratio between its width and its height. The default value is 4/3
minZoomnumberMinimum zoom of the image. Defaults to 1.
maxZoomnumberMaximum zoom of the image. Defaults to 3.
cropShape'rect' | 'round'Shape of the crop area. Defaults to 'rect'.
cropSize{ width: number, height: number }Size of the crop area (in pixels). If you don't provide it, it will be computed automatically using the aspect prop and the image size. Warning: this cannot be changed once the component is displayed. If you need to change it (based on some user inputs for instance), you can force the component to be reset by using a key.
showGridbooleanWhether to show or not the grid (third-lines). Defaults to true.
zoomSpeednumberMultiplies the value by which the zoom changes. Defaults to 1.
crossOriginstringAllows setting the crossOrigin attribute on the image.
onCropChangeFunctionCalled everytime the crop is changed. Use it to update your crop state.
onZoomChangeFunctionCalled everytime the zoom is changed. Use it to update your zoom state.
onCropCompleteFunctionCalled when the user stops moving the image or stops zooming. It will be passed the corresponding cropped area on the image in percentages and pixels
onImgErrorFunctionCalled when error occurs while loading an external image
style{ containerStyle: object, imageStyle: object, cropAreaStyle: object }Custom styles to be used with the Cropper. Styles passed via the style prop are merged with the defaults.
classes{ containerClassName: string, imageClassName: string, cropAreaClassName: string }Custom class names to be used with the Cropper. Classes passed via the classes prop are merged with the defaults.
restrictPositionbooleanWhether the position of the image should be restricted to the boundaries of the cropper. Useful setting in case of zoom < 1 or if the cropper should preserve all image content while forcing a specific aspect ratio for image throughout the application. Example: https://codesandbox.io/s/1rmqky233q.

onCropComplete(croppedArea, cropperAreaPixels)

This callback is the one you should use to save the cropped area of the image. It's passed 2 arguments:

  1. croppedArea: coordinates and dimensions of the cropped area in percentage of the image dimension
  2. cropperAreaPixels: coordinates and dimensions of the cropped area in pixels.

Both arguments have the following shape:

const area = {
  x: number, // x/y are the coordinates of the top/left corner of the cropped area
  y: number,
  width: number, // width of the cropped area
  height: number, // height of the cropped area
}

Development

yarn
yarn start

Now, open http://localhost:3001/index.html and start hacking!

License

MIT

Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

@h1-card/h1-lib-frontend@astvision/antd-img-crop-ssrritual-frontend-componentsa-antd-img-cropant-img-crop-cw@busy-bee/ui-wwwgrossjs@jibadano/components@jibadano/imageintraactive-sdk-ui@everything-registry/sub-chunk-2552caseark-uixtreme-uiyoviefp-img-cropyxkj-uiws-react-sdksceyt-chat-react-uikit-npmtest-2-sceyt-chat-react-uikit@wiplant/lib-front@wordpress/block-editor@vibexyz/copy-to-clipboard@vibexyz/crop-dialog@vibexyz/uikit@vibexyz/vibe-checkbox@wowon/components@types/react-easy-crop@ticmais/easycrop@socialcam/ui-elements@shashank8317/webapp-project@solyd/componentseditordrafteditorman@snaphunt/snaphunt-uidw-mx-template@sortd-io/antd-img-crop@sentrei/ui@shaker-hamdi/test-deployment@sfccplus/super-pd-core@yx-tools/cui42-antd-img-crop@yunbaoyuan/bill-templatesadarchitecture-ui@apg.gg/coreaiz-img-crop@apaas-lego/react-form-widgets@app-garage/image-uploader@zclab/react-image-itemtag@zclab/react-item-taggerablo-client-dashboardablo-demo-fe@alfred-is/alfred-design-systemacudexsportia-components@ahmadevnts/design-system@youknown/react-uiant-img-crop--dsms-react-uilimi-antd-img-cropdemio-ui-kitmui-image-cropmui-select-and-crop-image@eightfold.ai/octuplelinkmore-designbluedot-componentscalendar-ui@delosaqua/scylla@epeejs/antd-img-cropmeetneat-shell@bhtandev/antd-img-cropassui@audentio/stuffantd-pro-components-lowcode-materialsantd-easy-crop@avsync.live/formationantd-img-cropantd-img-crop-advantd-img-crop-cwantd-img-crop-fixantd-img-crop-forkantd-img-crop-ignore-gifantd-img-crop-turing-forkantd-img-cropperantd-img-edit@eaprules/easy-notification-2@eaprules/gallery-crop@eaprules/gallery-crop-testleafs-intake-web-templensung-antd-img-cropdingtalk-design-desktop@busy-bee/ui-components@cocalc/antd-img-cropchoerodon-uicommon-frontend-componentsbright-image-crop@cs_social_ank/ui-elementshuemix-antd-img-crop@careteen/antd-img-cropbase-components-antd@choiceform/ui-react@arco-materials/upload-crop
5.0.6

1 month ago

5.0.5

3 months ago

5.0.4

4 months ago

5.0.3

5 months ago

5.0.2

8 months ago

5.0.1

8 months ago

5.0.0

10 months ago

4.7.5

10 months ago

4.7.0

1 year ago

4.7.2

1 year ago

4.7.1

1 year ago

4.7.4

1 year ago

4.7.3

1 year ago

4.6.1

2 years ago

4.6.0

2 years ago

4.6.3

1 year ago

4.6.2

1 year ago

4.5.1

2 years ago

4.4.1

2 years ago

4.4.0

2 years ago

4.4.2

2 years ago

4.5.0

2 years ago

4.3.0

2 years ago

4.2.0

2 years ago

4.1.5

2 years ago

4.1.4

2 years ago

4.0.3

2 years ago

4.1.3

2 years ago

4.1.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.0.1

2 years ago

4.0.2

2 years ago

4.0.0-alpha.0

2 years ago

4.0.0

2 years ago

3.5.3

3 years ago

3.5.2

3 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.4.0

3 years ago

3.3.3

3 years ago

3.3.2

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

2.0.0-beta.2

4 years ago

2.0.0-beta.1

4 years ago

2.0.0-beta.0

4 years ago

1.17.1

4 years ago

2.0.0-alpha.1

4 years ago

2.0.0-alpha.0

4 years ago

1.17.0

4 years ago

1.16.2

4 years ago

1.16.1

5 years ago

1.16.0

5 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.1

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.12.0-alpha.1

5 years ago

1.12.0-alpha.0

5 years ago

1.11.1

5 years ago

1.11.1-alpha.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.10.0-alpha.0

5 years ago

1.9.2

5 years ago

1.9.2-alpha.0

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.9.0-alpha.0

5 years ago

1.8.3

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago