akcyp / react-lasso-select

React lasso tool for selecting area on images
https://www.npmjs.com/package/react-lasso-select
ISC License
22 stars 5 forks source link
free-select image lasso reactjs

React-lasso-select

A responsive react tool for marking irregular areas in images (lasso / free select). No dependencies!

React Lasso Select on NPM Minified size

Preview

Demos

Features

Installation

With yarn:

yarn add react-lasso-select

With npm:

npm i react-lasso-select

Usage

Import the main js module:

import ReactLassoSelect from 'react-lasso-select';

Example

import { useState } from 'react';
import ReactLassoSelect, { getCanvas } from 'react-lasso-select';

export default function App () {
  const src = './demo.jpg';
  const [points, setPoints] = useState([]);
  const [clippedImg, setClippedImg] = useState();
  return (
    <div className="App">
      <ReactLassoSelect
        value={points}
        src={src}
        onChange={value => {
          setPoints(value);
        }}
        onComplete={value => {
          if (!value.length) return;
          getCanvas(src, value, (err, canvas) => {
            if (!err) {
              setClippedImg(canvas.toDataURL());
            }
          });
        }}
      />
      <div>
        Points: {points.map(({x, y}) => `${x},${y}`).join(' ')}
      </div>
      <div>
        <img src={clippedImg} alt="" />
      </div>
    </div>
  );
}

Props

Most important props:

Props related to component:

Props related to image:

Difference between onChange and onComplete props in terms of dragging

Tips for better user experience

There are some extra features made to improve user experience.

  1. Press CTRL (Meta Key on Mac) while selecting area to straighten the path from the last point. (Keep the angle of 15 degrees)

  2. Press CTRL + SHIFT keys to maintain parallelism to another sides.

Contributing / Developing

Feel free to post any PR or issues. Be here for information on features, bug fixes, or documentation.

To develop clone this repository and run npm i -D and npm run build, this will create a lib folder with compiled files. I suggest you test your changes before PR with npm link and create-react-app & npm link react-lasso-select in another directory.