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

106 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-11-02 22:38:33 +00:00
import DragableBox from "./DragableBox";
2024-11-03 17:34:21 +00:00
import { ChangeEvent, useEffect, useRef, useState } from "react";
2024-11-02 22:38:33 +00:00
import { PhAngle } from "./PhAngle";
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
})
const [containerWidth, setContainerWidth] = useState<number | null>(null)
const [containerHeight, setContainerHeight] = useState<number | null>(null)
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
})
const containerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const updateContainerSize = () => {
if (containerRef.current) {
setContainerWidth(containerRef.current.offsetWidth);
setContainerHeight(containerRef.current.offsetHeight);
}
}
2024-11-02 22:41:12 +00:00
updateContainerSize();
2024-11-02 22:38:33 +00:00
window.addEventListener('resize', updateContainerSize);
return () => window.removeEventListener('resize', updateContainerSize);
2024-11-02 22:41:12 +00:00
}, [])
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
if (id === 'first') {
setFirstDragable(prev => ({
...prev,
[name]: value,
}
)
)
}
if (id === 'second') {
setSecondDragable(prev => ({
...prev,
[name]: value,
}
)
)
}
}
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-02 22:38:33 +00:00
<div ref={containerRef} className="relative w-1/3 h-1/3 border-black border">
{containerHeight !== null && containerWidth !== null && (
<>
<div
className="border border-black absolute"
style={{
top: `${selectorTop}px`,
left: `${selectorLeft}px`,
height: `${selectorHeight}px`,
width: `${selectorWidth}px`,
}}
/>
2024-11-03 17:34:21 +00:00
<DragableBox position={firstDragable} coordSetter={setFirstDragable} >
2024-11-02 22:38:33 +00:00
<PhAngle />
</DragableBox>
2024-11-03 17:34:21 +00:00
<DragableBox position={secondDragable} coordSetter={setSecondDragable} >
2024-11-02 22:38:33 +00:00
<PhAngle />
</DragableBox>
</>
)}
</div>
2024-11-03 17:34:21 +00:00
<input 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" />
2024-11-02 22:48:19 +00:00
<div className="flex flex-col text-left">
2024-11-02 22:38:33 +00:00
<p>{containerWidth} - {containerHeight}</p>
2024-11-02 22:48:19 +00:00
<p>firstDragable: x: {firstDragable.x.toFixed(2)} y: {firstDragable.y.toFixed(2)}</p>
<p>secondDragable: x: {secondDragable.x.toFixed(2)} y: {secondDragable.y.toFixed(2)}</p>
2024-11-02 22:38:33 +00:00
</div>
</div>
);
}