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-image-crop-inline-styles

v0.1.1

Published

A responsive image cropping tool for React

Downloads

7

Readme

React image crop

A responsive image cropping tool for React.

ReactCrop Demo

Features

  • Touch enabled
  • Free-form or fixed aspect crops
  • Keyboard support for nudging selection
  • Min/max crop size

Usage

Include the main js module, e.g.:

var ReactCrop = require('react-image-crop');
// or es6:
import ReactCrop from 'react-image-crop';

Props

src (required)

<ReactCrop src='path/to/image.jpg' />

You can of course pass a blob path or base64 data.

crop (optional)

All crop values are in percentages, and are relative to the image. All crop params are optional.

var crop = {
	x: 20,
	y: 10,
	width: 30,
	height: 10
}

<ReactCrop src='path/to/image.jpg' crop={crop} />

If you want a fixed aspect you only need to specify a width or a height:

var crop = {
   width: 30,
   aspect: 16/9
}

..Or you can omit both and only specify the aspect.

minWidth (optional)

A minimum crop width.

minHeight (optional)

A minimum crop height.

onChange(crop) (optional)

A callback which happens for every change of the crop (i.e. many times as you are dragging/resizing). Passes the current crop state object.

onComplete(crop) (optional)

A callback which happens after a resize, drag, or nudge. Passes the current crop state object.

What about showing the crop on the client?

I wanted to keep this component focused so I didn't provide this. Normally a cropped image will be rendered and cached by a backend. However here are some tips for client-side crop previews:

  • You can fake a crop in pure CSS, but in order to do this you need to know the maximum width & height of the crop preview and then perform the calc again if the container size changes (since this technique is only possible using pixels). It's advantage is that it's instantaneous:

Example gist

  • The other technique is to map the cropped image to a canvas, and then get the base64 of the canvas via toDataURL and set this as an image source. The advantage is that the preview behaves like a proper image and is responsive. Now this is important:
  1. toDataURL is synchronous and will block the main thread, for large images this could be for as long as a couple of seconds. Always use toDataURL('image/jpeg') otherwise it will default to image/png and the conversion will be significantly slower.

  2. Keep an eye out for toBlob when this lands on more browsers, as it will be both faster and asynchronous.

  3. Another option to make the conversion faster is to scale the image down before converting it to a base64 (see example in gist).

Example gist