servicepoint-tanks/tank-frontend/src/ClientScreen.tsx

70 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-04-07 21:09:52 +02:00
import useWebSocket from 'react-use-websocket';
import {useEffect, useRef} from 'react';
import './ClientScreen.css';
const pixelsPerRow = 352;
const pixelsPerCol = 160;
2024-04-11 20:48:21 +02:00
const onColor = [0, 180, 0, 255];
const offColor = [0, 0, 0, 255];
function getIndexes(bitIndex: number) {
return {
byteIndex: Math.floor(bitIndex / 8),
bitInByteIndex: 7 - bitIndex % 8
};
}
function drawPixelsToCanvas(pixels: Uint8Array, canvas: HTMLCanvasElement) {
const drawContext = canvas.getContext('2d');
if (!drawContext)
throw new Error('could not get draw context');
const imageData = drawContext.getImageData(0, 0, canvas.width, canvas.height, {colorSpace: 'srgb'});
const data = imageData.data;
for (let y = 0; y < canvas.height; y++) {
const rowStartPixelIndex = y * pixelsPerRow;
for (let x = 0; x < canvas.width; x++) {
const pixelIndex = rowStartPixelIndex + x;
const {byteIndex, bitInByteIndex} = getIndexes(pixelIndex);
const mask = (1 << bitInByteIndex);
2024-04-11 20:48:21 +02:00
const isOn = (pixels[byteIndex] & mask) !== 0;
const color = isOn ? onColor : offColor;
2024-04-11 20:48:21 +02:00
for (let colorChannel of [0, 1, 2, 3])
data[pixelIndex * 4 + colorChannel] = color[colorChannel];
}
}
drawContext.putImageData(imageData, 0, 0);
}
2024-04-11 20:48:21 +02:00
2024-04-13 23:07:08 +02:00
export default function ClientScreen({logout}: { logout: () => void }) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const {
lastMessage,
sendMessage,
getWebSocket
} = useWebSocket(import.meta.env.VITE_TANK_SCREEN_URL, {
onError: logout
});
const socket = getWebSocket();
if (socket)
(socket as WebSocket).binaryType = 'arraybuffer';
useEffect(() => {
if (lastMessage === null)
return;
if (canvasRef.current === null)
throw new Error('canvas null');
drawPixelsToCanvas(new Uint8Array(lastMessage.data), canvasRef.current);
sendMessage('');
}, [lastMessage, canvasRef.current]);
2024-04-13 23:07:08 +02:00
return <canvas ref={canvasRef} id="screen" width={pixelsPerRow} height={pixelsPerCol}/>;
}