- improved zoom while clicking on on image to zoom
This commit is contained in:
@@ -2,8 +2,14 @@ import { useEffect, useRef, useState, 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 { useCameraFeedContext } from "../../../../app/context/CameraFeedContext";
|
||||
import {
|
||||
useCameraFeedASocket,
|
||||
useCameraFeedBSocket,
|
||||
useCameraFeedCSocket,
|
||||
} from "../../../../app/context/WebSocketContext";
|
||||
import { ReadyState } from "react-use-websocket";
|
||||
import { toast } from "sonner";
|
||||
|
||||
const BACKEND_WIDTH = 640;
|
||||
const BACKEND_HEIGHT = 360;
|
||||
@@ -25,6 +31,7 @@ const VideoFeedGridPainter = () => {
|
||||
const isDrawingRef = useRef(false);
|
||||
const [scale, setScale] = useState(1);
|
||||
const [position, setPosition] = useState({ x: 0, y: 0 });
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const stageRef = useRef<any>(null);
|
||||
|
||||
@@ -34,6 +41,55 @@ const VideoFeedGridPainter = () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const paintLayerRef = useRef<any>(null);
|
||||
|
||||
const cameraASocket = useCameraFeedASocket();
|
||||
const cameraBSocket = useCameraFeedBSocket();
|
||||
const cameraCSocket = useCameraFeedCSocket();
|
||||
|
||||
const getCurrentSocket = () => {
|
||||
switch (cameraFeedID) {
|
||||
case "A":
|
||||
return cameraASocket;
|
||||
case "B":
|
||||
return cameraBSocket;
|
||||
case "C":
|
||||
return cameraCSocket;
|
||||
}
|
||||
};
|
||||
|
||||
const socket = getCurrentSocket();
|
||||
|
||||
const getMagnificationLevel = () => {
|
||||
const test = socket.data;
|
||||
if (!socket.data) return null;
|
||||
|
||||
if (!test || !test.magnificationLevel) return "0x";
|
||||
return test?.magnificationLevel;
|
||||
};
|
||||
|
||||
const handleZoomClick = (e: KonvaEventObject<MouseEvent>, cameraFeedID: "A" | "B" | "C") => {
|
||||
if (mode !== "zoom") return;
|
||||
const socket = getCurrentSocket();
|
||||
const stage = e.target.getStage();
|
||||
const coords = stage?.getPointerPosition();
|
||||
if (!coords || !socket) return;
|
||||
|
||||
const newX = coords.x / stageSize.width;
|
||||
const newY = coords.y / stageSize.height;
|
||||
|
||||
// Check if WebSocket is connected
|
||||
if (socket.readyState !== ReadyState.OPEN) {
|
||||
toast.error(`Camera ${cameraFeedID} WebSocket is not connected`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
socket.send(`ZOOM=${newX.toFixed(2)},${newY.toFixed(2)}`);
|
||||
} catch (error) {
|
||||
console.error("WebSocket send error:", error);
|
||||
toast.error(`Failed to send command to Camera ${cameraFeedID}`);
|
||||
}
|
||||
};
|
||||
|
||||
const draw = (bmp: RefObject<ImageBitmap | null>): ImageBitmap | null => {
|
||||
if (!bmp || !bmp.current) {
|
||||
return null;
|
||||
@@ -76,14 +132,14 @@ const VideoFeedGridPainter = () => {
|
||||
};
|
||||
|
||||
const handleStageMouseDown = (e: KonvaEventObject<MouseEvent>) => {
|
||||
if (!regions[selectedRegionIndex] || mode === "zoom") return;
|
||||
if (!regions[selectedRegionIndex] || mode === "magnify" || mode === "zoom") return;
|
||||
isDrawingRef.current = true;
|
||||
const pos = e.target.getStage()?.getPointerPosition();
|
||||
if (pos) paintCell(pos.x, pos.y);
|
||||
};
|
||||
|
||||
const handleStageMouseMove = (e: KonvaEventObject<MouseEvent>) => {
|
||||
if (!isDrawingRef.current || mode === "zoom") return;
|
||||
if (!isDrawingRef.current || mode === "magnify") return;
|
||||
if (!regions[selectedRegionIndex]) return;
|
||||
const pos = e.target.getStage()?.getPointerPosition();
|
||||
if (pos) paintCell(pos.x, pos.y);
|
||||
@@ -94,7 +150,7 @@ const VideoFeedGridPainter = () => {
|
||||
};
|
||||
|
||||
const handleMouseEnter = () => {
|
||||
if (mode !== "zoom") return;
|
||||
if (mode !== "magnify") return;
|
||||
setScale(2);
|
||||
};
|
||||
const handleMouseLeave = () => {
|
||||
@@ -171,7 +227,13 @@ const VideoFeedGridPainter = () => {
|
||||
x={position.x}
|
||||
y={position.y}
|
||||
>
|
||||
<Image image={image} width={stageSize.width} height={stageSize.height} classname={"rounded-lg"} />
|
||||
<Image
|
||||
image={image}
|
||||
width={stageSize.width}
|
||||
height={stageSize.height}
|
||||
classname={"rounded-lg"}
|
||||
onClick={(e) => handleZoomClick(e, cameraFeedID)}
|
||||
/>
|
||||
</Layer>
|
||||
|
||||
<Layer ref={paintLayerRef} opacity={0.6}>
|
||||
|
||||
Reference in New Issue
Block a user