Files
kuentagym/frontend/scripts/build-app-icons.mjs
T
dsantillan 9e580c4794
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
Cards you can see through, a splash that waits, and iOS launch screens
**The transparency was there and invisible.** `--glass` was a near-black at 55% alpha, and a
dark film over a dark background reads as solid no matter what the alpha says — so the cards
looked exactly as opaque as before. It is 34% now, the family tint weighs a little more
(14%) and the blur is 10px instead of 16: heavy blur flattens the backdrop into one colour,
which hides the very thing that is supposed to show through.

The opaque `@supports` fallback is gone as well. It was copied from the tab bar, where it is
right — the bar floats over scrolling text — and it is wrong here: a card sits in the page
flow and the only thing behind it is the aura. On any device that took that branch, the
cards were solid by construction.

**The splash now waits, which is what a launch screen does.** It stays until the boot is
finished, with a floor of 1.1s so it can actually be read (a warm boot takes milliseconds,
and a splash that lasts that long is a flicker) and a ceiling of 6s so a request that never
answers cannot hold anybody behind a photograph — their workouts are already local. The mark
pulses slowly while it waits, so it looks like a screen that is loading rather than a
photograph that is stuck.

**The long white is iOS, before any of our code runs.** The deployed HTML already carries
the aura's base colour inline, so what is left is the installed web app's own launch screen:
iOS paints it before the page exists, and without an `apple-touch-startup-image` that
matches the device it paints nothing. Fourteen entries now cover the phone and tablet sizes
the app is opened on, cropped from the same photograph (each device fetches only its own).
build-app-icons.mjs fails when the sizes it generates and the sizes index.html links
disagree, because a missing entry is the blank screen this exists to remove.
2026-09-23 11:47:52 -04:00

