state now belongs to CropSelector
All checks were successful
Deploy to Cloudflare Pages / deploy (push) Successful in 31s
All checks were successful
Deploy to Cloudflare Pages / deploy (push) Successful in 31s
This commit is contained in:
parent
7a669aa05e
commit
1ac04b1bf3
@ -1,19 +1,24 @@
|
|||||||
import DragableBox from "./DragableBox";
|
import DragableBox from "./DragableBox";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { ChangeEvent, useEffect, useRef, useState } from "react";
|
||||||
import { PhAngle } from "./PhAngle";
|
import { PhAngle } from "./PhAngle";
|
||||||
|
|
||||||
|
type Coordinates = {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
}
|
||||||
|
|
||||||
export default function CropSelector() {
|
export default function CropSelector() {
|
||||||
const [selectorHeight, setSelectorHeight] = useState(0);
|
const [selectorHeight, setSelectorHeight] = useState(0);
|
||||||
const [selectorWidth, setSelectorWidth] = useState(0);
|
const [selectorWidth, setSelectorWidth] = useState(0);
|
||||||
const [selectorTop, setSelectorTop] = useState(0);
|
const [selectorTop, setSelectorTop] = useState(0);
|
||||||
const [selectorLeft, setSelectorLeft] = useState(0);
|
const [selectorLeft, setSelectorLeft] = useState(0);
|
||||||
const [firstDragable, setFirstDragable] = useState({
|
const [firstDragable, setFirstDragable] = useState<Coordinates>({
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0
|
y: 0
|
||||||
})
|
})
|
||||||
const [containerWidth, setContainerWidth] = useState<number | null>(null)
|
const [containerWidth, setContainerWidth] = useState<number | null>(null)
|
||||||
const [containerHeight, setContainerHeight] = useState<number | null>(null)
|
const [containerHeight, setContainerHeight] = useState<number | null>(null)
|
||||||
const [secondDragable, setSecondDragable] = useState({
|
const [secondDragable, setSecondDragable] = useState<Coordinates>({
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0
|
y: 0
|
||||||
})
|
})
|
||||||
@ -31,6 +36,28 @@ export default function CropSelector() {
|
|||||||
return () => window.removeEventListener('resize', updateContainerSize);
|
return () => window.removeEventListener('resize', updateContainerSize);
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const width = Math.abs(secondDragable.x - firstDragable.x);
|
const width = Math.abs(secondDragable.x - firstDragable.x);
|
||||||
@ -55,15 +82,19 @@ export default function CropSelector() {
|
|||||||
width: `${selectorWidth}px`,
|
width: `${selectorWidth}px`,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<DragableBox initialX={0} initialY={0} setter={setFirstDragable}>
|
<DragableBox position={firstDragable} coordSetter={setFirstDragable} >
|
||||||
<PhAngle />
|
<PhAngle />
|
||||||
</DragableBox>
|
</DragableBox>
|
||||||
<DragableBox initialX={containerWidth} initialY={containerHeight} setter={setSecondDragable} >
|
<DragableBox position={secondDragable} coordSetter={setSecondDragable} >
|
||||||
<PhAngle />
|
<PhAngle />
|
||||||
</DragableBox>
|
</DragableBox>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
<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" />
|
||||||
<div className="flex flex-col text-left">
|
<div className="flex flex-col text-left">
|
||||||
<p>{containerWidth} - {containerHeight}</p>
|
<p>{containerWidth} - {containerHeight}</p>
|
||||||
<p>firstDragable: x: {firstDragable.x.toFixed(2)} y: {firstDragable.y.toFixed(2)}</p>
|
<p>firstDragable: x: {firstDragable.x.toFixed(2)} y: {firstDragable.y.toFixed(2)}</p>
|
||||||
|
@ -6,24 +6,20 @@ type Coordinates = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface DraggableBoxProps {
|
interface DraggableBoxProps {
|
||||||
initialX: number;
|
position: Coordinates;
|
||||||
initialY: number;
|
|
||||||
className?: string;
|
className?: string;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
setter?: Dispatch<SetStateAction<Coordinates>>;
|
coordSetter: Dispatch<SetStateAction<Coordinates>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DraggableBox = ({
|
const DraggableBox = ({
|
||||||
initialX,
|
position,
|
||||||
initialY,
|
|
||||||
className = "",
|
className = "",
|
||||||
children = "",
|
children = "",
|
||||||
setter
|
coordSetter
|
||||||
}: DraggableBoxProps) => {
|
}: DraggableBoxProps) => {
|
||||||
const [isDragging, setIsDragging] = useState(false);
|
const [isDragging, setIsDragging] = useState(false);
|
||||||
const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 });
|
const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 });
|
||||||
const [xPosition, setXPosition] = useState(initialX)
|
|
||||||
const [yPosition, setYPosition] = useState(initialY)
|
|
||||||
const dragRef = useRef<HTMLDivElement>(null);
|
const dragRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -44,14 +40,10 @@ const DraggableBox = ({
|
|||||||
let newX = (Math.max(0, Math.min(relativeX, maxX)));
|
let newX = (Math.max(0, Math.min(relativeX, maxX)));
|
||||||
let newY = (Math.max(0, Math.min(relativeY, maxY)));
|
let newY = (Math.max(0, Math.min(relativeY, maxY)));
|
||||||
|
|
||||||
setXPosition(newX);
|
coordSetter({
|
||||||
setYPosition(newY);
|
|
||||||
if (setter) {
|
|
||||||
setter({
|
|
||||||
x: newX,
|
x: newX,
|
||||||
y: newY
|
y: newY
|
||||||
})
|
})
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMouseUp = () => setIsDragging(false);
|
const handleMouseUp = () => setIsDragging(false);
|
||||||
@ -63,7 +55,7 @@ const DraggableBox = ({
|
|||||||
window.removeEventListener('mousemove', handleMouseMove);
|
window.removeEventListener('mousemove', handleMouseMove);
|
||||||
window.removeEventListener('mouseup', handleMouseUp);
|
window.removeEventListener('mouseup', handleMouseUp);
|
||||||
};
|
};
|
||||||
}, [isDragging, dragOffset, xPosition, yPosition, initialX, initialY]);
|
}, [isDragging, dragOffset, position.x, position.y]);
|
||||||
|
|
||||||
const handleMouseDown = (e: MouseEvent) => {
|
const handleMouseDown = (e: MouseEvent) => {
|
||||||
if (!dragRef.current?.parentElement) return;
|
if (!dragRef.current?.parentElement) return;
|
||||||
@ -71,8 +63,8 @@ const DraggableBox = ({
|
|||||||
const parentRect = dragRef.current.parentElement.getBoundingClientRect();
|
const parentRect = dragRef.current.parentElement.getBoundingClientRect();
|
||||||
|
|
||||||
setDragOffset({
|
setDragOffset({
|
||||||
x: e.clientX - parentRect.left - xPosition,
|
x: e.clientX - parentRect.left - position.x,
|
||||||
y: e.clientY - parentRect.top - yPosition
|
y: e.clientY - parentRect.top - position.y
|
||||||
});
|
});
|
||||||
setIsDragging(true);
|
setIsDragging(true);
|
||||||
};
|
};
|
||||||
@ -82,7 +74,7 @@ const DraggableBox = ({
|
|||||||
ref={dragRef}
|
ref={dragRef}
|
||||||
className={`absolute cursor-pointer ${className}`}
|
className={`absolute cursor-pointer ${className}`}
|
||||||
style={{
|
style={{
|
||||||
transform: `translate(${xPosition}px, ${yPosition}px)`,
|
transform: `translate(${position.x}px, ${position.y}px)`,
|
||||||
userSelect: 'none',
|
userSelect: 'none',
|
||||||
}}
|
}}
|
||||||
onMouseDown={handleMouseDown}
|
onMouseDown={handleMouseDown}
|
||||||
|
Loading…
Reference in New Issue
Block a user