Socket
Socket
Sign inDemoInstall

react-cropper

Package Overview
Dependencies
Maintainers
2
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-cropper

Cropper as React Component


Version published
Weekly downloads
155K
decreased by-17.56%
Maintainers
2
Weekly downloads
 
Created

What is react-cropper?

The react-cropper package is a React wrapper for Cropper.js, a JavaScript image cropping library. It allows you to easily integrate image cropping functionality into your React applications.

What are react-cropper's main functionalities?

Basic Image Cropping

This code demonstrates how to set up a basic image cropping component using react-cropper. It includes a reference to the Cropper instance and logs the cropped image data URL to the console.

```jsx
import React, { useRef } from 'react';
import Cropper from 'react-cropper';
import 'cropperjs/dist/cropper.css';

const ImageCropper = () => {
  const cropperRef = useRef(null);
  const onCrop = () => {
    const cropper = cropperRef.current.cropper;
    console.log(cropper.getCroppedCanvas().toDataURL());
  };

  return (
    <Cropper
      src="https://via.placeholder.com/800x400"
      style={{ height: 400, width: '100%' }}
      initialAspectRatio={16 / 9}
      guides={false}
      crop={onCrop}
      ref={cropperRef}
    />
  );
};

export default ImageCropper;
```

Custom Aspect Ratio

This example shows how to set a custom aspect ratio for the cropping area. In this case, the aspect ratio is set to 4:3.

```jsx
import React, { useRef } from 'react';
import Cropper from 'react-cropper';
import 'cropperjs/dist/cropper.css';

const CustomAspectRatioCropper = () => {
  const cropperRef = useRef(null);
  const onCrop = () => {
    const cropper = cropperRef.current.cropper;
    console.log(cropper.getCroppedCanvas().toDataURL());
  };

  return (
    <Cropper
      src="https://via.placeholder.com/800x400"
      style={{ height: 400, width: '100%' }}
      aspectRatio={4 / 3}
      guides={false}
      crop={onCrop}
      ref={cropperRef}
    />
  );
};

export default CustomAspectRatioCropper;
```

Cropping with Preview

This example demonstrates how to display a preview of the cropped image. The cropped image data URL is stored in the state and displayed in an <img> element.

```jsx
import React, { useRef, useState } from 'react';
import Cropper from 'react-cropper';
import 'cropperjs/dist/cropper.css';

const CropperWithPreview = () => {
  const cropperRef = useRef(null);
  const [preview, setPreview] = useState('');

  const onCrop = () => {
    const cropper = cropperRef.current.cropper;
    setPreview(cropper.getCroppedCanvas().toDataURL());
  };

  return (
    <div>
      <Cropper
        src="https://via.placeholder.com/800x400"
        style={{ height: 400, width: '100%' }}
        initialAspectRatio={16 / 9}
        guides={false}
        crop={onCrop}
        ref={cropperRef}
      />
      <div>
        <h3>Preview:</h3>
        <img src={preview} alt="Cropped Preview" />
      </div>
    </div>
  );
};

export default CropperWithPreview;
```

Other packages similar to react-cropper

Keywords

FAQs

Package last updated on 14 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc