Giter Club home page Giter Club logo

react-image-crop's Introduction

React image crop

A responsive image cropping tool for React.

ReactCrop Demo

Features

  • Responsive
  • Touch enabled
  • Free-form or fixed aspect crops
  • Keyboard support for nudging selection
  • Min/max crop size

Usage

Include the main js module, e.g.:

var ReactCrop = require('react-image-crop');
// or es6:
import ReactCrop from 'react-image-crop';

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

Props

src (required)

<ReactCrop src='path/to/image.jpg' />

You can of course pass a blob path or base64 data.

crop (optional)

All crop values are in percentages, and are relative to the image. All crop params are optional.

var crop = {
	x: 20,
	y: 10,
	width: 30,
	height: 10
}

<ReactCrop src='path/to/image.jpg' crop={crop} />

If you want a fixed aspect you only need to specify a width or a height:

var crop = {
   width: 30,
   aspect: 16/9
}

..Or you can omit both and only specify the aspect.

minWidth (optional)

A minimum crop width.

minHeight (optional)

A minimum crop height.

onChange(crop) (optional)

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.

onComplete(crop) (optional)

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

What about showing the crop on the client?

I wanted to keep this component focused so I didn't provide this. Normally a cropped image will be rendered and cached by a backend. However here are some tips for client-side crop previews:

  • You can fake a crop in pure CSS, but in order to do this you need to know the maximum width & height of the crop preview and then perform the calc again if the container size changes (since this technique is only possible using pixels). It's advantage is that it's instantaneous:

Example: https://gist.github.com/DominicTobias/6aa43d03bc12232ef723

  • The other technique is to map the cropped image to a canvas, and then get the base64 of the canvas via toDataURL and set this as an image source. The advantage is that the preview behaves like a proper image and is responsive. Now this is important:
  1. toDataURL is synchronous and will block the main thread, for large images this could be for as long as a couple of seconds. Always use toDataURL('image/jpeg') otherwise it will default to image/png and the conversion will be significantly slower.

  2. Keep an eye out for toBlob when this lands on more browsers, as it will be both faster and asynchronous.

Example: https://gist.github.com/DominicTobias/b1fb501349893922ec7f

react-image-crop's People

Contributors

sekoyo avatar

Watchers

Bran Sorem avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.