- improved grid painting and included resizing

This commit is contained in:
2025-11-24 12:19:51 +00:00
parent c5fe6754c3
commit b084c3016d
7 changed files with 70 additions and 20 deletions

View File

@@ -1,8 +1,9 @@
import { useState } from "react"; import { useState } from "react";
import VideoFeedGridPainter from "./VideoFeedGridPainter"; import VideoFeedGridPainter from "./Video/VideoFeedGridPainter";
import CameraSettings from "./CameraSettings"; import CameraSettings from "./CameraSettings/CameraSettings";
import type { Region } from "../../../types/types"; import type { Region } from "../../../types/types";
import PlatePatch from "./PlatePatch/PlatePatch";
const CameraGrid = () => { const CameraGrid = () => {
const [regions, setRegions] = useState<Region[]>([ const [regions, setRegions] = useState<Region[]>([
@@ -17,7 +18,7 @@ const CameraGrid = () => {
setRegions((prev) => prev.map((r, i) => (i === index ? { ...r, brushColour: newColour } : r))); setRegions((prev) => prev.map((r, i) => (i === index ? { ...r, brushColour: newColour } : r)));
}; };
return ( return (
<div className="grid grid-cols-1 md:grid-cols-2"> <div className="grid grid-cols-1 md:grid-cols-5 grid-rows-2">
<VideoFeedGridPainter regions={regions} selectedRegionIndex={selectedRegionIndex} isErasing={isErasing} /> <VideoFeedGridPainter regions={regions} selectedRegionIndex={selectedRegionIndex} isErasing={isErasing} />
<CameraSettings <CameraSettings
regions={regions} regions={regions}
@@ -27,6 +28,7 @@ const CameraGrid = () => {
isErasing={isErasing} isErasing={isErasing}
onSelectErasing={setErasing} onSelectErasing={setErasing}
/> />
<PlatePatch />
</div> </div>
); );
}; };

View File

@@ -1,8 +1,9 @@
import Card from "../../../ui/Card"; import Card from "../../../../ui/Card";
import { Tab, Tabs, TabList, TabPanel } from "react-tabs"; import { Tab, Tabs, TabList, TabPanel } from "react-tabs";
import "react-tabs/style/react-tabs.css"; import "react-tabs/style/react-tabs.css";
import RegionSelector from "./RegionSelector"; import RegionSelector from "./RegionSelector";
import type { Region } from "../../../types/types"; import type { Region } from "../../../../types/types";
import { useState } from "react";
type CameraSettingsProps = { type CameraSettingsProps = {
regions: Region[]; regions: Region[];
@@ -21,12 +22,19 @@ const CameraSettings = ({
isErasing, isErasing,
onSelectErasing, onSelectErasing,
}: CameraSettingsProps) => { }: CameraSettingsProps) => {
const [tabIndex, setTabIndex] = useState(0);
return ( return (
<Card className="p-4 min-h-screen w-[80%] place-self-end"> <Card className="p-4 max-h-screen col-span-3">
<Tabs selectedTabClassName="bg-gray-300 text-gray-900 font-semibold border-none rounded-sm"> <Tabs
selectedTabClassName="bg-gray-300 text-gray-900 font-semibold border-none rounded-sm mb-1"
className="react-tabs"
onSelect={(index) => setTabIndex(index)}
>
<TabList> <TabList>
<Tab>Target Detection</Tab> <Tab>Target Detection</Tab>
<Tab>Camera 1</Tab> <Tab>Camera 1</Tab>
<Tab>Camera 2</Tab>
<Tab>Camera 3</Tab>
</TabList> </TabList>
<TabPanel> <TabPanel>
<RegionSelector <RegionSelector
@@ -39,7 +47,13 @@ const CameraSettings = ({
/> />
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<div>Camera details</div> <div>Camera details {tabIndex}</div>
</TabPanel>
<TabPanel>
<div>Camera details {tabIndex}</div>
</TabPanel>
<TabPanel>
<div>Camera details {tabIndex}</div>
</TabPanel> </TabPanel>
</Tabs> </Tabs>
</Card> </Card>

View File

@@ -1,5 +1,5 @@
import ColourPicker from "./ColourPicker"; import ColourPicker from "./ColourPicker";
import type { Region } from "../../../types/types"; import type { Region } from "../../../../types/types";
type RegionSelectorProps = { type RegionSelectorProps = {
regions: Region[]; regions: Region[];

View File

@@ -0,0 +1,7 @@
import Card from "../../../../ui/Card";
const PlatePatch = () => {
return <Card>PlatePatch</Card>;
};
export default PlatePatch;

View File

@@ -1,9 +1,9 @@
import { useRef, type RefObject } from "react"; import { useEffect, useRef, useState, type RefObject } from "react";
import { Stage, Layer, Image, Shape } from "react-konva"; import { Stage, Layer, Image, Shape } from "react-konva";
import type { KonvaEventObject } from "konva/lib/Node"; import type { KonvaEventObject } from "konva/lib/Node";
import { useCreateVideoSnapshot } from "../hooks/useGetvideoSnapshots"; import { useCreateVideoSnapshot } from "../../hooks/useGetvideoSnapshots";
import Card from "../../../ui/Card"; import type { Region } from "../../../../types/types";
import type { Region } from "../../../types/types"; import Card from "../../../../ui/Card";
const rows = 40; const rows = 40;
const cols = 40; const cols = 40;
@@ -21,7 +21,8 @@ type PaintedCell = {
}; };
const VideoFeedGridPainter = ({ regions, selectedRegionIndex, isErasing }: VideoFeedGridPainterProps) => { const VideoFeedGridPainter = ({ regions, selectedRegionIndex, isErasing }: VideoFeedGridPainterProps) => {
const { latestBitmapRef } = useCreateVideoSnapshot(); const { latestBitmapRef, isloading } = useCreateVideoSnapshot();
const [stageSize, setStageSize] = useState({ width: 740, height: 460 });
const isDrawingRef = useRef(false); const isDrawingRef = useRef(false);
const paintedCellsRef = useRef<Map<string, PaintedCell>>(new Map()); const paintedCellsRef = useRef<Map<string, PaintedCell>>(new Map());
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -35,6 +36,8 @@ const VideoFeedGridPainter = ({ regions, selectedRegionIndex, isErasing }: Video
return image; return image;
}; };
const image = draw(latestBitmapRef);
const paintCell = (x: number, y: number) => { const paintCell = (x: number, y: number) => {
const col = Math.floor(x / (size + gap)); const col = Math.floor(x / (size + gap));
const row = Math.floor(y / (size + gap)); const row = Math.floor(y / (size + gap));
@@ -83,18 +86,39 @@ const VideoFeedGridPainter = ({ regions, selectedRegionIndex, isErasing }: Video
isDrawingRef.current = false; isDrawingRef.current = false;
}; };
useEffect(() => {
const handleResize = () => {
const width = window.innerWidth;
const aspectRatio = 740 / 460;
const newWidth = width * 0.36;
const newHeight = newWidth / aspectRatio;
setStageSize({ width: newWidth, height: newHeight });
};
handleResize();
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);
if (image === null || isloading)
return (
<Card className="row-span-1 col-span-2 rounded-lg p-4 w-full">
<span className="text-slate-500">Loading Video feed</span>
</Card>
);
return ( return (
<Card className="w-187.5 place-self-start"> <div className="row-span-1 col-span-2 rounded-lg">
<Stage <Stage
width={740} width={stageSize.width}
height={460} height={stageSize.height}
onMouseDown={handleStageMouseDown} onMouseDown={handleStageMouseDown}
onMouseMove={handleStageMouseMove} onMouseMove={handleStageMouseMove}
onMouseUp={handleStageMouseUp} onMouseUp={handleStageMouseUp}
onMouseLeave={handleStageMouseUp} onMouseLeave={handleStageMouseUp}
> >
<Layer> <Layer>
<Image image={draw(latestBitmapRef)} width={740} height={460} /> <Image image={image} width={stageSize.width} height={stageSize.height} classname={"rounded-lg"} />
</Layer> </Layer>
<Layer ref={paintLayerRef} opacity={0.6}> <Layer ref={paintLayerRef} opacity={0.6}>
@@ -117,10 +141,12 @@ const VideoFeedGridPainter = ({ regions, selectedRegionIndex, isErasing }: Video
ctx.fillStrokeShape(shape); ctx.fillStrokeShape(shape);
}} }}
width={stageSize.width}
height={stageSize.height}
/> />
</Layer> </Layer>
</Stage> </Stage>
</Card> </div>
); );
}; };

View File

@@ -6,6 +6,7 @@ export const useCreateVideoSnapshot = () => {
const { videoQuery } = useGetVideoFeed(); const { videoQuery } = useGetVideoFeed();
const snapShot = videoQuery?.data; const snapShot = videoQuery?.data;
const isloading = videoQuery.isPending;
useEffect(() => { useEffect(() => {
async function createBitmap() { async function createBitmap() {
@@ -22,5 +23,5 @@ export const useCreateVideoSnapshot = () => {
createBitmap(); createBitmap();
}, [snapShot]); }, [snapShot]);
return { latestBitmapRef }; return { latestBitmapRef, isloading };
}; };