Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion web/src/display/crosshair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export function createCrosshair(
overlayCanvas.style.left = "0";
overlayCanvas.style.pointerEvents = "none"; // Allow mouse events to pass through
overlayCanvas.style.zIndex = "10";

// The stylesheet paints every canvas black behind its picture; this one lies over the picture.
overlayCanvas.style.background = "transparent";

// Match the target canvas size
overlayCanvas.width = targetCanvas.width;
overlayCanvas.height = targetCanvas.height;
Expand Down
42 changes: 42 additions & 0 deletions web/src/display/crosshair_overlay_style.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @vitest-environment jsdom
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

import { afterEach, expect, it, vi } from "vitest";

import { createCrosshair } from "./crosshair";

const currentDir = dirname(fileURLToPath(import.meta.url));
const mainCss = readFileSync(resolve(currentDir, "../../main.css"), "utf8");

afterEach(() => {
vi.restoreAllMocks();
document.head.innerHTML = "";
document.body.innerHTML = "";
});

// nr-use: the shell's stylesheet paints every canvas black, and the sight's overlay is a canvas
// laid over the picture, so a captured light gun hid the whole game behind a black sheet.
it("Given the shell's stylesheet, When the light-gun sight's overlay is created over the picture, Then the overlay is see-through", () => {
// jsdom has no 2D context; creating the sight draws nothing, so an empty one is enough.
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue(
{} as CanvasRenderingContext2D,
);
const style = document.createElement("style");
style.textContent = mainCss;
document.head.appendChild(style);
const wrap = document.createElement("div");
wrap.className = "screen-wrap";
const screen = document.createElement("canvas");
screen.id = "screen";
wrap.appendChild(screen);
document.body.appendChild(wrap);

createCrosshair(screen);

const overlay = wrap.querySelectorAll("canvas")[1];
expect(overlay).toBeDefined();
expect(getComputedStyle(screen).backgroundColor).toBe("rgb(0, 0, 0)");
expect(getComputedStyle(overlay).backgroundColor).toBe("rgba(0, 0, 0, 0)");
});
Loading