1.0.2 • Published 9 months ago

react-avatar-crop v1.0.2

Weekly downloads
23
License
MIT
Repository
github
Last release
9 months ago

react-avatar-crop

example1 A lightweight, customizable, non-intrusive image cropper for using on the frontend. Its primary purpose is to enable end users to crop avatar/ profile images that they uploaded via input. With an intuitive design, it ensures a seamless and user-friendly experience. The package has no external dependencies and has a minimal footprint. It works seamlessly on both desktop and mobile devices, providing a consistent experience across platforms. Cropper requires the consuming project to have React installed.

Installation

npm install react-avatar-crop

Usage

Basic example

import { Cropper } from 'react-avatar-crop';
import 'react-avatar-crop/css';

...
<Cropper
    file={sourceFile}
    onSave={blob => setTargetFile(blob)}
    saveButton={saveBtnRef}
    box={{
        width: '500px',
        height: '400px',
    }}
    wheelControl
    rangeControl
/>

Elaborate example

import { useState, useRef, useEffect } from 'react';
import { Cropper } from 'react-avatar-crop';
import 'react-avatar-crop/css';

const Example = () => {
    const [sourceFile, setSourceFile] = useState(null);
    const [targetFile, setTargetFile] = useState(null);
    const saveBtnRef = useRef(null);
    const targetImgRef = useRef(null);

    useEffect(() => {
        if (targetFile) {
            targetImgRef.current.src = URL.createObjectURL(targetFile);
        }
    }, [targetFile]);

    return (
        <div>
            <h3>Your avatar</h3>
            <input
                type="file"
                accept="image/*"
                name="avatar"
                onChange={evt => setSourceFile(evt.target.files[0])}
            />
            {Boolean(sourceFile) &&
                <>
                    <Cropper
                        file={sourceFile}
                        onSave={blob => setTargetFile(blob)}
                        saveButton={saveBtnRef}
                        box={{
                            width: '500px',
                            height: '400px',
                        }}
                        wheelControl
                        rangeControl 
                    />
                    <button ref={saveBtnRef}>Save</button>
                </>
            }
            <img
                ref={targetImgRef}
                style={{
                    borderRadius: '50%',
                    display: 'block',
                }}
            />
        </div>
    );
};

API

NameTypeRequiredDefaultDescription
fileblobyesInput file selected by user
onSavefuncyesHandles cropped image blob, e.g. uploads to a server
saveButtonrefyesRef to a button, onSave will be attached to it
boxobjectyesCropper element width and height
shapeobject{ borderRadius: '50%' }Crop area width, height and border-radius
layerobject{ dim: '40%', dimTransition: '1.5s' }Layer around crop area
rangeControlbool | object{ color: 'grey' }Range widget
wheelControlbool | object{ sensitivity: 5 }Allows mouse wheel and trackpad zoom
pinchZoomobject{ sensitivity: 3 }Allows touch screen devices pinch zoom
buttonsControlobject{ step: 3 }Refs to zoomInControl, zoomOutControl and step size (1 to 10)
targetImageobject{ type: 'image/png', quality: 1, preserveOriginalScale: false }Config for target image
1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago