2025-11-21 10:12:42 +00:00
|
|
|
import { useRef, type RefObject } from "react";
|
2025-11-21 16:01:34 +00:00
|
|
|
import { Stage, Layer, Image, Shape } from "react-konva";
|
|
|
|
|
import type { KonvaEventObject } from "konva/lib/Node";
|
2025-11-21 10:12:42 +00:00
|
|
|
import { useCreateVideoSnapshot } from "../hooks/useGetvideoSnapshots";
|
2025-11-21 16:01:34 +00:00
|
|
|
import Card from "../../../ui/Card";
|
|
|
|
|
import type { Region } from "../../../types/types";
|
2025-11-21 10:12:42 +00:00
|
|
|
|
2025-11-21 16:01:34 +00:00
|
|
|
const rows = 40;
|
|
|
|
|
const cols = 40;
|
|
|
|
|
const size = 20;
|
|
|
|
|
const gap = 0;
|
|
|
|
|
|
|
|
|
|
type VideoFeedGridPainterProps = {
|
|
|
|
|
regions: Region[];
|
|
|
|
|
selectedRegionIndex: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const VideoFeedGridPainter = ({ regions, selectedRegionIndex }: VideoFeedGridPainterProps) => {
|
2025-11-21 10:12:42 +00:00
|
|
|
const { latestBitmapRef } = useCreateVideoSnapshot();
|
2025-11-21 16:01:34 +00:00
|
|
|
|
|
|
|
|
const isDrawingRef = useRef(false);
|
|
|
|
|
const paintedCellsRef = useRef<Set<string>>(new Set());
|
|
|
|
|
|
|
|
|
|
const paintLayerRef = useRef<any>(null);
|
|
|
|
|
|
|
|
|
|
const draw = (bmp: RefObject<ImageBitmap | null>) => {
|
|
|
|
|
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();
|
2025-11-21 10:12:42 +00:00
|
|
|
};
|
|
|
|
|
|
2025-11-21 16:01:34 +00:00
|
|
|
const handleStageMouseDown = (e: KonvaEventObject<MouseEvent>) => {
|
|
|
|
|
if (!selectedRegionIndex) return;
|
|
|
|
|
isDrawingRef.current = true;
|
|
|
|
|
const pos = e.target.getStage()?.getPointerPosition();
|
|
|
|
|
if (pos) paintCell(pos.x, pos.y);
|
2025-11-21 10:12:42 +00:00
|
|
|
};
|
|
|
|
|
|
2025-11-21 16:01:34 +00:00
|
|
|
const handleStageMouseMove = (e: KonvaEventObject<MouseEvent>) => {
|
|
|
|
|
if (!isDrawingRef.current) return;
|
|
|
|
|
if (!selectedRegionIndex) return;
|
|
|
|
|
const pos = e.target.getStage()?.getPointerPosition();
|
|
|
|
|
if (pos) paintCell(pos.x, pos.y);
|
2025-11-21 10:12:42 +00:00
|
|
|
};
|
|
|
|
|
|
2025-11-21 16:01:34 +00:00
|
|
|
const handleStageMouseUp = () => {
|
|
|
|
|
isDrawingRef.current = false;
|
2025-11-21 10:12:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-11-21 16:01:34 +00:00
|
|
|
<Card className="w-187.5 place-self-start">
|
|
|
|
|
<Stage
|
|
|
|
|
width={740}
|
|
|
|
|
height={460}
|
|
|
|
|
onMouseDown={handleStageMouseDown}
|
|
|
|
|
onMouseMove={handleStageMouseMove}
|
|
|
|
|
onMouseUp={handleStageMouseUp}
|
|
|
|
|
onMouseLeave={handleStageMouseUp}
|
|
|
|
|
>
|
|
|
|
|
<Layer>
|
|
|
|
|
<Image image={draw(latestBitmapRef)} width={740} height={460} />
|
|
|
|
|
</Layer>
|
|
|
|
|
|
|
|
|
|
<Layer ref={paintLayerRef} opacity={0.6}>
|
|
|
|
|
<Shape
|
|
|
|
|
sceneFunc={(ctx, shape) => {
|
|
|
|
|
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);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Layer>
|
|
|
|
|
</Stage>
|
|
|
|
|
</Card>
|
2025-11-21 10:12:42 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default VideoFeedGridPainter;
|