import React, { useState, useRef, FC } from "react"; import ReactCrop, { Crop, PixelCrop } from "react-image-crop"; import { Box, Button, Modal, Slider, Typography, useMediaQuery, useTheme } from "@mui/material"; import { canvasPreview } from "./utils/canvasPreview"; import { useDebounceEffect } from "./utils/useDebounceEffect"; import { ResetIcon } from "@icons/ResetIcon"; import "react-image-crop/dist/ReactCrop.css"; interface Iprops { opened: boolean; onClose: React.Dispatch>; } export const CropModal2: FC = ({ opened, onClose }) => { const [imgSrc, setImgSrc] = useState(""); const imgRef = useRef(null); const fileInputRef = useRef(null); const previewCanvasRef = useRef(null); const hiddenAnchorRef = useRef(null); const blobUrlRef = useRef(""); const [crop, setCrop] = useState(); const [completedCrop, setCompletedCrop] = useState(); const [rotate, setRotate] = useState(0); const [imageSize, setImageSize] = useState(580); const [darken, setDarken] = useState(0); const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down(786)); const style = { position: "absolute", top: "50%", left: "50%", transform: "translate(-50%, -50%)", width: isMobile ? "343px" : "620px", bgcolor: "background.paper", boxShadow: 24, padding: "20px", borderRadius: "8px", }; const styleSlider = { width: isMobile ? "350px" : "250px", color: "#7E2AEA", height: "12px", "& .MuiSlider-track": { border: "none", }, "& .MuiSlider-rail": { backgroundColor: "#F2F3F7", border: `1px solid "#9A9AAF"`, }, "& .MuiSlider-thumb": { height: 26, width: 26, border: `6px solid #7E2AEA`, backgroundColor: "white", boxShadow: `0px 0px 0px 3px white, 0px 4px 4px 3px #C3C8DD`, "&:focus, &:hover, &.Mui-active, &.Mui-focusVisible": { boxShadow: `0px 0px 0px 3px white, 0px 4px 4px 3px #C3C8DD`, }, }, }; const rotateImage = () => { const newRotation = (rotate + 90) % 360; setRotate(newRotation); }; const onSelectFile = (event: React.ChangeEvent) => { if (event.target.files && event.target.files.length > 0) { setCrop(undefined); const reader = new FileReader(); reader.addEventListener("load", () => setImgSrc(reader.result?.toString() || "")); reader.readAsDataURL(event.target.files[0]); } }; const onDownloadCropClick = () => { if (!previewCanvasRef.current) { throw new Error("Crop canvas does not exist"); } const canvasCopy = document.createElement("canvas"); const ctx = canvasCopy.getContext("2d"); canvasCopy.width = previewCanvasRef.current.width; canvasCopy.height = previewCanvasRef.current.height; ctx!.filter = `brightness(${100 - darken}%)`; ctx!.drawImage(previewCanvasRef.current, 0, 0); canvasCopy.toBlob((blob) => { if (!blob) { throw new Error("Failed to create blob"); } if (blobUrlRef.current) { URL.revokeObjectURL(blobUrlRef.current); } blobUrlRef.current = URL.createObjectURL(blob); hiddenAnchorRef.current!.href = blobUrlRef.current; hiddenAnchorRef.current!.click(); }); }; useDebounceEffect( async () => { if (completedCrop?.width && completedCrop?.height && imgRef.current && previewCanvasRef.current) { canvasPreview(imgRef.current, previewCanvasRef.current, completedCrop, rotate); } }, 100, [completedCrop, rotate] ); return ( <> {completedCrop && (
)} {!!imgSrc && ( setCrop(percentCrop)} onComplete={(c) => setCompletedCrop(c)} maxWidth={500} maxHeight={320} > Crop me )} {crop?.width ? Math.round(crop.width) + "px" : ""} {crop?.width ? Math.round(crop.width) + "px" : ""} Размер { setImageSize(newValue as number); }} /> Затемнение setDarken(newValue as number)} /> ); };