Newer
Older
import { useEffect, useRef } from "react";
import { useAppContext } from "../../contexts/AppContext";
import { Canvas } from "../../lib/canvas";
import { KeybindManager } from "../../lib/keybinds";
export const VirginOverlay = () => {

Grant
committed
const { config, virginOverlay, setVirginOverlay } = useAppContext();
const canvasRef = useRef<HTMLCanvasElement | null>(null);
useEffect(() => {
const handleKeybind = () => {

Grant
committed
setVirginOverlay((v) => ({ ...v, enabled: !v.enabled }));
};
KeybindManager.on("TOGGLE_VIRGIN", handleKeybind);
return () => {
KeybindManager.off("TOGGLE_VIRGIN", handleKeybind);
};

Grant
committed
}, [setVirginOverlay]);
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
useEffect(() => {
if (!config) {
console.warn("[VirginOverlay] config is not defined");
return;
}
if (!canvasRef.current) {
console.warn("[VirginOverlay] canvasRef is not defined");
return;
}
const [width, height] = config.canvas.size;
canvasRef.current.width = width;
canvasRef.current.height = height;
}, [config]);
useEffect(() => {
if (!canvasRef.current) {
console.warn("[VirginOverlay] canvasRef is not defined");
return;
}
const updateVirginmap = () => {
const ctx = canvasRef.current!.getContext("2d");
if (!ctx) {
console.warn("[VirginOverlay] canvas context cannot be aquired");
return;
}
ctx.clearRect(0, 0, canvasRef.current!.width, canvasRef.current!.height);
const pixels = Canvas.instance!.getAllPixels();
for (const pixel of pixels) {
if (pixel.color !== -1) continue;
ctx.fillStyle = "rgba(0,140,0,0.5)";
ctx.fillRect(pixel.x, pixel.y, 1, 1);
}
};
var updateInterval = setInterval(updateVirginmap, 1000);
return () => {
clearInterval(updateInterval);
};
}, [canvasRef]);
return (
<canvas
id="virgin-overlay"
className="board-overlay no-interact pixelate"
ref={(r) => (canvasRef.current = r)}
width="1000"
height="1000"
style={{

Grant
committed
display: virginOverlay.enabled ? "block" : "none",
opacity: virginOverlay.opacity.toFixed(1),