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

simple-image-cropper

v1.1.3

Published

![alt text](https://github.com/gopendrajangir/simple-image-cropper/blob/main/simple-image-cropper-customizable%20.png)

Downloads

63

Readme

simple-image-cropper

alt text

simple-image-cropper is a customizable ReactJS library for cropping some specific part of an image by zooming and and selecting specific portion of image.

Installation

npm install simple-image-cropper

Usage

simple-image-cropper is a customizable library so you can apply your own styles on specific parts of this tool. simple-image-cropper uses a higher order component function called withCropper which makes it possible to apply custom design on it.

Imports

import { withCropper, Avatar, ZoomSlider } from 'simple-image-cropper';

Basic Example

const Cropper = withCropper(({ avatarProps, zoomSliderProps, onSave, onCancel }) => {
  return (
    <div>
      <Avatar avatarProps={avatarProps} />
      <ZoomSlider zoomSliderProps={zoomSliderProps} />
      <button type="button" onClick={onCancel}>Cancel</button>
      <button type="button" onClick={onSave}>Save</button>
    </div>
   );
});

class App extends React.Component() {
  constructor(props){
    ...
  }
  onSave(url) {
    ...
  }
  onCancel() {
    ...
  }
  render() {
    returns (
      <Cropper
        width={200}
        height={200}
        url={imageUrl}
        onCancel={this.onCancel}
        onSave={this.onSave}
      />
    )
  }
}

withCropper()

withCropper is a higher order component function which provides all the necessary props required by the Avatar and ZoomSlider component and by the cancel and save button and returns a Cropper Component.

const Cropper = withCropper(({ avatarProps, zoomSliderProps, onSave, onCancel }) => {
  return ...
}); // returns Croppper Component

Note: withCropper returns a Cropper Component which renders our image cropper.


Avatar

Avatar component is responsible for showing image with zooming and dragging features.

<Avatar avatarProps={avatarProps} />

Note: avatarProps is provided by withCropper function's single object argument's property.

<Avatar
  className="avatar"
  style={{ borderRadius: '100%' }}
  avatarProps={avatarProps}
/>

Note: Avatar component also accepts style and className props.


ZoomSlider

ZoomSlider component is responsible for showing range slider for zoom.

<ZoomSlider zoomSliderProps={zoomSliderProps} />

Note: zoomSliderProps is provided by withCropper function's single object argument's property.

<ZoomSlider
  className="zoom-slider"
  style={{ marginTop: '10px' }}
  zoomSliderProps={zoomSliderProps}
/>

Note: ZoomSlider component also accepts style and className props.


Cropper

Cropper component is responsible for render our image cropper. Cropper Component is returned from withCropper function. Cropper component accepts 5 arguments:

| Prop | Type | Required | | -------- | :---------------------: | -------: | | width | number | true | | height | number | true | | url | image url or base64 url | true | | onSave | function | true | | onCancel | function | false |

width : number

width is the width of the Avatar component. Required.

height : number

height is the height of the AvatarComponent. Required.

url: string

url is the online image url or base64 url. Required. Note: If you are using file type input for image you will need to read file using fileReader api to convert it into data url.

onSave: function

onSave prop accepts a function which is passed to a button through withCropper function and is called when this button is clicked. Function passed to this prop is called with a url argument which is the base64 url of cropped part of image. Required.

onCancel: function

onCancel prop accepts a function which is passed to a button through withCropper function and is called when this button is clicked. Not Required.

Additional Help

If you are using file type input for image you will need to convert that file into data url using fileReader api. You can use code below to convert input file to data url.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      imageUrl: null,
    };
    this.onFileChange = this.onFileChange.bind(this);
  }

  onFileChange(e) {
    const file = e.target.files[0];

    const fileReader = new FileReader();

    fileReader.readAsDataURL(file);

    fileReader.onloadend = (e) => {
      this.setState({ imageUrl: e.target.result });
    };
  }

  render() {
    const { imageUrl } = this.state;

    return (
      <div className="app">
        <input type="file" onChange={this.onFileChange} />
        ...
        <Cropper
          width={200}
          height={200}
          url={imageUrl}
          onCancel={this.onCancel}
          onSave={this.onSave}
        />
      </div>
    );
  }
}