image-converter-client/app/routes/_index.tsx

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-11-02 22:30:55 +00:00
import type { MetaFunction } from "@remix-run/cloudflare";
2024-11-02 15:47:17 +00:00
import DragableBox from "./DragableBox";
2024-11-02 22:30:55 +00:00
import { useEffect, useRef, useState } from "react";
import { PhAngle } from "./PhAngle";
2024-10-30 22:11:22 +00:00
export const meta: MetaFunction = () => {
return [
2024-11-02 15:47:17 +00:00
{ title: "Convert images for the web!" },
{ name: "image converter", content: "Image conversion service" },
2024-10-30 22:11:22 +00:00
];
};
export default function Index() {
2024-11-02 15:47:17 +00:00
const [selectorHeight, setSelectorHeight] = useState(0);
const [selectorWidth, setSelectorWidth] = useState(0);
const [selectorTop, setSelectorTop] = useState(0);
const [selectorLeft, setSelectorLeft] = useState(0);
2024-11-02 17:25:14 +00:00
const [firstDragable, setFirstDragable] = useState({
x: 0,
y: 0
})
2024-11-02 22:30:55 +00:00
const [containerWidth, setContainerWidth] = useState<number | null>(null)
const [containerHeight, setContainerHeight] = useState<number | null>(null)
2024-11-02 17:25:14 +00:00
const [secondDragable, setSecondDragable] = useState({
x: 0,
y: 0
})
2024-11-02 22:30:55 +00:00
const containerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const updateContainerSize = () => {
if (containerRef.current) {
setContainerWidth(containerRef.current.offsetWidth);
setContainerHeight(containerRef.current.offsetHeight);
}
}
window.addEventListener('resize', updateContainerSize);
return () => window.removeEventListener('resize', updateContainerSize);
}, [containerRef])
2024-11-02 15:47:17 +00:00
useEffect(() => {
2024-11-02 17:25:14 +00:00
const width = Math.abs(secondDragable.x - firstDragable.x);
2024-11-02 15:47:17 +00:00
setSelectorWidth(width);
2024-11-02 17:25:14 +00:00
const height = Math.abs(secondDragable.y - firstDragable.y);
2024-11-02 15:47:17 +00:00
setSelectorHeight(height);
2024-11-02 17:25:14 +00:00
setSelectorTop(Math.min(firstDragable.y, secondDragable.y));
setSelectorLeft(Math.min(firstDragable.x, secondDragable.x));
}, [firstDragable, secondDragable]);
2024-11-02 15:47:17 +00:00
2024-10-30 22:11:22 +00:00
return (
2024-11-02 15:47:17 +00:00
<div className="flex h-screen w-screen justify-center items-center">
2024-11-02 22:30:55 +00:00
<div ref={containerRef} className="relative w-1/3 h-1/3 border-black border">
{containerHeight !== null && containerWidth !== null && (
<>
<div
className="border border-black absolute"
style={{
top: `${selectorTop}px`,
left: `${selectorLeft}px`,
height: `${selectorHeight}px`,
width: `${selectorWidth}px`,
}}
/>
<DragableBox initialX={0} initialY={0} setter={setFirstDragable}>
<PhAngle />
</DragableBox>
<DragableBox initialX={containerWidth} initialY={containerHeight} setter={setSecondDragable} >
<PhAngle />
</DragableBox>
</>
)}
2024-11-02 15:47:17 +00:00
</div>
2024-11-02 22:30:55 +00:00
{containerWidth} - {containerHeight}
2024-11-02 15:47:17 +00:00
</div>
);
2024-10-30 22:11:22 +00:00
}