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:39:34 +00:00
|
|
|
setFirstDragable(prev => ({
|
|
|
|
x: Math.min(firstDragable.x, secondDragable.x) + deltaX,
|
|
|
|
y: Math.min(firstDragable.y, secondDragable.y) + deltaY
|
|
|
|
}));
|
2024-11-03 17:34:21 +00:00
|
|
|
|
2024-11-03 21:39:34 +00:00
|
|
|
setSecondDragable(prev => ({
|
|
|
|
x: Math.max(firstDragable.x, secondDragable.x) + deltaX,
|
|
|
|
y: Math.max(firstDragable.y, secondDragable.y) + deltaY
|
|
|
|
}));
|
|
|
|
};
|
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:39:34 +00:00
|
|
|
<Suspense fallback={<p>Loading...</p>}>
|
2024-11-03 20:56:34 +00:00
|
|
|
<div ref={containerRef} className="relative w-1/3 h-1/3 border-black border">
|
2024-11-03 21:39:34 +00:00
|
|
|
<DragableBox
|
|
|
|
position={selectorPosition}
|
|
|
|
mode="box"
|
|
|
|
width={selectorWidth}
|
|
|
|
height={selectorHeight}
|
|
|
|
coordSetter={setSelectorPosition}
|
|
|
|
onDrag={handleSelectorDrag}
|
|
|
|
className="border border-black"
|
2024-11-03 20:56:34 +00:00
|
|
|
/>
|
2024-11-03 21:39:34 +00:00
|
|
|
<DragableBox position={firstDragable} coordSetter={setFirstDragable} mode="handle">
|
2024-11-03 20:56:34 +00:00
|
|
|
<PhAngle />
|
|
|
|
</DragableBox>
|
2024-11-03 21:39:34 +00:00
|
|
|
<DragableBox position={secondDragable} coordSetter={setSecondDragable} mode="handle">
|
2024-11-03 20:56:34 +00:00
|
|
|
<PhAngle />
|
|
|
|
</DragableBox>
|
|
|
|
</div>
|
|
|
|
</Suspense>
|
2024-11-02 22:38:33 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|