Compare commits

..

No commits in common. "1ac04b1bf3a8afd12f89d1df69b1cfd7847ff554" and "dc5247c4dbc42be777f0396707154cf93ecbf96c" have entirely different histories.

3 changed files with 33 additions and 56 deletions

View File

@ -1,24 +1,19 @@
import DragableBox from "./DragableBox"; import DragableBox from "./DragableBox";
import { ChangeEvent, useEffect, useRef, useState } from "react"; import { 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<Coordinates>({ const [firstDragable, setFirstDragable] = useState({
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<Coordinates>({ const [secondDragable, setSecondDragable] = useState({
x: 0, x: 0,
y: 0 y: 0
}) })
@ -36,28 +31,6 @@ 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);
@ -82,19 +55,15 @@ export default function CropSelector() {
width: `${selectorWidth}px`, width: `${selectorWidth}px`,
}} }}
/> />
<DragableBox position={firstDragable} coordSetter={setFirstDragable} > <DragableBox initialX={0} initialY={0} setter={setFirstDragable}>
<PhAngle /> <PhAngle />
</DragableBox> </DragableBox>
<DragableBox position={secondDragable} coordSetter={setSecondDragable} > <DragableBox initialX={containerWidth} initialY={containerHeight} setter={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>

View File

@ -6,20 +6,24 @@ type Coordinates = {
} }
interface DraggableBoxProps { interface DraggableBoxProps {
position: Coordinates; initialX: number;
initialY: number;
className?: string; className?: string;
children?: React.ReactNode; children?: React.ReactNode;
coordSetter: Dispatch<SetStateAction<Coordinates>>; setter?: Dispatch<SetStateAction<Coordinates>>;
} }
const DraggableBox = ({ const DraggableBox = ({
position, initialX,
initialY,
className = "", className = "",
children = "", children = "",
coordSetter setter
}: 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(() => {
@ -40,10 +44,14 @@ 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)));
coordSetter({ setXPosition(newX);
setYPosition(newY);
if (setter) {
setter({
x: newX, x: newX,
y: newY y: newY
}) })
}
}; };
const handleMouseUp = () => setIsDragging(false); const handleMouseUp = () => setIsDragging(false);
@ -55,7 +63,7 @@ const DraggableBox = ({
window.removeEventListener('mousemove', handleMouseMove); window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('mouseup', handleMouseUp); window.removeEventListener('mouseup', handleMouseUp);
}; };
}, [isDragging, dragOffset, position.x, position.y]); }, [isDragging, dragOffset, xPosition, yPosition, initialX, initialY]);
const handleMouseDown = (e: MouseEvent) => { const handleMouseDown = (e: MouseEvent) => {
if (!dragRef.current?.parentElement) return; if (!dragRef.current?.parentElement) return;
@ -63,8 +71,8 @@ const DraggableBox = ({
const parentRect = dragRef.current.parentElement.getBoundingClientRect(); const parentRect = dragRef.current.parentElement.getBoundingClientRect();
setDragOffset({ setDragOffset({
x: e.clientX - parentRect.left - position.x, x: e.clientX - parentRect.left - xPosition,
y: e.clientY - parentRect.top - position.y y: e.clientY - parentRect.top - yPosition
}); });
setIsDragging(true); setIsDragging(true);
}; };
@ -74,7 +82,7 @@ const DraggableBox = ({
ref={dragRef} ref={dragRef}
className={`absolute cursor-pointer ${className}`} className={`absolute cursor-pointer ${className}`}
style={{ style={{
transform: `translate(${position.x}px, ${position.y}px)`, transform: `translate(${xPosition}px, ${yPosition}px)`,
userSelect: 'none', userSelect: 'none',
}} }}
onMouseDown={handleMouseDown} onMouseDown={handleMouseDown}

View File

@ -2,11 +2,11 @@
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
/*html,*/ html,
/*body {*/ body {
/* @apply bg-white dark:bg-gray-950;*/ @apply bg-white dark:bg-gray-950;
/**/
/* @media (prefers-color-scheme: dark) {*/ @media (prefers-color-scheme: dark) {
/* color-scheme: dark;*/ color-scheme: dark;
/* }*/ }
/*}*/ }