Spaces:
Paused
Paused
File size: 4,031 Bytes
9bb286f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import React, { useState, useCallback } from 'react';
import Cropper, { Area, Point } from 'react-easy-crop';
import { Check, X, ZoomIn, ZoomOut } from 'lucide-react';
import { motion } from 'motion/react';
interface ImageCropperProps {
image;
aspect;
onCropComplete(croppedImage) => void;
onCancel() => void;
circularCrop?;
}
export const ImageCropper.FC = ({
image,
aspect,
onCropComplete,
onCancel,
circularCrop = false
}) => {
const [crop, setCrop] = useState({ x});
const [zoom, setZoom] = useState(1);
const [croppedAreaPixels, setCroppedAreaPixels] = useState<Area | null>(null);
const onCropChange = (crop) => {
setCrop(crop);
};
const onZoomChange = (zoom) => {
setZoom(zoom);
};
const onCropCompleteInternal = useCallback((_croppedArea) => {
setCroppedAreaPixels(croppedAreaPixels);
}, []);
const createImage = (url)=>
new Promise((resolve, reject) => {
const image = new Image();
image.addEventListener('load', () => resolve(image));
image.addEventListener('error', (error) => reject(error));
image.setAttribute('crossOrigin', 'anonymous');
image.src = url;
});
const getCroppedImg = async (
imageSrc)=> {
const image = await createImage(imageSrc);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
throw new Error('No 2d context');
}
canvas.width = pixelCrop.width;
canvas.height = pixelCrop.height;
ctx.drawImage(
image,
pixelCrop.x,
pixelCrop.y,
pixelCrop.width,
pixelCrop.height,
0,
0,
pixelCrop.width,
pixelCrop.height
);
return new Promise((resolve, reject) => {
canvas.toBlob((blob) => {
if (!blob) {
reject(new Error('Canvas is empty'));
return;
}
resolve(blob);
}, 'image/jpeg');
});
};
const handleDone = async () => {
if (croppedAreaPixels) {
try {
const croppedImage = await getCroppedImg(image, croppedAreaPixels);
onCropComplete(croppedImage);
} catch (e) {
console.error(e);
}
}
};
return (
<motion.div
initial={{ opacity}}
animate={{ opacity}}
exit={{ opacity}}
className="fixed inset-0 z-[100] bg-black flex flex-col"
>
<div className="relative flex-1 bg-neutral-900">
<Cropper
image={image}
crop={crop}
zoom={zoom}
aspect={aspect}
cropShape={circularCrop ? 'round' 'rect'}
showGrid={false}
onCropChange={onCropChange}
onCropComplete={onCropCompleteInternal}
onZoomChange={onZoomChange}
/>
</div>
<div className="glass-thick p-6 space-y-6">
<div className="flex items-center gap-4">
<ZoomOut size={20} className="text-white/40" />
<input
type="range"
value={zoom}
min={1}
max={3}
step={0.1}
aria-labelledby="Zoom"
onChange={(e) => setZoom(Number(e.target.value))}
className="flex-1 h-1.5 bg-white/10 rounded-lg appearance-none cursor-pointer accent-orange-500"
/>
<ZoomIn size={20} className="text-white/40" />
</div>
<div className="flex items-center justify-between gap-4">
<button
onClick={onCancel}
className="flex-1 flex items-center justify-center gap-2 py-3 rounded-2xl bg-white/5 hover-white/10 transition-colors font-bold"
>
<X size={20} />
Cancel
</button>
<button
onClick={handleDone}
className="flex-1 flex items-center justify-center gap-2 py-3 rounded-2xl bg-orange-500 shadow-lg shadow-orange-500/20 active-95 transition-transform font-bold"
>
<Check size={20} />
Apply
</button>
</div>
</div>
</motion.div>
);
};
|