- improved zoom while clicking on on image to zoom

This commit is contained in:
2025-12-10 13:09:07 +00:00
parent 0ff43d975d
commit 10e2644666
7 changed files with 126 additions and 41 deletions

View File

@@ -1,6 +1,6 @@
import { createContext, useContext } from "react"; import { createContext, useContext } from "react";
import { ReadyState } from "react-use-websocket"; import { ReadyState } from "react-use-websocket";
import type { InfoBarData } from "../../types/types"; import type { CameraZoomData, InfoBarData } from "../../types/types";
type InfoSocketState = { type InfoSocketState = {
data: InfoBarData | null; data: InfoBarData | null;
@@ -10,7 +10,7 @@ type InfoSocketState = {
}; };
type CameraSocketState = { type CameraSocketState = {
data: null; data: CameraZoomData | null;
readyState: ReadyState; readyState: ReadyState;
send: (msg: string) => void; send: (msg: string) => void;
}; };

View File

@@ -2,7 +2,7 @@ import { useEffect, useMemo, useState, type ReactNode } from "react";
import { WebsocketContext, type WebSocketConextValue } from "../context/WebSocketContext"; import { WebsocketContext, type WebSocketConextValue } from "../context/WebSocketContext";
import useWebSocket from "react-use-websocket"; import useWebSocket from "react-use-websocket";
import { wsConfig } from "../config/wsconfig"; import { wsConfig } from "../config/wsconfig";
import type { InfoBarData } from "../../types/types"; import type { CameraZoomData, InfoBarData } from "../../types/types";
type WebSocketProviderProps = { type WebSocketProviderProps = {
children: ReactNode; children: ReactNode;
@@ -10,7 +10,7 @@ type WebSocketProviderProps = {
export const WebSocketProvider = ({ children }: WebSocketProviderProps) => { export const WebSocketProvider = ({ children }: WebSocketProviderProps) => {
const [systemData, setSystemData] = useState<InfoBarData | null>(null); const [systemData, setSystemData] = useState<InfoBarData | null>(null);
const [socketData, setSocketData] = useState<CameraZoomData | null>(null);
const infoSocket = useWebSocket(wsConfig.infoBar, { share: true, shouldReconnect: () => true }); const infoSocket = useWebSocket(wsConfig.infoBar, { share: true, shouldReconnect: () => true });
const cameraFeedASocket = useWebSocket(wsConfig.cameraFeedA, { share: true, shouldReconnect: () => true }); const cameraFeedASocket = useWebSocket(wsConfig.cameraFeedA, { share: true, shouldReconnect: () => true });
const cameraFeedBSocket = useWebSocket(wsConfig.cameraFeedB, { share: true, shouldReconnect: () => true }); const cameraFeedBSocket = useWebSocket(wsConfig.cameraFeedB, { share: true, shouldReconnect: () => true });
@@ -23,9 +23,20 @@ export const WebSocketProvider = ({ children }: WebSocketProviderProps) => {
const data = JSON.parse(text); const data = JSON.parse(text);
setSystemData(data); setSystemData(data);
} }
if (cameraFeedASocket.lastMessage || cameraFeedBSocket.lastMessage || cameraFeedCSocket.lastMessage) {
const message = cameraFeedASocket.lastMessage || cameraFeedBSocket.lastMessage || cameraFeedCSocket.lastMessage;
const data = await message?.data.text();
const parsedData: CameraZoomData = JSON.parse(data || "");
setSocketData(parsedData);
}
} }
parseData(); parseData();
}, [infoSocket.lastMessage]); }, [
cameraFeedASocket.lastMessage,
cameraFeedBSocket.lastMessage,
cameraFeedCSocket.lastMessage,
infoSocket.lastMessage,
]);
const value = useMemo<WebSocketConextValue>( const value = useMemo<WebSocketConextValue>(
() => ({ () => ({
@@ -35,19 +46,19 @@ export const WebSocketProvider = ({ children }: WebSocketProviderProps) => {
sendJson: infoSocket.sendJsonMessage, sendJson: infoSocket.sendJsonMessage,
}, },
cameraFeedA: { cameraFeedA: {
data: null, data: socketData,
readyState: cameraFeedASocket.readyState, readyState: cameraFeedASocket.readyState,
send: cameraFeedASocket.sendMessage, send: cameraFeedASocket.sendMessage,
}, },
cameraFeedB: { cameraFeedB: {
data: null, data: socketData,
readyState: cameraFeedBSocket.readyState, readyState: cameraFeedBSocket.readyState,
send: cameraFeedBSocket.sendMessage, send: cameraFeedBSocket.sendMessage,
}, },
cameraFeedC: { cameraFeedC: {
data: null, data: socketData,
readyState: cameraFeedCSocket.readyState, readyState: cameraFeedCSocket.readyState,
send: cameraFeedCSocket.sendMessage, send: cameraFeedCSocket.sendMessage,
@@ -62,6 +73,7 @@ export const WebSocketProvider = ({ children }: WebSocketProviderProps) => {
cameraFeedCSocket.sendMessage, cameraFeedCSocket.sendMessage,
infoSocket.readyState, infoSocket.readyState,
infoSocket.sendJsonMessage, infoSocket.sendJsonMessage,
socketData,
systemData, systemData,
], ],
); );

View File

@@ -4,7 +4,6 @@ import { useCameraFeedContext } from "../../../../app/context/CameraFeedContext"
import { useColourDectection } from "../../hooks/useColourDetection"; import { useColourDectection } from "../../hooks/useColourDetection";
import { useBlackBoard } from "../../../../hooks/useBlackBoard"; import { useBlackBoard } from "../../../../hooks/useBlackBoard";
import { toast } from "sonner"; import { toast } from "sonner";
import { ReadyState } from "react-use-websocket";
import { import {
useCameraFeedASocket, useCameraFeedASocket,
useCameraFeedBSocket, useCameraFeedBSocket,
@@ -34,8 +33,6 @@ const RegionSelector = ({
const { state, dispatch } = useCameraFeedContext(); const { state, dispatch } = useCameraFeedContext();
const { blackboardMutation } = useBlackBoard(); const { blackboardMutation } = useBlackBoard();
const paintedCells = state.paintedCells[cameraFeedID]; const paintedCells = state.paintedCells[cameraFeedID];
// Get the socket for the current camera only
const cameraASocket = useCameraFeedASocket(); const cameraASocket = useCameraFeedASocket();
const cameraBSocket = useCameraFeedBSocket(); const cameraBSocket = useCameraFeedBSocket();
const cameraCSocket = useCameraFeedCSocket(); const cameraCSocket = useCameraFeedCSocket();
@@ -51,6 +48,16 @@ const RegionSelector = ({
} }
}; };
const socket = getCurrentSocket();
const getMagnificationLevel = () => {
const test = socket.data;
if (!socket.data) return null;
if (!test || !test.magnificationLevel) return "0x";
return test?.magnificationLevel;
};
const handleChange = (e: { target: { value: string } }) => { const handleChange = (e: { target: { value: string } }) => {
dispatch({ type: "CHANGE_MODE", payload: { cameraFeedID: cameraFeedID, mode: e.target.value } }); dispatch({ type: "CHANGE_MODE", payload: { cameraFeedID: cameraFeedID, mode: e.target.value } });
}; };
@@ -99,23 +106,6 @@ const RegionSelector = ({
setIsResetModalOpen(true); setIsResetModalOpen(true);
}; };
const textClick = (cameraFeedID: "A" | "B" | "C") => {
const socket = getCurrentSocket();
// Check if WebSocket is connected
if (socket.readyState !== ReadyState.OPEN) {
toast.error(`Camera ${cameraFeedID} WebSocket is not connected`);
return;
}
try {
socket.send("ZOOM=0.3,0.3");
toast.success(`Zoom command sent to Camera ${cameraFeedID}`);
} catch (error) {
console.error("WebSocket send error:", error);
toast.error(`Failed to send command to Camera ${cameraFeedID}`);
}
};
const handleSaveclick = () => { const handleSaveclick = () => {
const regions: ColourData[] = []; const regions: ColourData[] = [];
const test = Array.from(paintedCells.entries()); const test = Array.from(paintedCells.entries());
@@ -213,6 +203,25 @@ const RegionSelector = ({
/> />
<span className="text-xl">Erase mode</span> <span className="text-xl">Erase mode</span>
</label> </label>
<label
htmlFor="magnifyMode"
className={`p-4 border rounded-lg mb-2
${mode === "magnify" ? "border-gray-400 bg-[#202b36]" : "bg-[#253445] border-gray-700"}
hover:bg-[#202b36] hover:cursor-pointer`}
>
<input
id="magnifyMode"
type="radio"
onChange={handleChange}
checked={mode === "magnify"}
value="magnify"
className="sr-only"
/>
<div className="flex flex-col space-y-3">
<span className="text-xl">Magnifier</span>
{mode === "magnify" && <small className={`text-gray-400 italic`}>Use mouse to magnify the image</small>}
</div>
</label>
<label <label
htmlFor="zoomMode" htmlFor="zoomMode"
className={`p-4 border rounded-lg mb-2 className={`p-4 border rounded-lg mb-2
@@ -228,13 +237,11 @@ const RegionSelector = ({
className="sr-only" className="sr-only"
/> />
<div className="flex flex-col space-y-3"> <div className="flex flex-col space-y-3">
<span className="text-xl">Magnifier</span> <span className="text-xl">Digital Zoom mode</span>
{mode === "zoom" && ( <pre className="text-xs text-gray-400">{getMagnificationLevel()}</pre>
<small className={`text-gray-400 italic`}>Use mouse to digitally zoom in and out</small> {mode === "zoom" && <small className={`text-gray-400 italic`}>Click image to digitally zoom</small>}
)}
</div> </div>
</label> </label>
<button onClick={() => textClick(cameraFeedID)}>click me</button>
</div> </div>
</div> </div>

View File

@@ -2,8 +2,14 @@ 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 { useCameraFeedContext } from "../../../../app/context/CameraFeedContext"; 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_WIDTH = 640;
const BACKEND_HEIGHT = 360; const BACKEND_HEIGHT = 360;
@@ -25,6 +31,7 @@ const VideoFeedGridPainter = () => {
const isDrawingRef = useRef(false); const isDrawingRef = useRef(false);
const [scale, setScale] = useState(1); const [scale, setScale] = useState(1);
const [position, setPosition] = useState({ x: 0, y: 0 }); const [position, setPosition] = useState({ x: 0, y: 0 });
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const stageRef = useRef<any>(null); const stageRef = useRef<any>(null);
@@ -34,6 +41,55 @@ const VideoFeedGridPainter = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const paintLayerRef = useRef<any>(null); 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 => { const draw = (bmp: RefObject<ImageBitmap | null>): ImageBitmap | null => {
if (!bmp || !bmp.current) { if (!bmp || !bmp.current) {
return null; return null;
@@ -76,14 +132,14 @@ const VideoFeedGridPainter = () => {
}; };
const handleStageMouseDown = (e: KonvaEventObject<MouseEvent>) => { const handleStageMouseDown = (e: KonvaEventObject<MouseEvent>) => {
if (!regions[selectedRegionIndex] || mode === "zoom") return; if (!regions[selectedRegionIndex] || mode === "magnify" || mode === "zoom") return;
isDrawingRef.current = true; isDrawingRef.current = true;
const pos = e.target.getStage()?.getPointerPosition(); const pos = e.target.getStage()?.getPointerPosition();
if (pos) paintCell(pos.x, pos.y); if (pos) paintCell(pos.x, pos.y);
}; };
const handleStageMouseMove = (e: KonvaEventObject<MouseEvent>) => { const handleStageMouseMove = (e: KonvaEventObject<MouseEvent>) => {
if (!isDrawingRef.current || mode === "zoom") return; if (!isDrawingRef.current || mode === "magnify") return;
if (!regions[selectedRegionIndex]) return; if (!regions[selectedRegionIndex]) return;
const pos = e.target.getStage()?.getPointerPosition(); const pos = e.target.getStage()?.getPointerPosition();
if (pos) paintCell(pos.x, pos.y); if (pos) paintCell(pos.x, pos.y);
@@ -94,7 +150,7 @@ const VideoFeedGridPainter = () => {
}; };
const handleMouseEnter = () => { const handleMouseEnter = () => {
if (mode !== "zoom") return; if (mode !== "magnify") return;
setScale(2); setScale(2);
}; };
const handleMouseLeave = () => { const handleMouseLeave = () => {
@@ -171,7 +227,13 @@ const VideoFeedGridPainter = () => {
x={position.x} x={position.x}
y={position.y} 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>
<Layer ref={paintLayerRef} opacity={0.6}> <Layer ref={paintLayerRef} opacity={0.6}>

View File

@@ -28,14 +28,14 @@ export const useGetVideoFeed = (cameraFeedID: "A" | "B" | "C" | null, mode: stri
queryKey: ["getfeed", cameraFeedID], queryKey: ["getfeed", cameraFeedID],
queryFn: () => targetDectionFeed(cameraFeedID), queryFn: () => targetDectionFeed(cameraFeedID),
refetchInterval: 500, refetchInterval: 500,
enabled: mode !== "zoom", enabled: mode !== "magnify" && mode !== "zoom",
}); });
const videoFeedQuery = useQuery({ const videoFeedQuery = useQuery({
queryKey: ["videoQuery", cameraFeedID, mode], queryKey: ["videoQuery", cameraFeedID, mode],
queryFn: () => getVideoFeed(cameraFeedID), queryFn: () => getVideoFeed(cameraFeedID),
refetchInterval: 500, refetchInterval: 500,
enabled: mode === "zoom", enabled: mode === "magnify" || mode === "zoom",
}); });
return { targetDetectionQuery, videoFeedQuery }; return { targetDetectionQuery, videoFeedQuery };

View File

@@ -15,7 +15,7 @@ export const useCreateVideoSnapshot = () => {
const videoSnapShot = videoFeedQuery?.data; const videoSnapShot = videoFeedQuery?.data;
const isVideoLoading = videoFeedQuery.isPending; const isVideoLoading = videoFeedQuery.isPending;
if (isVideoLoading === false && videoSnapShot && mode === "zoom") { if ((isVideoLoading === false && videoSnapShot && mode === "magnify") || mode === "zoom") {
snapShot = videoSnapShot; snapShot = videoSnapShot;
} }

View File

@@ -11,6 +11,10 @@ export type InfoBarData = {
"thread-count": string; "thread-count": string;
}; };
export type CameraZoomData = {
magnificationLevel: string;
};
export type StatusIndicator = "neutral-quaternary" | "dark" | "info" | "success" | "warning" | "danger"; export type StatusIndicator = "neutral-quaternary" | "dark" | "info" | "success" | "warning" | "danger";
export type Region = { export type Region = {
name: string; name: string;