All Projects → thegreatercurve → react-simple-crop

thegreatercurve / react-simple-crop

Licence: MIT license
✂️ A React component library for cropping and previewing images

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to react-simple-crop

Cropiwa
📐 Configurable Custom Crop widget for Android
Stars: ✭ 2,185 (+11400%)
Mutual labels:  crop, cropper, crop-image
Croppy
Image Cropping Library for Android
Stars: ✭ 906 (+4668.42%)
Mutual labels:  crop, cropper, crop-image
pikaso
Seamless and headless HTML5 Canvas library
Stars: ✭ 23 (+21.05%)
Mutual labels:  crop, cropper, crop-image
Tocropviewcontroller
A view controller for iOS that allows users to crop portions of UIImage objects
Stars: ✭ 4,210 (+22057.89%)
Mutual labels:  crop, cropper, crop-image
react-drop-n-crop
An opinionated implementation of react-dropzone and react-cropper
Stars: ✭ 17 (-10.53%)
Mutual labels:  crop, cropper, crop-image
Krop
Small widget for image cropping in Instagram-like style
Stars: ✭ 107 (+463.16%)
Mutual labels:  crop, cropper, crop-image
React Cropper
Cropperjs as React component
Stars: ✭ 1,600 (+8321.05%)
Mutual labels:  crop, cropper, crop-image
Dragscalecircleview
a custom view that provides dragged and scaled
Stars: ✭ 513 (+2600%)
Mutual labels:  crop, crop-image
Flyimg
Dockerized PHP7 application runs as a Microservice to resize and crop images on the fly. Get optimised images with MozJPEG, WebP or PNG using ImageMagick. Includes face detection, cropping, face blurring, image rotation and many other options. Abstract storage based on FlySystem in order to store images on any provider (local, AWS S3...).
Stars: ✭ 762 (+3910.53%)
Mutual labels:  crop, crop-image
Pdfcropmargins
pdfCropMargins -- a program to crop the margins of PDF files
Stars: ✭ 141 (+642.11%)
Mutual labels:  crop, cropper
Smartcircle
✂️Automatically determine where to crop a circular image out of a rectangular.
Stars: ✭ 29 (+52.63%)
Mutual labels:  crop, crop-image
React Easy Crop
A React component to crop images/videos with easy interactions
Stars: ✭ 1,093 (+5652.63%)
Mutual labels:  crop, crop-image
Igrphototweaks
Drag, Rotate, Scale and Crop
Stars: ✭ 212 (+1015.79%)
Mutual labels:  crop, crop-image
Tkimageview
An easy way to crop an image.
Stars: ✭ 342 (+1700%)
Mutual labels:  crop, crop-image
Croppr.js
A vanilla JavaScript image cropper that's lightweight, awesome, and has absolutely zero dependencies.
Stars: ✭ 294 (+1447.37%)
Mutual labels:  crop, cropper
crops
🌄 Image thumbnail generation server
Stars: ✭ 37 (+94.74%)
Mutual labels:  crop, crop-image
Bimg
Go package for fast high-level image processing powered by libvips C library
Stars: ✭ 1,394 (+7236.84%)
Mutual labels:  crop, crop-image
react-native-avatar-crop
Highly customisable <Crop /> component for React Native < 💅 >
Stars: ✭ 47 (+147.37%)
Mutual labels:  crop, cropper
Svg Autocrop
🚗🌽🔳An NPM module to autocrop and slim down SVGs
Stars: ✭ 80 (+321.05%)
Mutual labels:  crop, crop-image
Rskimagecropper
An image cropper / photo cropper for iOS like in the Contacts app with support for landscape orientation.
Stars: ✭ 2,371 (+12378.95%)
Mutual labels:  crop, cropper

React Simple Crop

A simple, easy-to-use and dependecy-free library for cropping images in React.

Features

  • Responsive and touch enabled
  • Crop supports mimimum and maximum sizes
  • Crop can conform to an aspect ratio
  • Crop can be refined with arrow keys
  • Dependency-free (only 8kb minified)
  • Compatible with >IE11
  • Typescript support
  • Accessible

