Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@krainboltgreene/react-image-crop
Advanced tools
A responsive image cropping tool for React.
npm i react-image-crop --save
Include the main js module, e.g.:
var ReactCrop = require('react-image-crop');
// or es6:
import ReactCrop from 'react-image-crop';
Include either dist/ReactCrop.css
or ReactCrop.scss
.
<ReactCrop src="path/to/image.jpg" />
You can of course pass a blob path or base64 data.
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.
Please note that the values will be adjusted if the cropping area is outside of the image boundaries.
A minimum crop width, as a percentage of the image width.
A minimum crop height, as a percentage of the image height.
A maximum crop width, as a percentage of the image width.
A maximum crop height, as a percentage of the image height.
If true is passed then selection can't be disabled if the user clicks outside the selection area.
If true then the user cannot modify or draw a new crop. A class of ReactCrop--disabled
is also added to the container for user styling.
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, as well as a pixel-converted crop for your convenience. This callback is not called on the load even if the crop was adjusted.
Note that when setting state in a callback you must also ensure that you set the new crop state, otherwise your component will re-render with whatever crop state was initially set.
A callback which happens after a resize, drag, or nudge. Passes the current crop state object, as well as a pixel-converted crop for your convenience.
Note that when setting state in a callback you must also ensure that you set the new crop state, otherwise your component will re-render with whatever crop state was initially set.
A callback which happens when the image is loaded. Passes the current crop state object and the image DOM element, as well as a pixel-converted crop for your convenience. If the crop was adjusted during the load, this callback gives you the adjusted crop.
Note that when setting state in a callback you must also ensure that you set the new crop state, otherwise your component will re-render with whatever crop state was initially set.
A callback which happens when the new aspect ratio is passed to the component. Passes the current crop state object, as well as a pixel-converted crop for your convenience.
Note that when setting state in a callback you must also ensure that you set the new crop state, otherwise your component will re-render with whatever crop state was initially set.
A callback which happens when a user starts dragging or resizing. It is convenient to manipulate elements outside this component.
A callback which happens when a user releases the cursor or touch after dragging or resizing.
Allows setting the crossorigin attribute used for the img tags.
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:
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.
Keep an eye out for toBlob when this lands on more browsers, as it will be both faster and asynchronous.
Another option to make the conversion faster is to scale the image down before converting it to a base64 (see example in gist).
To develop run npm start
, this will recompile your JS and SCSS on changes.
You can test your changes by opening demo/index.html
in a browser (you don't need to be running a server).
When you are happy with your changes you can build to dist with npm run release
.
FAQs
A responsive image cropping tool for React
We found that @krainboltgreene/react-image-crop demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.