2024-11-03 21:39:34 +00:00
|
|
|
import { Suspense, useEffect, useRef, useState } from "react";
|
2024-11-02 22:38:33 +00:00
|
|
|
import DragableBox from "./DragableBox";
|
|
|
|
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() {
|
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 21:39:34 +00:00
|
|
|
});
|
2024-11-03 17:34:21 +00:00
|
|
|
const [secondDragable, setSecondDragable] = useState<Coordinates>({
|
2024-11-03 21:39:34 +00:00
|
|
|
x: 100,
|
|
|
|
y: 100
|
|
|
|
});
|
|
|
|
const [selectorPosition, setSelectorPosition] = useState<Coordinates>({
|
2024-11-02 22:38:33 +00:00
|
|
|
x: 0,
|
|
|
|
y: 0
|
2024-11-03 21:39:34 +00:00
|
|
|
});
|
2024-11-02 22:38:33 +00:00
|
|
|
|
2024-11-03 21:39:34 +00:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
2024-11-03 20:56:34 +00:00
|
|
|
|
2024-11-03 21:39:34 +00:00
|
|
|
// Update selector position and dimensions based on handle positions
|
2024-11-02 22:38:33 +00:00
|
|
|
useEffect(() => {
|
2024-11-03 21:39:34 +00:00
|
|
|
setSelectorPosition({
|
|
|
|
x: Math.min(firstDragable.x, secondDragable.x),
|
|
|
|
y: Math.min(firstDragable.y, secondDragable.y)
|
|
|
|
});
|
|
|
|
}, [firstDragable, secondDragable]);
|
2024-11-03 20:56:34 +00:00
|
|
|
|
2024-11-03 21:39:34 +00:00
|
|
|
// Calculate selector dimensions
|
|
|
|
const selectorWidth = Math.abs(secondDragable.x - firstDragable.x);
|
|
|
|
const selectorHeight = Math.abs(secondDragable.y - firstDragable.y);
|
2024-11-03 20:56:34 +00:00
|
|
|
|
2024-11-03 21:39:34 +00:00
|
|
|
// Handle selector drag
|
|
|
|
const handleSelectorDrag = (newPosition: Coordinates) => {
|
|
|
|
const deltaX = newPosition.x - selectorPosition.x;
|
|
|
|
const deltaY = newPosition.y - selectorPosition.y;
|
2024-11-02 22:38:33 +00:00
|
|
|
|
2024-11-03 21:41:10 +00:00
|
|
|
setFirstDragable({
|
2024-11-03 21:39:34 +00:00
|
|
|
x: Math.min(firstDragable.x, secondDragable.x) + deltaX,
|
|
|
|
y: Math.min(firstDragable.y, secondDragable.y) + deltaY
|
2024-11-03 21:41:10 +00:00
|
|
|
});
|
2024-11-03 17:34:21 +00:00
|
|
|
|
2024-11-03 21:41:10 +00:00
|
|
|
setSecondDragable({
|
2024-11-03 21:39:34 +00:00
|
|
|
x: Math.max(firstDragable.x, secondDragable.x) + deltaX,
|
|
|
|
y: Math.max(firstDragable.y, secondDragable.y) + deltaY
|
2024-11-03 21:41:10 +00:00
|
|
|
});
|
2024-11-03 21:39:34 +00:00
|
|
|
};
|
2024-11-02 22:38:33 +00:00
|
|
|
|
|
|
|
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 21:41:10 +00:00
|
|
|
<div ref={containerRef} className="relative w-1/3 h-1/3 border-black border">
|
|
|
|
<DragableBox
|
|
|
|
position={selectorPosition}
|
|
|
|
mode="box"
|
|
|
|
width={selectorWidth}
|
|
|
|
height={selectorHeight}
|
|
|
|
coordSetter={setSelectorPosition}
|
|
|
|
onDrag={handleSelectorDrag}
|
|
|
|
className="border border-black"
|
|
|
|
/>
|
|
|
|
<DragableBox position={firstDragable} coordSetter={setFirstDragable} mode="handle">
|
|
|
|
<PhAngle />
|
|
|
|
</DragableBox>
|
|
|
|
<DragableBox position={secondDragable} coordSetter={setSecondDragable} mode="handle">
|
|
|
|
<PhAngle />
|
|
|
|
</DragableBox>
|
|
|
|
</div>
|
2024-11-02 22:38:33 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|