Split The code is public; the app answers only to the crew.
Play it fullscreen: the view and the touch pad together, on a phone and on a laptop #62
Check · Review · Land · Live done — Landed and live in production.
scout/cs:scout/51-play-it-fullscreen-the-view-and-the-touc → main 1 commit fork
6 files
changed +177
−2
README.md +8
| @@ -108,6 +108,14 @@ the gun once it is gone), <kbd>Tab</kbd> shows | ||
| 108 | 108 | the scoreboard, <kbd>Esc</kbd> pauses. Pausing also opens mute, volume and |
| 109 | 109 | look sensitivity, which take effect at once and are remembered for next time. |
| 110 | 110 | |
| 111 | A **Fullscreen** button sits in the menu and in the pause overlay — wherever | |
| 112 | the browser can do it at all; iOS Safari on an iPhone cannot, so there the | |
| 113 | button is not shown and the game plays exactly as it does without one. It | |
| 114 | fullscreens the view and the pad together, not the view alone, so a phone in | |
| 115 | fullscreen keeps every button it had before. Leaving fullscreen with the | |
| 116 | system gesture or <kbd>Esc</kbd> pauses a running match, the same as losing | |
| 117 | pointer lock does, and puts the page back exactly as it was. | |
| 118 | ||
| 111 | 119 | On a touch screen the same actions are a pad, and the view is the aim: |
| 112 | 120 | |
| 113 | 121 | | what | key | thumb | |
src/client/fullscreen.test.ts added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { fullscreenControl } from "./fullscreen.ts"; | |
| 3 | ||
| 4 | test("a browser that cannot go fullscreen never shows the control", () => { | |
| 5 | expect(fullscreenControl(false, false)).toEqual({ shown: false, label: "Fullscreen", pressed: false }); | |
| 6 | expect(fullscreenControl(false, true)).toEqual({ shown: false, label: "Fullscreen", pressed: false }); | |
| 7 | }); | |
| 8 | ||
| 9 | test("supported and not active: shown, unpressed, and says which way it will go", () => { | |
| 10 | const state = fullscreenControl(true, false); | |
| 11 | expect(state.shown).toBe(true); | |
| 12 | expect(state.pressed).toBe(false); | |
| 13 | expect(state.label).toBe("Fullscreen"); | |
| 14 | }); | |
| 15 | ||
| 16 | test("supported and active: shown, pressed, and the label reverses", () => { | |
| 17 | const state = fullscreenControl(true, true); | |
| 18 | expect(state.shown).toBe(true); | |
| 19 | expect(state.pressed).toBe(true); | |
| 20 | expect(state.label).toBe("Exit fullscreen"); | |
| 21 | }); |
src/client/fullscreen.ts added +75
| @@ -0,0 +1,75 @@ | ||
| 1 | /** | |
| 2 | * Fullscreen: a pure decision — is the control shown, and what does it say — | |
| 3 | * plus the thin DOM glue that asks for it. It always fullscreens the | |
| 4 | * *wrapper*, the element holding both `#stage` and `#touch`, never `#stage` | |
| 5 | * alone: fullscreening the canvas hides the pad and leaves a phone with no | |
| 6 | * buttons at all. | |
| 7 | */ | |
| 8 | ||
| 9 | export type FullscreenControl = { | |
| 10 | /** False on a browser that cannot do it at all — iOS Safari on an iPhone. */ | |
| 11 | shown: boolean; | |
| 12 | label: string; | |
| 13 | pressed: boolean; | |
| 14 | }; | |
| 15 | ||
| 16 | /** Pure: what the button shows, from support and the current element alone. */ | |
| 17 | export function fullscreenControl(supported: boolean, active: boolean): FullscreenControl { | |
| 18 | if (!supported) return { shown: false, label: "Fullscreen", pressed: false }; | |
| 19 | return { shown: true, label: active ? "Exit fullscreen" : "Fullscreen", pressed: active }; | |
| 20 | } | |
| 21 | ||
| 22 | export type FullscreenHandle = { | |
| 23 | toggle(): void; | |
| 24 | detach(): void; | |
| 25 | }; | |
| 26 | ||
| 27 | /** | |
| 28 | * Wires one or more buttons — the menu's and the pause overlay's are the same | |
| 29 | * switch — to the wrapper's fullscreen state. `onExit` fires only when the | |
| 30 | * wrapper itself drops out of fullscreen (the system gesture or `Esc`), never | |
| 31 | * for some other element's fullscreen coming or going, and never on entry. | |
| 32 | */ | |
| 33 | export function attachFullscreen( | |
| 34 | wrapper: HTMLElement, | |
| 35 | buttons: HTMLButtonElement[], | |
| 36 | onExit: () => void, | |
| 37 | ): FullscreenHandle { | |
| 38 | const supported = document.fullscreenEnabled === true; | |
| 39 | const isActive = (): boolean => document.fullscreenElement === wrapper; | |
| 40 | let wasActive = isActive(); | |
| 41 | ||
| 42 | const render = (): void => { | |
| 43 | const state = fullscreenControl(supported, isActive()); | |
| 44 | for (const button of buttons) { | |
| 45 | button.hidden = !state.shown; | |
| 46 | button.textContent = state.label; | |
| 47 | button.setAttribute("aria-pressed", String(state.pressed)); | |
| 48 | } | |
| 49 | }; | |
| 50 | ||
| 51 | const onChange = (): void => { | |
| 52 | const active = isActive(); | |
| 53 | if (wasActive && !active) onExit(); | |
| 54 | wasActive = active; | |
| 55 | render(); | |
| 56 | }; | |
| 57 | ||
| 58 | const toggle = (): void => { | |
| 59 | if (!supported) return; | |
| 60 | if (isActive()) void document.exitFullscreen(); | |
| 61 | else void wrapper.requestFullscreen().catch(() => {}); | |
| 62 | }; | |
| 63 | ||
| 64 | for (const button of buttons) button.addEventListener("click", toggle); | |
| 65 | document.addEventListener("fullscreenchange", onChange); | |
| 66 | render(); | |
| 67 | ||
| 68 | return { | |
| 69 | toggle, | |
| 70 | detach: () => { | |
| 71 | document.removeEventListener("fullscreenchange", onChange); | |
| 72 | for (const button of buttons) button.removeEventListener("click", toggle); | |
| 73 | }, | |
| 74 | }; | |
| 75 | } |
src/client/main.ts +8 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { createAudio } from "./audio.ts"; |
| 10 | import { attachFullscreen } from "./fullscreen.ts"; | |
| 10 | 11 | import { drawHud } from "./hud.ts"; |
| 11 | 12 | import { attachButtons, createControls } from "./input.ts"; |
| 12 | 13 | import { clamp, dist } from "./math.ts"; |
| @@ -36,6 +37,7 @@ const SCALE_EVERY_MS = 500; | ||
| 36 | 37 | const $ = <T extends HTMLElement>(id: string): T => document.getElementById(id) as T; |
| 37 | 38 | |
| 38 | 39 | function boot(): void { |
| 40 | const wrapper = $("wrapper"); | |
| 39 | 41 | const scene = $<HTMLCanvasElement>("scene"); |
| 40 | 42 | const hudCanvas = $<HTMLCanvasElement>("hud"); |
| 41 | 43 | const menu = $("menu"); |
| @@ -51,6 +53,8 @@ function boot(): void { | ||
| 51 | 53 | const pauseSensField = $<HTMLInputElement>("pause-sens"); |
| 52 | 54 | const volumeField = $<HTMLInputElement>("volume"); |
| 53 | 55 | const muteButton = $<HTMLButtonElement>("mute"); |
| 56 | const fullscreenMenuButton = $<HTMLButtonElement>("fullscreen-menu"); | |
| 57 | const fullscreenPauseButton = $<HTMLButtonElement>("fullscreen-pause"); | |
| 54 | 58 | |
| 55 | 59 | const renderer = createRenderer(scene); |
| 56 | 60 | if (renderer === null) { |
| @@ -106,7 +110,7 @@ function boot(): void { | ||
| 106 | 110 | }); |
| 107 | 111 | // The pause dialog sits over a live match: a key typed into one of its |
| 108 | 112 | // fields must never reach the window-level handler that drives the game. |
| 109 | for (const field of [pauseSensField, volumeField, muteButton]) | |
| 113 | for (const field of [pauseSensField, volumeField, muteButton, fullscreenPauseButton]) | |
| 110 | 114 | field.addEventListener("keydown", (event) => event.stopPropagation()); |
| 111 | 115 | |
| 112 | 116 | let world: World | null = null; |
| @@ -195,6 +199,9 @@ function boot(): void { | ||
| 195 | 199 | $("resume").focus(); |
| 196 | 200 | } |
| 197 | 201 | }; |
| 202 | // Leaving fullscreen — the system gesture or Esc — gets the same pause a | |
| 203 | // lost pointer lock already gets: a match is never left running unseen. | |
| 204 | attachFullscreen(wrapper, [fullscreenMenuButton, fullscreenPauseButton], () => controls.onPause()); | |
| 198 | 205 | |
| 199 | 206 | // --- results, sent as the match goes rather than only at the end |
| 200 | 207 | async function postResult(final: boolean): Promise<void> { |
src/pages/game.test.ts added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { openDatabase } from "../db.ts"; | |
| 6 | import { gamePage } from "./game.ts"; | |
| 7 | ||
| 8 | const db = openDatabase(mkdtempSync(join(tmpdir(), "game-page-"))); | |
| 9 | const options = { env: "development", pull: undefined, commit: "", script: "/static/app.js", ssh: undefined }; | |
| 10 | ||
| 11 | /** A `<div id="X" ...>`'s own body, whatever else is on its tag, to its matching `</div>`. */ | |
| 12 | function element(html: string, id: string): string { | |
| 13 | const marker = html.indexOf(`id="${id}"`); | |
| 14 | expect(marker).toBeGreaterThan(-1); | |
| 15 | const start = html.lastIndexOf("<div", marker); | |
| 16 | expect(start).toBeGreaterThan(-1); | |
| 17 | let depth = 0; | |
| 18 | let i = start; | |
| 19 | while (i < html.length) { | |
| 20 | if (html.startsWith("<div", i)) depth += 1; | |
| 21 | else if (html.startsWith("</div>", i)) { | |
| 22 | depth -= 1; | |
| 23 | if (depth === 0) return html.slice(start, i + "</div>".length); | |
| 24 | } | |
| 25 | i += 1; | |
| 26 | } | |
| 27 | throw new Error(`unclosed #${id}`); | |
| 28 | } | |
| 29 | ||
| 30 | test("the wrapper — not the stage alone — holds both the view and the pad", () => { | |
| 31 | const html = gamePage(db, options); | |
| 32 | const wrapper = element(html, "wrapper"); | |
| 33 | expect(wrapper).toContain(`<div id="stage">`); | |
| 34 | expect(wrapper).toContain(`<div id="touch" hidden>`); | |
| 35 | // Nesting, not just presence: a stage or a pad that escaped the wrapper | |
| 36 | // would still match `toContain` but would not go fullscreen together. | |
| 37 | expect(wrapper.indexOf(`<div id="stage">`)).toBeLessThan(wrapper.indexOf(`<div id="touch" hidden>`)); | |
| 38 | }); | |
| 39 | ||
| 40 | test("a fullscreen control sits in the menu and in the pause overlay", () => { | |
| 41 | const html = gamePage(db, options); | |
| 42 | const menu = element(html, "menu"); | |
| 43 | const pause = element(html, "pause"); | |
| 44 | expect(menu).toContain(`id="fullscreen-menu"`); | |
| 45 | expect(menu).toContain(`aria-pressed="false"`); | |
| 46 | expect(pause).toContain(`id="fullscreen-pause"`); | |
| 47 | expect(pause).toContain(`aria-pressed="false"`); | |
| 48 | }); |
src/pages/game.ts +17 −1
| @@ -61,6 +61,18 @@ h1 { font-size:1.4rem; margin:0; letter-spacing:.02em; } | ||
| 61 | 61 | small.commit { color:var(--dim); font-size:.75rem; } |
| 62 | 62 | { position:relative; width:100%; aspect-ratio:16/9; max-height:78vh; margin:1rem auto; background:00; |
| 63 | 63 | border:1px solid var(--line); border-radius:.6rem; overflow:hidden; touch-action:none; } |
| 64 | /* Fullscreen goes on #wrapper, not #stage: the pad has to come along, or a | |
| 65 | phone in fullscreen has a view and no buttons. */ | |
| 66 | :fullscreen, :-webkit-full-screen { | |
| 67 | display:flex; flex-direction:column; height:100dvh; width:100vw; background:00; | |
| 68 | padding-bottom:env(safe-area-inset-bottom); | |
| 69 | } | |
| 70 | :fullscreen , :-webkit-full-screen { | |
| 71 | flex:1 1 auto; min-height:0; max-height:none; aspect-ratio:auto; margin:0; border:none; border-radius:0; | |
| 72 | } | |
| 73 | :fullscreen , :-webkit-full-screen { | |
| 74 | flex:0 0 auto; margin:0 auto; padding-bottom:env(safe-area-inset-bottom); | |
| 75 | } | |
| 64 | 76 | , { position:absolute; inset:0; width:100%; height:100%; display:block; } |
| 65 | 77 | { pointer-events:none; } |
| 66 | 78 | .overlay { position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center; |
| @@ -142,6 +154,7 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 142 | 154 | ${options.commit ? `<small class="commit">@ ${escape(options.commit)}</small>` : ""} |
| 143 | 155 | </header> |
| 144 | 156 | <main> |
| 157 | <div id="wrapper"> | |
| 145 | 158 | <div id="stage"> |
| 146 | 159 | <canvas id="scene"></canvas> |
| 147 | 160 | <canvas id="hud"></canvas> |
| @@ -155,7 +168,8 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 155 | 168 | <label>Side<select id="side"><option value="ct">Counter-Terrorists</option><option value="t">Terrorists</option></select></label> |
| 156 | 169 | <label>Sensitivity<input id="sens" type="number" min="0.2" max="12" step="0.1" value="2.2"></label> |
| 157 | 170 | </div> |
| 158 | <div class="row"><button type="button" class="primary" id="play">Play</button></div> | |
| 171 | <div class="row"><button type="button" class="primary" id="play">Play</button> | |
| 172 | <button type="button" id="fullscreen-menu" aria-pressed="false" hidden>Fullscreen</button></div> | |
| 159 | 173 | <p class="hint"><kbd>WASD</kbd> move · <kbd>Space</kbd> jump · <kbd>Ctrl</kbd> crouch · <kbd>Shift</kbd> walk · |
| 160 | 174 | mouse to aim and fire · <kbd>R</kbd> reload · <kbd>E</kbd> plant, defuse or take a gun · <kbd>B</kbd> buy · |
| 161 | 175 | right click or <kbd>Q</kbd> to scope · <kbd>1</kbd><kbd>2</kbd><kbd>3</kbd> guns · |
| @@ -171,6 +185,7 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 171 | 185 | <label>Sensitivity<input id="pause-sens" type="number" min="0.2" max="12" step="0.1"></label> |
| 172 | 186 | <label>Volume<input id="volume" type="range" min="0" max="100" step="1"></label> |
| 173 | 187 | <button type="button" id="mute" aria-pressed="false">Mute</button> |
| 188 | <button type="button" id="fullscreen-pause" aria-pressed="false" hidden>Fullscreen</button> | |
| 174 | 189 | </div> |
| 175 | 190 | <div class="row"><button type="button" class="primary" id="resume">Resume</button><button type="button" id="quit">Leave match</button></div> |
| 176 | 191 | </div> |
| @@ -210,6 +225,7 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 210 | 225 | <p class="hint">Left of the view is the stick, the right aims — and a second |
| 211 | 226 | finger anywhere on the right fires without moving your aim.</p> |
| 212 | 227 | </div> |
| 228 | </div> | |
| 213 | 229 | |
| 214 | 230 | <section id="stats">${statsHtml(db)}</section> |
| 215 | 231 |
new above
offline