170 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* build-app-icons.mjs — derive every icon the app ships from one source image.
*
* The source is assets/logoym.png (a PNG despite its old .jpeg name; it has a real alpha channel,
* with the emblem's corners transparent). Everything else here is generated, so the app icon, the
* iOS home-screen icon, the in-app brand mark and the Capacitor source icon can never drift apart
* from each other or from the file the owner actually dropped in assets/.
*
* Run it after replacing the source:
*
* cd frontend && node scripts/build-app-icons.mjs
*
* `sharp` comes with @capacitor/assets (a devDependency here), which is the only reason it is
* available without adding a dependency of our own — see CONTRIBUTING.md on that.
*
* THE ONE DESIGN DECISION: the emblem is dark green (its most common colour is #031808, its light
* accents are greens too), and its corners are transparent. So a transparent icon would disappear
* on a dark home screen, and a white one would put a white card behind an already-square logo. The
* canvases below are filled with the emblem's OWN darkest green and the logo is centred at 88% of
* the square: the corners it does not cover blend into it, the maskable safe circle stays clean,
* and the icon reads as one piece at 180 px. Change ICON_BG to re-skin all of them at once.
*/
import sharp from 'sharp'
import { existsSync, mkdirSync, readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
// sharp takes paths, not file: URLs — hence fileURLToPath here and in icon() below.
const SRC = fileURLToPath(new URL('../../assets/logoym.png', import.meta.url))
/** The launch photo. 3862×5786 and 12 MB straight off a camera — every version the app ships is
* derived here, because none of them can be that file. */
const SPLASH_SRC = fileURLToPath(new URL('../../assets/splash.jpg', import.meta.url))
// The photograph itself is NOT committed: it is 12 MB straight off a camera, and the two versions
// the app uses are a fraction of that (see the bottom of this file). Keep the original next to the
// repo, or drop a replacement at the same path, and this regenerates from it.
const splashMissing = !existsSync(SPLASH_SRC)
const PUB = new URL('../public/', import.meta.url)
const RES = new URL('../resources/', import.meta.url)
/** The emblem's own dominant colour (sampled from the source), used to fill the icon squares. */
const ICON_BG = '#031808'
/** Behind the native splash, the aura's own base colour (index.css) so the two never disagree. */
const SPLASH_BG = '#100e0b'
/** Fraction of the canvas the emblem occupies. The maskable safe circle is 80% of the square, and
* the emblem's own corners are transparent, so slightly past it costs nothing and gains size. */
const ICON_FIT = 0.88
/** Longest side of the in-app mark. Displayed at 52 px (tab bar) and ~88 px (login), so 256 covers
* a 3× screen twice over; anything larger is bytes nobody looks at. */
const MARK_PX = 256
/* Every PNG here is written as a 256-colour palette. Measured against the full RGBA version the
* mean per-channel error is 1.5/255 — invisible — for a file about 2.5× smaller: the emblem is
* greens on greens, which is exactly what a palette is good at. The transparency survives as the
* palette's own alpha entries (checked: the corners really are alpha 0.) */
const PNG = { palette: true, colours: 256, compressionLevel: 9, effort: 10 }
const logo = sharp(SRC).ensureAlpha().trim()
/** One opaque square with the emblem centred in it. */
async function icon(size, out) {
const inner = Math.round(size * ICON_FIT)
const placed = await logo.clone().resize(inner, inner, { fit: 'inside' }).png().toBuffer()
await sharp({ create: { width: size, height: size, channels: 4, background: ICON_BG } })
.composite([{ input: placed, gravity: 'centre' }])
.png(PNG)
.toFile(fileURLToPath(out))
console.log(` ${out.pathname.split('/').slice(-2).join('/')} ${size}x${size} bg ${ICON_BG}`)
}
console.log('generating app icons from assets/logoym.png')
// The two the web app ships: icon-512 is declared "any maskable" in manifest.json (hence the
// opaque background and the centred emblem), icon-180 is the iOS home screen and the favicon.
await icon(512, new URL('icon-512.png', PUB))
await icon(180, new URL('icon-180.png', PUB))
// The in-app brand mark: transparent, so it sits on the page's own background. Used by the tab
// bar's Start button, the login screen and the boot placeholder.
await logo.clone().resize(MARK_PX, MARK_PX, { fit: 'inside' }).png(PNG)
.toFile(fileURLToPath(new URL('logo.png', PUB)))
const mark = readFileSync(fileURLToPath(new URL('logo.png', PUB)))
console.log(` public/logo.png transparent ${(mark.length / 1024).toFixed(1)} KB`)
// The Capacitor source icon for the native shells (docs/MOBILE.md: `npx @capacitor/assets
// generate` reads resources/). Kept opaque and 1024 wide, which is what that tool wants.
await icon(1024, new URL('icon.png', RES))
/* ---------------------------------------------------------------- the launch screen --
*
* assets/splash.jpg is a photograph — 3862×5786, 12 MB — so nothing ships it as it is. Two
* versions come out of it:
*
* public/splash.webp what the web app shows while the profile loads (`.boot-splash` in
* index.css). Phone-width and WebP, because it is a photograph: the same
* frame as a JPEG at matching quality is several times the bytes.
* resources/splash.png the @capacitor/assets source for the native shells. That tool wants a
* square, so the photo is fitted inside 2732×2732 on the dark base colour
* rather than cropped — a portrait photograph cropped to a square loses
* the composition somebody chose.
*/
const SPLASH_W = 1170 // covers a 390pt phone at 3× without shipping a poster
if (!splashMissing) {
await sharp(SPLASH_SRC)
.resize(SPLASH_W, null, { fit: 'inside', withoutEnlargement: true })
.webp({ quality: 78, effort: 5 })
.toFile(fileURLToPath(new URL('splash.webp', PUB)))
const splashOut = readFileSync(fileURLToPath(new URL('splash.webp', PUB)))
console.log(` public/splash.webp launch screen ${(splashOut.length / 1024).toFixed(0)} KB`)
}
if (splashMissing) {
console.log(' assets/splash.jpg is not here — skipping the launch screen (it is not committed)')
} else {
const splashInner = await sharp(SPLASH_SRC).resize(2732, 2732, { fit: 'inside' }).toBuffer()
// JPEG, not PNG: this is a photograph, and as a PNG it came out at 10.6 MB for no visible gain.
await sharp({ create: { width: 2732, height: 2732, channels: 3, background: SPLASH_BG } })
.composite([{ input: splashInner, gravity: 'centre' }])
.jpeg({ quality: 86, chromaSubsampling: '4:4:4' })
.toFile(fileURLToPath(new URL('splash.jpg', RES)))
const nativeOut = readFileSync(fileURLToPath(new URL('splash.jpg', RES)))
console.log(` resources/splash.jpg native source 2732x2732 bg ${SPLASH_BG} ${(nativeOut.length / 1024 / 1024).toFixed(1)} MB`)
/* --------------------------------------------------- iOS launch screens --
*
* An installed iOS web app paints its OWN screen before any of this code runs, and without these it
* is a blank — the white flash on every open that no amount of CSS can touch. iOS picks the entry
* whose media query matches the device, so each one is a differently-cropped copy of the same
* photograph at that device's exact pixel size; a device that finds no match gets the blank back.
*
* Only one file is ever fetched per device, but all of them live in the repository, so this list is
* kept to the screens the app is realistically opened on. Add a line and re-run to cover another.
* The CSS pixels and the device-pixel-ratio here are exactly what the <link> in index.html states.
*/
const IOS_SIZES = [
[375, 667, 2], [414, 736, 3], [375, 812, 3], [414, 896, 2], [414, 896, 3],
[390, 844, 3], [393, 852, 3], [402, 874, 3], [428, 926, 3], [430, 932, 3], [440, 956, 3],
[768, 1024, 2], [834, 1194, 2], [1024, 1366, 2]
]
const STARTUP = new URL('../public/startup/', import.meta.url)
if (!splashMissing) {
mkdirSync(fileURLToPath(STARTUP), { recursive: true })
let bytes = 0
for (const [w, h, dpr] of IOS_SIZES) {
const px = { w: w * dpr, h: h * dpr }
const out = new URL(`${px.w}x${px.h}@${dpr}.jpg`, STARTUP)
// `cover` rather than `contain`: a launch screen fills the screen, and the composition is
// centred, so a crop keeps the middle of the frame and loses only sky and floor.
await sharp(SPLASH_SRC).resize(px.w, px.h, { fit: 'cover', position: 'centre' })
.jpeg({ quality: 72, chromaSubsampling: '4:2:0' })
.toFile(fileURLToPath(out))
bytes += readFileSync(fileURLToPath(out)).length
}
console.log(` public/startup/ iOS launch screens ${IOS_SIZES.length} sizes ${(bytes / 1024).toFixed(0)} KB total`)
// The <link> tags in index.html have to name the same sizes as the table above, and two lists in
// two files is exactly the kind of thing that drifts silently: iOS matches on the media query, so
// a size that is generated but not linked is fetched by nobody and a linked size that is not
// generated is a 404 — and iOS answers a missing launch image with the blank screen this whole
// section exists to remove. Cheap to check, so it is checked.
const head = readFileSync(fileURLToPath(new URL('../index.html', import.meta.url)), 'utf8')
const linked = [...head.matchAll(/\(device-width: (\d+)px\) and \(device-height: (\d+)px\) and \(-webkit-device-pixel-ratio: (\d+)\)/g)]
.map(m => [Number(m[1]), Number(m[2]), Number(m[3])])
const key = a => a.join('x')
const missing = IOS_SIZES.filter(s => !linked.some(l => key(l) === key(s)))
const extra = linked.filter(l => !IOS_SIZES.some(s => key(s) === key(l)))
if (missing.length || extra.length) {
console.error(` index.html and IOS_SIZES disagree — generated but not linked: ${missing.map(key).join(', ') || 'none'}; linked but not generated: ${extra.map(key).join(', ') || 'none'}`)
process.exitCode = 1
}
}
}