image-converter-client/app/routes/CropSelector.tsx

150 lines
5.3 KiB
TypeScript
Raw Normal View History

2024-11-02 22:38:33 +00:00
import DragableBox from "./DragableBox";
2024-11-03 20:56:34 +00:00
import { ChangeEvent, Suspense, useEffect, useRef, useState } from "react";
2024-11-02 22:38:33 +00:00
import { PhAngle } from "./PhAngle";
2024-11-03 20:56:34 +00:00
import { parse } from "postcss";
2024-11-02 22:38:33 +00:00
2024-11-03 17:34:21 +00:00
type Coordinates = {
x: number;
y: number;
}
2024-11-02 22:38:33 +00:00
export default function CropSelector() {
const [selectorHeight, setSelectorHeight] = useState(0);
const [selectorWidth, setSelectorWidth] = useState(0);
const [selectorTop, setSelectorTop] = useState(0);
const [selectorLeft, setSelectorLeft] = useState(0);
2024-11-03 17:34:21 +00:00
const [firstDragable, setFirstDragable] = useState<Coordinates>({
2024-11-02 22:38:33 +00:00
x: 0,
y: 0
})
2024-11-03 17:34:21 +00:00
const [secondDragable, setSecondDragable] = useState<Coordinates>({
2024-11-02 22:38:33 +00:00
x: 0,
y: 0
})
2024-11-03 20:56:34 +00:00
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 });
const [scaleRatio, setScaleRatio] = useState({ width: 1, height: 1 });
2024-11-02 22:38:33 +00:00
const containerRef = useRef<HTMLDivElement>(null)
2024-11-03 20:56:34 +00:00
const prevSize = useRef({ width: 0, height: 0 });
// Helper function to round pixel values
const roundPixel = (value: number) => Math.round(value);
2024-11-02 22:38:33 +00:00
useEffect(() => {
const updateContainerSize = () => {
2024-11-03 20:56:34 +00:00
if (!containerRef.current) return;
const newWidth = roundPixel(containerRef.current.offsetWidth);
const newHeight = roundPixel(containerRef.current.offsetHeight);
// Only calculate ratios if we have previous sizes
if (prevSize.current.width && prevSize.current.height) {
const widthRatio = newWidth / prevSize.current.width;
const heightRatio = newHeight / prevSize.current.height;
setScaleRatio({ width: widthRatio, height: heightRatio });
// Update dragable positions with new ratios and round the results
setFirstDragable(prev => ({
x: roundPixel(prev.x * widthRatio),
y: roundPixel(prev.y * heightRatio)
}));
setSecondDragable(prev => ({
x: roundPixel(prev.x * widthRatio),
y: roundPixel(prev.y * heightRatio)
}));
2024-11-02 22:38:33 +00:00
}
2024-11-03 20:56:34 +00:00
// Update container size and store as previous for next resize
setContainerSize({ width: newWidth, height: newHeight });
prevSize.current = { width: newWidth, height: newHeight };
};
// Initial size setup
2024-11-02 22:41:12 +00:00
updateContainerSize();
2024-11-03 20:56:34 +00:00
// Add resize listener with debounce to prevent too frequent updates
let resizeTimeout: any;
const handleResize = () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(updateContainerSize, 16); // roughly 60fps
};
window.addEventListener('resize', handleResize);
// Cleanup
return () => {
window.removeEventListener('resize', handleResize);
clearTimeout(resizeTimeout);
};
}, []); // Empty dependency array since we're using refs
2024-11-02 22:38:33 +00:00
2024-11-03 17:34:21 +00:00
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
event.preventDefault()
const { value, name, id } = event.target
2024-11-03 20:56:34 +00:00
let valueInt = parseInt(value)
if (name === 'x' && containerSize.width !== null && valueInt > containerSize.width) {
valueInt = containerSize.width
}
if (name === 'y' && containerSize.height !== null && valueInt > containerSize.height) {
valueInt = containerSize.height
}
2024-11-03 17:34:21 +00:00
if (id === 'first') {
setFirstDragable(prev => ({
...prev,
2024-11-03 20:56:34 +00:00
[name]: valueInt,
}))
2024-11-03 17:34:21 +00:00
}
if (id === 'second') {
setSecondDragable(prev => ({
...prev,
2024-11-03 20:56:34 +00:00
[name]: valueInt,
}))
2024-11-03 17:34:21 +00:00
}
}
2024-11-02 22:38:33 +00:00
useEffect(() => {
const width = Math.abs(secondDragable.x - firstDragable.x);
setSelectorWidth(width);
const height = Math.abs(secondDragable.y - firstDragable.y);
setSelectorHeight(height);
setSelectorTop(Math.min(firstDragable.y, secondDragable.y));
setSelectorLeft(Math.min(firstDragable.x, secondDragable.x));
}, [firstDragable, secondDragable]);
return (
2024-11-02 22:48:19 +00:00
<div className="flex flex-col h-screen w-screen justify-center items-center">
2024-11-03 20:56:34 +00:00
<Suspense fallback={<p>Loading... </p>}>
<div ref={containerRef} className="relative w-1/3 h-1/3 border-black border">
<div
className="border border-black absolute"
style={{
top: `${selectorTop}px`,
left: `${selectorLeft}px`,
height: `${selectorHeight}px`,
width: `${selectorWidth}px`,
}}
/>
<DragableBox position={firstDragable} coordSetter={setFirstDragable} >
<PhAngle />
</DragableBox>
<DragableBox position={secondDragable} coordSetter={setSecondDragable} >
<PhAngle />
</DragableBox>
</div>
<input min={0} max={300} id="first" onChange={handleChange} value={firstDragable.x} name="x" type="number" />
<input id="first" onChange={handleChange} value={firstDragable.y} name="y" type="number" />
<input id="second" onChange={handleChange} value={secondDragable.x} name="x" type="number" />
<input id="second" onChange={handleChange} value={secondDragable.y} name="y" type="number" />
<div className="flex flex-col text-left">
<p>{containerSize.width} - {containerSize.height}</p>
<p>firstDragable: x: {firstDragable.x} y: {firstDragable.y}</p>
<p>secondDragable: x: {secondDragable.x} y: {secondDragable.y}</p>
<p>Scale height: {scaleRatio.height}</p>
<p>Scale width: {scaleRatio.width}</p>
</div>
</Suspense>
2024-11-02 22:38:33 +00:00
</div>
);
}