fix double frame request

This commit is contained in:
Vinzenz Schroeter 2024-04-30 23:45:00 +02:00 committed by RobbersDaughter
parent 061c893456
commit 0b10695e07

View file

@ -119,10 +119,13 @@ export default function ClientScreen({theme, player}: {
useEffect(() => { useEffect(() => {
if (lastMessage === null) if (lastMessage === null)
return; return;
if (canvasRef.current === null)
let ignore = false;
const start = async () => {
const canvas = canvasRef.current;
if (canvas === null)
throw new Error('canvas null'); throw new Error('canvas null');
const canvas = canvasRef.current;
const drawContext = canvas.getContext('2d'); const drawContext = canvas.getContext('2d');
if (!drawContext) if (!drawContext)
throw new Error('could not get draw context'); throw new Error('could not get draw context');
@ -134,6 +137,9 @@ export default function ClientScreen({theme, player}: {
pixels = pixels.slice(0, observerMessageSize); pixels = pixels.slice(0, observerMessageSize);
} }
if (ignore)
return;
drawPixelsToCanvas({ drawPixelsToCanvas({
context: drawContext, context: drawContext,
width: canvas.width, width: canvas.width,
@ -145,7 +151,17 @@ export default function ClientScreen({theme, player}: {
playerColor: normalizeColor(drawContext, hslToString(theme.secondary)), playerColor: normalizeColor(drawContext, hslToString(theme.secondary)),
otherTanksColor: normalizeColor(drawContext, hslToString(theme.tertiary)) otherTanksColor: normalizeColor(drawContext, hslToString(theme.tertiary))
}); });
if (ignore)
return;
sendMessage(''); sendMessage('');
};
start();
return () => {
ignore = true;
};
}, [lastMessage, canvasRef.current, theme]); }, [lastMessage, canvasRef.current, theme]);
return <canvas ref={canvasRef} id="screen" width={pixelsPerRow} height={pixelsPerCol}/>; return <canvas ref={canvasRef} id="screen" width={pixelsPerRow} height={pixelsPerCol}/>;