Skip to content
BlankOverlay.tsx 1.3 KiB
Newer Older
Grant's avatar
Grant committed
import { useEffect, useRef } from "react";
import { useAppContext } from "../../contexts/AppContext";
import { KeybindManager } from "../../lib/keybinds";
Grant's avatar
Grant committed
import { getRenderer } from "../../lib/utils";
Grant's avatar
Grant committed

Grant's avatar
Grant committed
export const BlankOverlay = () => {
Grant's avatar
Grant committed
  const { blankOverlay, setBlankOverlay } = useAppContext();
Grant's avatar
Grant committed
  const canvasRef = useRef<HTMLCanvasElement | null>(null);

  useEffect(() => {
    const handleKeybind = () => {
Grant's avatar
Grant committed
      setBlankOverlay((v) => ({ ...v, enabled: !v.enabled }));
Grant's avatar
Grant committed
    KeybindManager.on("TOGGLE_BLANK", handleKeybind);
Grant's avatar
Grant committed

    return () => {
Grant's avatar
Grant committed
      KeybindManager.off("TOGGLE_BLANK", handleKeybind);
Grant's avatar
Grant committed
    };
Grant's avatar
Grant committed
  }, [setBlankOverlay]);
Grant's avatar
Grant committed

  useEffect(() => {
    if (!canvasRef.current) {
      return;
    }

Grant's avatar
Grant committed
    let timeout = setTimeout(() => {
      if (!canvasRef.current) return;
Grant's avatar
Grant committed
      getRenderer().useCanvas(canvasRef.current, "blank");
    }, 1000);
Grant's avatar
Grant committed

    return () => {
Grant's avatar
Grant committed
      clearTimeout(timeout);
      getRenderer().removeCanvas("blank");
Grant's avatar
Grant committed
    };
Grant's avatar
Grant committed
  }, [canvasRef.current]);
Grant's avatar
Grant committed

  return (
    <canvas
Grant's avatar
Grant committed
      id="blank-overlay"
Grant's avatar
Grant committed
      className="board-overlay no-interact pixelate"
      ref={(r) => (canvasRef.current = r)}
      width="1000"
      height="1000"
      style={{
Grant's avatar
Grant committed
        display: blankOverlay.enabled ? "block" : "none",
        opacity: blankOverlay.opacity.toFixed(1),
Grant's avatar
Grant committed
      }}
    />
  );
};