1.0.0 • Published 2 years ago

react-polygon-image-cropper v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

react-polygon-image-cropper

Demo

https://user-images.githubusercontent.com/53790951/176822796-6f5d6611-3a4a-4fed-9e8b-b6b6c9e1fedb.mp4

Installation

npm install react-polygon-image-cropper

Quickstart - Demo setup

import React, { useRef, useState } from "react";
// import the Canvas component
import { Canvas } from "react-polygon-image-cropper";
// *important* import the styles for the component
import "react-polygon-image-cropper/dist/style.css";

function App() {
  const [url, setUrl] = useState("");
  const [source, setSource] = useState<any>("");
  const buttonRef = useRef(null);
  const resetRef = useRef(null);
  const rescaleRef = useRef(null);
  const saveRef = useRef(null);
  const saveCallback = (imageUrl: any) => setUrl(imageUrl);

  const handleImage = (e: React.ChangeEvent<HTMLInputElement>) => {
    const reader = new FileReader();
    reader.onload = function (event) {
      if (event.target) setSource(event.target.result);
    };
    if (e.target.files) reader.readAsDataURL(e.target.files[0]);
  };

  return (
    <div className="App">
      <Canvas
        width={500}
        height={500}
        source={source}
        radius={30}
        color="red"
        cropEvent={{ elementRef: buttonRef, eventType: "click" }}
        rescaleEvent={{ elementRef: rescaleRef, eventType: "click" }}
        resetEvent={{ elementRef: resetRef, eventType: "click" }}
        saveProps={{ saveRef, saveCallback }}
        styles={{
          border: "1px solid red",
          display: "flex",
          alignItems: "center",
        }}
      />
      <input type="file" accept="image/png" onChange={(e) => handleImage(e)} />
      <button ref={buttonRef}>Crop</button>
      <button ref={rescaleRef}>Rescale</button>
      <button ref={resetRef}>Reset</button>
      <button ref={saveRef}>Save</button>
      <img src={url} />
    </div>
  );
}

Documentation

Canvas Props Table

PropTypeRequiredDescription
widthnumbertrueWidth of the image canvas
heightnumbertrueHeight of the image canvas
sourcestringtrueSource string for the image. Can be a URL or local image (e.g. in assets)
radiusnumbertrueRadius of the handles
colorstringtrueFill color of the handles
cropEventEventListenerPropsfalseUsed to attach a HTMLEvent to a React.RefObject in order to trigger a crop function
resetEventEventListenerPropsfalseUsed to attach a HTMLEvent to a React.RefObject in order to trigger a reset function
rescaleEventEventListenerPropsfalseUsed to attach a HTMLEvent to a React.RefObject in order to trigger a rescale function
savePropsSavePropsfalseUsed to attach a HTMLEvent to a React.RefObject in order to trigger a save callback which passes the data url of the final canvas (after rescale).
stylesReact.CSSPropertiesfalseUsed to style the bounding box for the canvases
customCallbackCustomCallbackPropsfalsePasses the React.RefObject for all 3 canvases to be used in custom logic