import DragableBox from "./DragableBox"; 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({ x: 0, y: 0 }) const [containerWidth, setContainerWidth] = useState(null) const [containerHeight, setContainerHeight] = useState(null) const [secondDragable, setSecondDragable] = useState({ x: 0, y: 0 }) const containerRef = useRef(null) useEffect(() => { const updateContainerSize = () => { if (containerRef.current) { setContainerWidth(containerRef.current.offsetWidth); setContainerHeight(containerRef.current.offsetHeight); } } updateContainerSize(); window.addEventListener('resize', updateContainerSize); 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); 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 (
{containerHeight !== null && containerWidth !== null && ( <>
)}

{containerWidth} - {containerHeight}

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

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

); }