npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-perspective-cropper

v0.2.2

Published

React component performing crop, border detection, perspective correction and simple image filters over a provided image 📲 📸

Downloads

238

Readme

react-perspective-cropper

React component performing border detection, perspective correction and simple image filters over a provided image 📲 📸

NPM JavaScript Style Guide

Intro

react-perspective-cropper doesn't, yet, do live borders recognition like some famous mobile apps. Though it exports a main <Cropper /> component which given an image it renders a cropper component with an already applied but editable crop area. You must pass an img through the src prop otherwise the component won't be rendered. Using its ref you have:

  • a done async method that you can call and it will return the cropped and filtered image and you have the option to also show a preview of the edited image!
  • a backToCrop method to use only if you requested for a preview in the done.

If you have special needs, please open a issue and we'll discuss it there!

Demo

demo gif

Install

npm i react-perspective-cropper

or

yarn add react-perspective-cropper

Props

export interface CropperProps {
  image: string | File
  onDragStop: () => void
  onChange: () => void
  cropperRef: React.ElementRef
  pointSize: number
  lineWidth: number
  pointBgColor: string
  pointBorder: string // css border property value
  lineColor: string
  maxWidth: number
  maxHeight: number
  openCvPath: string
}

Usage

import React from 'react'
import Cropper from 'react-perspective-cropper'

const App = () => {
  const [cropState, setCropState] = useState()
  const [img, setImg] = useState()
  const [inputKey, setInputKey] = useState(0)
  const cropperRef = useRef()

  const onDragStop = useCallback((s) => setCropState(s), [])
  const onChange = useCallback((s) => setCropState(s), [])

  const doSomething = async () => {
    console.log(cropState)
    try {
      const res = await cropperRef.current.done({ preview: true })
      console.log(res)
    } catch (e) {
      console.log('error', e)
    }
  }

  const onImgSelection = async (e) => {
    if (e.target.files && e.target.files.length > 0) {
      // it can also be a http or base64 string for example
      setImg(e.target.files[0])
    }
  }

  return (
    <Cropper
      ref={cropperRef}
      image={img}
      onChange={onChange}
      onDragStop={onDragStop}
    />
    <input
      type='file'
      key={inputKey}
      onChange={onImgSelection}
      accept='image/*'
    />
    <button onClick={doSomething}>Ho finito</button>
  )
}

OpenCV

This cropper uses OpenCV for border recognition, perspective transformation and b&w thresholding. In order to use it, I've created this other handy wrapper around it: opencv-react If you're already using it or if you're importing OpenCV manually in a different way, this lib got you covered as long as you provide the OpenCV instance in window.cv and the component isn't rendered before OpenCV finished loading. So, be careful.

OpenCV async loading

The openCV library is really big (approx. 1mb for the entire js file). Now, opencv-react uses the document.createElement('script') browser function to inject this script and that's equal to writing <script src="..."></script> in your html page. The parsing of the js file from the browser is done synchronously and we don't want this since it'd block the js thread. The solution, which is already implemented in the opencv-react lib, is to use the keyword async in front of the script tag. This won't block the thread but still you gotta decide when to load it. Basically I can imagine many, if not all, of you will render this component under certain conditions; well you need to know that doing so you'll start fetching and asynchronously parsing the library only when you'll render this cropper component. If you wanna start fetching the lib as soon as your app is opened, then you want to wrap your entire app with the opencv-react provider and then render the component whenever you want. The cropper will surely start faster, straight away.

OpenCV locally or from CDN

You might be asking yourself this question. Well, openCV or not, the answer doesn't differ based on the libs you need in your project. Always fewer js devs import stuff from cdns nowadays (think about npm i). If you notice the example folder the lib is loaded locally. But this lib doesn't decide this on your behalf. Do what you think is best :)

Nice to have

It would be nice to have a react-doc-scan component which uses a webcam featuring live borders recognition.

Inspiration and help

Huge thanks to ngx-document-scanner which served me quite some openCV and canvas code to use.

License

MIT © giacomocerquone