Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
i need actual full screen for games like age of empires. right now it doesn't… #235
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/234-i-need-actual-full-screen-for-games-like → main 5 commits fork 2 threads · 2 unresolved
5 files
changed +58
−5
empire/index.html +18 −4
| @@ -50,6 +50,13 @@ | ||
| 50 | 50 | #ctl button[aria-pressed="true"] { color: var(); border-color: var(); } |
| 51 | 51 | #status { color: var(); min-height: 1.35em; } |
| 52 | 52 | @media (max-width: 600px) { #bar { gap: 2px 10px; font-size: 12px; } #cmds, #ctl { gap: 4px; } button { padding: 4px 7px; } #app { padding-bottom: max(76px, calc(env(safe-area-inset-bottom) + 62px)); } } |
| 53 | /* full screen: the map takes everything the bar and the dock leave */ | |
| 54 | #app:fullscreen { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); gap: 2px; background: var(); } | |
| 55 | #app:fullscreen #bar, #app:fullscreen #stage, #app:fullscreen #dock { max-width: none; } | |
| 56 | /* the prefixed rules stay apart: one selector list with an unknown pseudo-class is dropped whole */ | |
| 57 | #app:-webkit-full-screen { padding: 0; gap: 2px; background: var(); } | |
| 58 | #app:-webkit-full-screen #bar, #app:-webkit-full-screen #stage, #app:-webkit-full-screen #dock { max-width: none; } | |
| 59 | #app:fullscreen #dock { height: max(130px, min(30vh, 200px)); } | |
| 53 | 60 | </style> |
| 54 | 61 | </head> |
| 55 | 62 | <body> |
| @@ -109,12 +116,12 @@ PixelPlays.mount({ | ||
| 109 | 116 | <script> |
| 110 | 117 | 'use strict'; |
| 111 | 118 | |
| 112 | const W = 360, H = 280; | |
| 119 | let W = 360, H = 280; // the view in pixels: this in a page, the whole screen in full screen | |
| 113 | 120 | const canvas = document.getElementById('c'); |
| 114 | 121 | canvas.width = W; canvas.height = H; |
| 115 | 122 | const ctx = canvas.getContext('2d'); |
| 116 | const image = ctx.createImageData(W, H); | |
| 117 | const pix = new Uint32Array(image.data.buffer); | |
| 123 | let image = ctx.createImageData(W, H); | |
| 124 | let pix = new Uint32Array(image.data.buffer); | |
| 118 | 125 | const $ = id => document.getElementById(id); |
| 119 | 126 | // write text only when it differs, so a screen reader is not told the same thing again |
| 120 | 127 | const shown = new Map(); |
| @@ -476,8 +483,16 @@ function crosshair() { | ||
| 476 | 483 | } |
| 477 | 484 | |
| 478 | 485 | /* ---------- fit and boot ---------- */ |
| 486 | const fsOn = () => document.fullscreenElement || document.webkitFullscreenElement; | |
| 479 | 487 | function fit() { |
| 480 | 488 | const stage = $('stage'), w = stage.clientWidth || W, h = stage.clientHeight || H; |
| 489 | const { W: nw, H: nh } = Paint.viewSize(w, h, !!fsOn()); | |
| 490 | if (nw !== W || nh !== H) { | |
| 491 | const ow = W, oh = H; | |
| 492 | W = canvas.width = nw; H = canvas.height = nh; | |
| 493 | image = ctx.createImageData(W, H); pix = new Uint32Array(image.data.buffer); | |
| 494 | Paint.anchorMini(W, H); Paint.recentre(V, ow, oh, W, H); | |
| 495 | } | |
| 481 | 496 | let k = Math.min(w / W, h / H); |
| 482 | 497 | if (k >= 2) k = Math.floor(k); |
| 483 | 498 | k = Math.max(.5, k); |
| @@ -486,7 +501,6 @@ function fit() { | ||
| 486 | 501 | } |
| 487 | 502 | addEventListener('resize', fit); |
| 488 | 503 | // full screen: the app fills the screen, the canvas refits when it changes |
| 489 | const fsOn = () => document.fullscreenElement || document.webkitFullscreenElement; | |
| 490 | 504 | const root = $('app'); |
| 491 | 505 | if (root.requestFullscreen || root.webkitRequestFullscreen) { |
| 492 | 506 | $('full').hidden = false; |
empire/paint.js +12 −1
| @@ -43,6 +43,17 @@ | ||
| 43 | 43 | }; |
| 44 | 44 | // put the camera so a tile point is in the middle of the view |
| 45 | 45 | function centreOn(v, x, y, W, H) { v.camX = (x - y) * TW - W / 2; v.camY = (x + y) * TH - H / 2; clampCam(v, W, H); } |
| 46 | // the minimap hugs the bottom right corner of a view of any size | |
| 47 | const anchorMini = (W, H) => { MINI.x = W - MINI.w - 6; MINI.y = H - MINI.h - 4; }; | |
| 48 | // the view in pixels for a stage of w x h CSS pixels: 360x280 in a page; in full screen whole | |
| 49 | // pixels drawn k times over, k the smallest that keeps the view to about 640 wide, never so big that it drops under 240x180 | |
| 50 | function viewSize(w, h, full) { | |
| 51 | if (!full) return { W: 360, H: 280 }; | |
| 52 | const k = Math.max(1, Math.min(Math.ceil(w / 640), Math.floor(w / 240), Math.floor(h / 180))); | |
| 53 | return { W: Math.max(240, Math.floor(w / k)), H: Math.max(180, Math.floor(h / k)) }; | |
| 54 | } | |
| 55 | // keep the same point in the middle when the view changes size | |
| 56 | function recentre(v, oldW, oldH, W, H) { v.camX += (oldW - W) / 2; v.camY += (oldH - H) / 2; clampCam(v, W, H); } | |
| 46 | 57 | const miniAt = (sx, sy) => sx >= MINI.x && sy >= MINI.y && sx < MINI.x + MINI.w && sy < MINI.y + MINI.h; |
| 47 | 58 | function make(pix, W, H) { |
| 48 | 59 | const px = (x, y, c) => { if (x >= 0 && y >= 0 && x < W && y < H) pix[y * W + x] = c; }; |
| @@ -263,6 +274,6 @@ | ||
| 263 | 274 | return [Math.max(0, Math.min(SIZE - .01, (a + b) / 2)), Math.max(0, Math.min(SIZE - .01, (b - a) / 2))]; |
| 264 | 275 | } |
| 265 | 276 | |
| 266 | const api = { TW, TH, MINI, TEAM, rgb, proj, toWorld, clampCam, centreOn, miniAt, miniWorld, draw, HEIGHT }; | |
| 277 | const api = { TW, TH, MINI, TEAM, rgb, proj, toWorld, clampCam, centreOn, anchorMini, viewSize, recentre, miniAt, miniWorld, draw, HEIGHT }; | |
| 267 | 278 | if (typeof module !== 'undefined') module.exports = api; else root.Paint = api; |
| 268 | 279 | })(typeof window !== 'undefined' ? window : globalThis); |
empire/ui.test.mjs +28
| @@ -220,3 +220,31 @@ const html = readPage(new URL('./index.html', import.meta.url), 'utf8'); | ||
| 220 | 220 | expect(html).toContain('requestFullscreen'); |
| 221 | 221 | }); |
| 222 | 222 | } |
| 223 | ||
| 224 | test('full screen widens the view and the minimap follows the corner', () => { | |
| 225 | P.anchorMini(640, 400); | |
| 226 | expect(P.MINI.x + P.MINI.w).toBeLessThanOrEqual(640); | |
outdated
Trailing whitespace on a blank line. More importantly, the two |
||
| 227 | expect(P.MINI.y + P.MINI.h).toBeLessThanOrEqual(400); | |
| 228 | expect(P.miniAt(P.MINI.x + 1, P.MINI.y + 1)).toBe(true); | |
| 229 | P.anchorMini(360, 280); | |
| 230 | expect(P.MINI).toMatchObject({ x: 266, y: 232 }); | |
| 231 | }); | |
| 232 | ||
| 233 | test('the view is 360x280 windowed and about 640 wide on a big screen, in whole-pixel steps', () => { | |
| 234 | expect(P.viewSize(1920, 1080, false)).toEqual({ W: 360, H: 280 }); | |
| 235 | expect(P.viewSize(1920, 1080, true)).toEqual({ W: 640, H: 360 }); | |
| 236 | expect(P.viewSize(1280, 720, true)).toEqual({ W: 640, H: 360 }); | |
| 237 | expect(P.viewSize(360, 500, true)).toEqual({ W: 360, H: 500 }); // a phone upright: 1:1, not scaled down | |
| 238 | expect(P.viewSize(780, 250, true)).toEqual({ W: 780, H: 250 }); // a phone sideways keeps 1:1 too | |
| 239 | expect(P.viewSize(200, 100, true)).toEqual({ W: 240, H: 180 }); // the floor | |
| 240 | }); | |
| 241 | ||
| 242 | test('the camera stays centred across a resize', () => { | |
| 243 | const v = { camX: 0, camY: 0 }; | |
| 244 | P.centreOn(v, 20, 20, 360, 280); | |
| 245 | const mid = [v.camX + 180, v.camY + 140]; | |
| 246 | P.recentre(v, 360, 280, 640, 360); | |
| 247 | expect([v.camX + 320, v.camY + 180]).toEqual(mid); | |
| 248 | P.recentre(v, 640, 360, 360, 280); | |
| 249 | expect([v.camX + 180, v.camY + 140]).toEqual(mid); | |
| 250 | }); |
og/empire.png
Binary file not shown.
og/home.png
Binary file not shown.
new above
offline
This branch is the heart of the change and nothing exercises it. Extract the size choice as a pure function and test it for desktop, phone and windowed sizes. Also check the 240x180 floor: on a 360px-wide screen the view is wider than the stage, so the scale falls below 1 and pixel-art edges blur.