Installation

npm i react-simple-crop

Demo

A simple example can be found here on CodeSandbox.

There is also a full suite of Storybook stories, which can be run locally:

npm run storybook

Basic Usage

import React from "react";
import { Crop, Preview } from "react-simple-crop";

export const App = () => {
  const imageRef = React.createRef();

  const [value, setValue] = React.useState({
    x: 0,
    y: 0,
    width: 0,
    height: 0
  });

  return (
    <>
      <Crop
        onChange={crop => setValue({ ...value, ...crop })}
        ref={imageRef}
        src={"..."}
        value={value}
      />
      {filePreview && <Preview ref={imageRef} value={value} />}
    </>
  );
};

Components

<Crop />

Displays an image with a crop area overlay.

This crop area can be drawn, moved or resized, using mouse click, touch, or keyboard arrow events.

The onChange callback will fire any time the coordinates or size of the crop area change.

You must then update the local parent component state with the new value to see these changes reflected in the DOM.

How to use

import { Crop } from "react-simple-crop";

/* ... */

const [value, setValue] = React.useState({
  x: 0,
  y: 0,
  width: 0,
  height: 0
});

<Crop
  onChange={crop => setValue({ ...value, ...crop })}
  src="..."
  value={value}
/>;

Props

Prop Required Type Description
onChange ({ x: number; y: number; width: number; height: number; }) => void Callback which is fired any time the coordinates or size of the crop area change.
src string Source attribute which is passed to the image element.
value { x: number; y: number; width: number; height: number; } Percentage coordinates and size of the crop area. You can also use this prop to initialize the size of the crop on the first render.
aspectRatio [number, number] Restricts the ability to draw or resize a crop area to specific dimensions.
alt string Alt attribute which is passed to the image element.
className string Class name attribute passed to the containing element of the image and crop area.
crossOrigin `"anonymous" "use-credentials"`
maxHeight number Restricts the height of the crop area to a maximum percentage of the image element.
maxWidth number Restricts the width of the crop area to a maximum percentage of the image element.
minHeight number Restricts the height of the crop area to a minimum percentage of the image element.
minWidth number Restricts the width of the crop area to a minimum percentage of the image element.
onComplete () => void Callback which is fired any time the crop area finishes being drawn, moved by mouse or keyboard, or resized.
onStart () => void Callback which is fired any time the crop area starts being drawn, moved by mouse or keyboard, or resized.
ref { current: HTMLImageElement } React ref object which is passed to the image element, and also needs to be passed to the <Preview /> component. This is used to draw the preview image.

<Preview />

Displays a new preview image of the currently selected crop area. This image can then be saved as its own file.

How to use

import { Crop, Preview } from "react-simple-crop";

/* ... */

const imageRef = React.createRef();

const [value, setValue] = React.useState({
  x: 0,
  y: 0,
  width: 0,
  height: 0
});

<Crop
  onChange={crop => setValue({ ...value, ...crop })}
  ref={imageRef}
  src="..."
  value={value}
/>
<Preview
  ref={imageRef}
  value={value}
/>

Props

Prop Required Type Description
ref { current: HTMLImageElement } React ref object of the <Crop /> component image element. This is used to draw the preview image.
value { x: number; y: number; width: number; height: number; } Percentage coordinates and size of the crop area.
fileType string File type of the preview image (i.e. image/jpeg, image/png, or image/gif).

Also accepts any HTML image attributes as props (i.e. alt, crossOrigin or style), apart from src, which is used internally by this component.

Testing

There are no unit tests. Instead, we use end-to-end tests.

We require an actual DOM, versus an emulated DOM like JSDOM, which is often used with unit testing libraries, to be able to easily compute image sizes and positions.

To run the Cypress E2E tests locally, first ensure the Storybook server is running:

npm run storybook

Then, in another terminal:

npm run cypress:open
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].