import { useRef, type RefObject } from "react"; import { Stage, Layer, Image, Shape } from "react-konva"; import type { KonvaEventObject } from "konva/lib/Node"; import { useCreateVideoSnapshot } from "../hooks/useGetvideoSnapshots"; import Card from "../../../ui/Card"; import type { Region } from "../../../types/types"; const rows = 40; const cols = 40; const size = 20; const gap = 0; type VideoFeedGridPainterProps = { regions: Region[]; selectedRegionIndex: number; }; const VideoFeedGridPainter = ({ regions, selectedRegionIndex }: VideoFeedGridPainterProps) => { const { latestBitmapRef } = useCreateVideoSnapshot(); const isDrawingRef = useRef(false); const paintedCellsRef = useRef>(new Set()); const paintLayerRef = useRef(null); const draw = (bmp: RefObject) => { if (!bmp || !bmp.current) return null; return bmp.current; }; const paintCell = (x: number, y: number) => { const col = Math.floor(x / (size + gap)); const row = Math.floor(y / (size + gap)); if (row < 0 || row >= rows || col < 0 || col >= cols) return; const key = `${row}-${col}`; const set = paintedCellsRef.current; if (set.has(key)) return; set.add(key); paintLayerRef.current?.batchDraw(); }; const handleStageMouseDown = (e: KonvaEventObject) => { if (!selectedRegionIndex) return; isDrawingRef.current = true; const pos = e.target.getStage()?.getPointerPosition(); if (pos) paintCell(pos.x, pos.y); }; const handleStageMouseMove = (e: KonvaEventObject) => { if (!isDrawingRef.current) return; if (!selectedRegionIndex) return; const pos = e.target.getStage()?.getPointerPosition(); if (pos) paintCell(pos.x, pos.y); }; const handleStageMouseUp = () => { isDrawingRef.current = false; }; return ( { const cells = paintedCellsRef.current; cells.forEach((key) => { const [rowStr, colStr] = key.split("-"); const row = Number(rowStr); const col = Number(colStr); const x = col * (size + gap); const y = row * (size + gap); ctx.beginPath(); ctx.rect(x, y, size, size); ctx.fillStyle = regions[selectedRegionIndex]?.brushColour; ctx.fill(); }); ctx.fillStrokeShape(shape); }} /> ); }; export default VideoFeedGridPainter;