import React, { useState, useRef } from 'react' import ReactCrop, { centerCrop, makeAspectCrop, Crop, PixelCrop } from '..' import { canvasPreview } from './canvasPreview' import { useDebounceEffect } from './useDebounceEffect' import '../ReactCrop.scss' import './index.scss' // This is to demonstate how to make and center a % aspect crop // which is a bit trickier so we use some helper functions. function centerAspectCrop(mediaWidth: number, mediaHeight: number, aspect: number) { return centerCrop( makeAspectCrop( { unit: '%', width: 90, }, aspect, mediaWidth, mediaHeight ), mediaWidth, mediaHeight ) } // const defaultAspect = 9 / 16 const defaultAspect = 16 / 9 export function Demo() { const [imgSrc, setImgSrc] = useState('') const previewCanvasRef = useRef(null) const imgRef = useRef(null) const [crop, setCrop] = useState() const [completedCrop, setCompletedCrop] = useState() const [scale, setScale] = useState(1) const [rotate, setRotate] = useState(0) const [aspect, setAspect] = useState(defaultAspect) function onSelectFile(e: React.ChangeEvent) { if (e.target.files && e.target.files.length > 0) { setCrop(undefined) // Makes crop preview update between images. const reader = new FileReader() reader.addEventListener('load', () => setImgSrc(reader.result?.toString() || '')) reader.readAsDataURL(e.target.files[0]) } } function onImageLoad(e: React.SyntheticEvent) { if (aspect) { const { width, height } = e.currentTarget setCrop(centerAspectCrop(width, height, aspect)) } } useDebounceEffect( async () => { if (completedCrop?.width && completedCrop?.height && imgRef.current && previewCanvasRef.current) { // We use canvasPreview as it's much faster than imgPreview. canvasPreview(imgRef.current, previewCanvasRef.current, completedCrop, scale, rotate) } }, 100, [completedCrop, scale, rotate] ) function handleToggleAspectClick() { if (aspect) { setAspect(undefined) } else { setAspect(defaultAspect) if (imgRef.current) { const { width, height } = imgRef.current setCrop(centerAspectCrop(width, height, defaultAspect)) } } } return (
setScale(Number(e.target.value))} />
setRotate(Math.min(180, Math.max(-180, Number(e.target.value))))} />
{!!imgSrc && ( setCrop(percentCrop)} onComplete={c => setCompletedCrop(c)} aspect={aspect} minWidth={400} minHeight={200} circularCrop ruleOfThirds > Crop me )}
{!!completedCrop && ( )}
) }