diff --git a/app/routes/CropSelector.tsx b/app/routes/CropSelector.tsx index b4f2c39..c240e2b 100644 --- a/app/routes/CropSelector.tsx +++ b/app/routes/CropSelector.tsx @@ -1,19 +1,24 @@ import DragableBox from "./DragableBox"; -import { useEffect, useRef, useState } from "react"; +import { ChangeEvent, useEffect, useRef, useState } from "react"; import { PhAngle } from "./PhAngle"; +type Coordinates = { + x: number; + y: number; +} + export default function CropSelector() { const [selectorHeight, setSelectorHeight] = useState(0); const [selectorWidth, setSelectorWidth] = useState(0); const [selectorTop, setSelectorTop] = useState(0); const [selectorLeft, setSelectorLeft] = useState(0); - const [firstDragable, setFirstDragable] = useState({ + const [firstDragable, setFirstDragable] = useState({ x: 0, y: 0 }) const [containerWidth, setContainerWidth] = useState(null) const [containerHeight, setContainerHeight] = useState(null) - const [secondDragable, setSecondDragable] = useState({ + const [secondDragable, setSecondDragable] = useState({ x: 0, y: 0 }) @@ -31,6 +36,28 @@ export default function CropSelector() { return () => window.removeEventListener('resize', updateContainerSize); }, []) + const handleChange = (event: ChangeEvent) => { + 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(() => { const width = Math.abs(secondDragable.x - firstDragable.x); @@ -55,15 +82,19 @@ export default function CropSelector() { width: `${selectorWidth}px`, }} /> - + - + )} + + + +

{containerWidth} - {containerHeight}

firstDragable: x: {firstDragable.x.toFixed(2)} y: {firstDragable.y.toFixed(2)}

diff --git a/app/routes/DragableBox.tsx b/app/routes/DragableBox.tsx index e2138e2..62b0d9e 100644 --- a/app/routes/DragableBox.tsx +++ b/app/routes/DragableBox.tsx @@ -6,24 +6,20 @@ type Coordinates = { } interface DraggableBoxProps { - initialX: number; - initialY: number; + position: Coordinates; className?: string; children?: React.ReactNode; - setter?: Dispatch>; + coordSetter: Dispatch>; } const DraggableBox = ({ - initialX, - initialY, + position, className = "", children = "", - setter + coordSetter }: DraggableBoxProps) => { const [isDragging, setIsDragging] = useState(false); const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 }); - const [xPosition, setXPosition] = useState(initialX) - const [yPosition, setYPosition] = useState(initialY) const dragRef = useRef(null); useEffect(() => { @@ -44,14 +40,10 @@ const DraggableBox = ({ let newX = (Math.max(0, Math.min(relativeX, maxX))); let newY = (Math.max(0, Math.min(relativeY, maxY))); - setXPosition(newX); - setYPosition(newY); - if (setter) { - setter({ - x: newX, - y: newY - }) - } + coordSetter({ + x: newX, + y: newY + }) }; const handleMouseUp = () => setIsDragging(false); @@ -63,7 +55,7 @@ const DraggableBox = ({ window.removeEventListener('mousemove', handleMouseMove); window.removeEventListener('mouseup', handleMouseUp); }; - }, [isDragging, dragOffset, xPosition, yPosition, initialX, initialY]); + }, [isDragging, dragOffset, position.x, position.y]); const handleMouseDown = (e: MouseEvent) => { if (!dragRef.current?.parentElement) return; @@ -71,8 +63,8 @@ const DraggableBox = ({ const parentRect = dragRef.current.parentElement.getBoundingClientRect(); setDragOffset({ - x: e.clientX - parentRect.left - xPosition, - y: e.clientY - parentRect.top - yPosition + x: e.clientX - parentRect.left - position.x, + y: e.clientY - parentRect.top - position.y }); setIsDragging(true); }; @@ -82,7 +74,7 @@ const DraggableBox = ({ ref={dragRef} className={`absolute cursor-pointer ${className}`} style={{ - transform: `translate(${xPosition}px, ${yPosition}px)`, + transform: `translate(${position.x}px, ${position.y}px)`, userSelect: 'none', }} onMouseDown={handleMouseDown}