fix: image crop broken (#37)

This commit is contained in:
trafficlunar 2026-04-19 16:43:46 +01:00
parent 3d2de94acf
commit a42a4126ec
2 changed files with 115 additions and 114 deletions

View file

@ -1,114 +1,114 @@
import { useCallback, useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
import ReactCrop, { type Crop } from "react-image-crop"; import ReactCrop, { type Crop } from "react-image-crop";
import { Icon } from "@iconify/react"; import { Icon } from "@iconify/react";
interface Props { interface Props {
isOpen: boolean; isOpen: boolean;
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>; setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
image: string | undefined; image: string | undefined;
setImage: (value: string | undefined) => void; setImage: (value: string | undefined) => void;
} }
export default function ImageEditorPortrait({ isOpen, setIsOpen, image, setImage }: Props) { export default function ImageEditorPortrait({ isOpen, setIsOpen, image, setImage }: Props) {
const [isVisible, setIsVisible] = useState(false); const [isVisible, setIsVisible] = useState(false);
const [crop, setCrop] = useState<Crop>(); const [crop, setCrop] = useState<Crop>();
const imageRef = useRef<HTMLImageElement>(null); const imageRef = useRef<HTMLImageElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
const applyCrop = useCallback(() => { const applyCrop = useCallback(() => {
if (!imageRef.current || !canvasRef.current || !crop) return; if (!imageRef.current || !canvasRef.current || !crop) return;
const image = imageRef.current; const image = imageRef.current;
const canvas = canvasRef.current; const canvas = canvasRef.current;
if (!crop.width || !crop.height || image.naturalWidth === 0 || image.naturalHeight === 0) return; if (!crop.width || !crop.height || image.naturalWidth === 0 || image.naturalHeight === 0) return;
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
if (!ctx) return; if (!ctx) return;
const scaleX = image.naturalWidth / image.width; const scaleX = image.naturalWidth / image.width;
const scaleY = image.naturalHeight / image.height; const scaleY = image.naturalHeight / image.height;
canvas.width = crop.width * scaleX; canvas.width = crop.width * scaleX;
canvas.height = crop.height * scaleY; canvas.height = crop.height * scaleY;
ctx.drawImage(image, crop.x * scaleX, crop.y * scaleY, crop.width * scaleX, crop.height * scaleY, 0, 0, crop.width * scaleX, crop.height * scaleY); ctx.drawImage(image, crop.x * scaleX, crop.y * scaleY, crop.width * scaleX, crop.height * scaleY, 0, 0, crop.width * scaleX, crop.height * scaleY);
setImage(canvas.toDataURL()); setImage(canvas.toDataURL());
setCrop(undefined); setCrop(undefined);
}, [crop, setImage]); }, [crop, setImage]);
const rotate = () => { const rotate = () => {
if (!imageRef.current || !canvasRef.current) return; if (!imageRef.current || !canvasRef.current) return;
const image = imageRef.current; const image = imageRef.current;
const canvas = canvasRef.current; const canvas = canvasRef.current;
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
if (!ctx) return; if (!ctx) return;
canvas.width = image.naturalHeight; canvas.width = image.naturalHeight;
canvas.height = image.naturalWidth; canvas.height = image.naturalWidth;
ctx.translate(canvas.width / 2, canvas.height / 2); ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(Math.PI / 2); ctx.rotate(Math.PI / 2);
ctx.drawImage(image, -image.naturalWidth / 2, -image.naturalHeight / 2); ctx.drawImage(image, -image.naturalWidth / 2, -image.naturalHeight / 2);
setImage(canvas.toDataURL()); setImage(canvas.toDataURL());
}; };
const close = () => { const close = () => {
setIsVisible(false); setIsVisible(false);
setTimeout(() => { setTimeout(() => {
setIsOpen(false); setIsOpen(false);
}, 300); }, 300);
}; };
useEffect(() => { useEffect(() => {
if (isOpen) { if (isOpen) {
// slight delay to trigger animation // slight delay to trigger animation
setTimeout(() => setIsVisible(true), 10); setTimeout(() => setIsVisible(true), 10);
} }
}, [isOpen]); }, [isOpen]);
return ( return (
<div className={`fixed inset-0 h-[calc(100%-var(--header-height))] top-(--header-height) flex items-center justify-center z-40 ${!isOpen ? "hidden" : ""}`}> <div className={`fixed inset-0 h-[calc(100%-var(--header-height))] top-(--header-height) flex items-center justify-center z-40 ${!isOpen ? "hidden" : ""}`}>
<div <div
onClick={close} onClick={close}
className={`z-40 absolute inset-0 backdrop-brightness-75 backdrop-blur-xs transition-opacity duration-300 ${isVisible ? "opacity-100" : "opacity-0"}`} className={`z-40 absolute inset-0 backdrop-brightness-75 backdrop-blur-xs transition-opacity duration-300 ${isVisible ? "opacity-100" : "opacity-0"}`}
/> />
<div <div
className={`z-50 bg-orange-50 border-2 border-amber-500 rounded-2xl shadow-lg p-6 w-full max-w-md transition-discrete duration-300 ${ className={`z-50 bg-orange-50 border-2 border-amber-500 rounded-2xl shadow-lg p-6 w-full max-w-md transition-discrete duration-300 ${
isVisible ? "scale-100 opacity-100" : "scale-75 opacity-0" isVisible ? "scale-100 opacity-100" : "scale-75 opacity-0"
}`} }`}
> >
<div className="flex justify-between items-center mb-2"> <div className="flex justify-between items-center mb-2">
<h2 className="text-xl font-bold">Edit Image</h2> <h2 className="text-xl font-bold">Edit Image</h2>
<button type="button" aria-label="Close" onClick={close} className="text-red-400 hover:text-red-500 text-2xl cursor-pointer"> <button type="button" aria-label="Close" onClick={close} className="text-red-400 hover:text-red-500 text-2xl cursor-pointer">
<Icon icon="material-symbols:close-rounded" /> <Icon icon="material-symbols:close-rounded" />
</button> </button>
</div> </div>
<div className="relative w-full flex justify-center"> <div className="relative w-full flex justify-center">
<ReactCrop crop={crop} onChange={(c) => setCrop(c)} className="rounded-2xl border-2 border-amber-500 overflow-hidden max-h-96"> <ReactCrop crop={crop} onChange={(c) => setCrop(c)} className="rounded-2xl border-2 border-amber-500 overflow-hidden max-h-96">
<img ref={imageRef} src={image} /> <img ref={imageRef} src={image} />
</ReactCrop> </ReactCrop>
<canvas ref={canvasRef} className="hidden" /> <canvas ref={canvasRef} className="hidden" />
</div> </div>
<div className="mt-4 flex justify-center gap-2"> <div className="mt-4 flex justify-center gap-2">
<button type="button" onClick={close} className="pill button"> <button type="button" onClick={close} className="pill button">
Done Done
</button> </button>
<button type="button" onClick={applyCrop} className="pill button"> <button type="button" onClick={applyCrop} className="pill button">
Crop Crop
</button> </button>
<button type="button" onClick={rotate} className="pill button"> <button type="button" onClick={rotate} className="pill button">
Rotate Rotate
</button> </button>
</div> </div>
</div> </div>
</div> </div>
); );
} }

View file

@ -2,6 +2,7 @@ import { StrictMode } from "react";
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import { BrowserRouter, Route, Routes } from "react-router"; import { BrowserRouter, Route, Routes } from "react-router";
import "./index.css"; import "./index.css";
import "react-image-crop/dist/ReactCrop.css";
import "@fontsource-variable/lexend/wght.css"; import "@fontsource-variable/lexend/wght.css";
import PrivacyPage from "./pages/privacy.tsx"; import PrivacyPage from "./pages/privacy.tsx";