@a2f0/skyline 0.2.74 → 0.2.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/package.md CHANGED
@@ -122,7 +122,8 @@ files already there. Use a dedicated directory for these assets, and clear it be
122
122
  copying an upgrade, so files a new version dropped do not linger. All the viewer's
123
123
  code, CSS, markup, SVGs and models, and its vendored Three.js, travel together. Copy
124
124
  them again whenever you upgrade the package, so the loader you bundle and the viewer
125
- you serve match.
125
+ you serve match: a loader newer than its viewer, for one, shows two loading silhouettes until
126
+ the first frame.
126
127
 
127
128
  In `~/github/devopsrockstars`, `packages/frontend/tooling/build.ts` calls the helper
128
129
  when the `skyline3d` flag is on and serves the copy at `/static/skyline/`, the browser
@@ -255,8 +256,9 @@ the option out.
255
256
 
256
257
  ### How the viewer's code loads
257
258
 
258
- The package's module (`lib/`) is a small loader that a host bundles (under 4.5 KB). When
259
- it mounts, it imports the viewer's code from the assets (`skyline-viewer.js`) with a
259
+ The package's module (`lib/`) is a small loader that a host bundles (about 8 KB, 3.5 KB
260
+ compressed), most of it the loading silhouette, which it paints the moment it mounts, before
261
+ any asset arrives. When it mounts, it imports the viewer's code from the assets (`skyline-viewer.js`) with a
260
262
  dynamic `import()` that carries `webpackIgnore`, `turbopackIgnore` and `@vite-ignore`
261
263
  comments, so webpack (including Next.js with `--webpack`), Turbopack and Vite leave it
262
264
  to the browser; Rollup, esbuild and Bun leave an import of a variable alone anyway. A
@@ -269,12 +271,65 @@ arrive together rather than one import at a time, the loader also reads
269
271
  to the document's head, leaving out the vendored engine when the host shares its own;
270
272
  the links leave the head once the viewer is ready, fails, or is destroyed.
271
273
 
274
+ ### Start loading with the page
275
+
276
+ A host that mounts the viewer from its own code can start the viewer's downloads, and show its
277
+ loading silhouette, before that code runs. Both helpers come from the Node/Bun build entrypoint,
278
+ for a build step that writes the host's HTML:
279
+
280
+ ```ts
281
+ import { skylineBootMarkup, skylinePreloadLinks } from '@a2f0/skyline/build';
282
+
283
+ // In the <head> of each page that mounts the viewer as it loads:
284
+ const head = await skylinePreloadLinks('/static/skyline/', { three: true });
285
+ // In the <body>, in an element placed and sized exactly like the viewer's container:
286
+ const placeholder = `<div id="skyline-placeholder" style="position:fixed;inset:0">${skylineBootMarkup({ theme: 'dark' })}</div>`;
287
+ ```
288
+
289
+ - `skylinePreloads(assetsUrl, { three })` lists every file the viewer loads to draw its first
290
+ frame, and its stars, as `{ rel, href, as?, crossorigin?, fetchpriority? }` link attributes;
291
+ `skylinePreloadLinks` renders them as `<link>` elements. Pass the URL the viewer mounts with,
292
+ and `three: true` when the page passes `mountSkyline` its own Three.js, which leaves out the
293
+ engine copy in the assets. Each link matches how the viewer requests its file (module
294
+ preloads for its modules, a `style` preload for its stylesheets, anonymous-CORS `fetch`
295
+ preloads for the markup, module list and stars), so the browser hands the viewer the
296
+ preloaded response instead of fetching it again. Put them only on pages that mount a viewer
297
+ within a few seconds of loading: the browser warns about preloads it never used.
298
+ - `skylineBootMarkup({ theme })` returns the loading silhouette as static HTML: an element that
299
+ fills its parent, with the silhouette, its styles and its animation in a declarative shadow
300
+ root, so it paints and moves before any script runs. Pass the same `theme` as to
301
+ `mountSkyline`. It matches the default folded control bar.
302
+
303
+ Then mount over it, passing the placeholder:
304
+
305
+ ```ts
306
+ mountSkyline(container, {
307
+ assetsUrl: '/static/skyline/',
308
+ three: import('@a2f0/skyline/three'),
309
+ placeholder: document.querySelector('#skyline-placeholder'),
310
+ });
311
+ ```
312
+
313
+ `placeholder` takes the element `skylineBootMarkup` returned, or one holding it; one that holds
314
+ the container is refused with a `TypeError`. The viewer paints its own silhouette in the same
315
+ place, starts its animation where the placeholder's had reached, and removes the placeholder,
316
+ all in the same task, so nothing visibly changes. With
317
+ `controls: "open"` it just removes the placeholder: an open bar's height depends on the
318
+ viewer's layout, so the scene's own outline shows once its markup arrives. A browser without
319
+ declarative shadow roots shows the placeholder's backdrop alone until the viewer mounts.
320
+
321
+ The silhouette's animations change only transform and opacity, which browsers run off the main
322
+ thread, so it keeps moving while the scene builds its first frame in one long task.
323
+
272
324
  ### Content security policy and other origins
273
325
 
274
326
  The viewer runs in the host's page, under the host's content security policy. A host
275
327
  with one must allow the assets' origin (usually `'self'`) in `script-src`,
276
328
  `style-src` and `connect-src` (the viewer fetches its markup, stars and module list), and inline
277
- styles in `style-src`, which the viewer sets on its elements. Serve the assets from
329
+ styles in `style-src` (`'unsafe-inline'`): the viewer sets them on its elements, and its loading
330
+ silhouette brings its own `<style>` element. The markup from `skylineBootMarkup` needs the same
331
+ in the host's HTML, for its `style` attribute and the `<style>` in its template; under a policy
332
+ that allows only nonced styles, the silhouette shows unstyled. Serve the assets from
278
333
  the host's origin when you can; on another origin, every file must allow the page's
279
334
  origin through CORS. The viewer's code runs with the host page's privileges wherever
280
335
  it is served from: serve it only from an origin you trust.
@@ -282,7 +337,9 @@ it is served from: serve it only from an origin you trust.
282
337
  ### For tests
283
338
 
284
339
  `element.shadowRoot` holds the viewer: its 3D canvas is `#building` and the scene's
285
- loading indicator `#loading`, hidden at the first frame. The region drops `aria-busy`
340
+ loading indicator `#loading`, hidden at the first frame. With the bar folded, the visible
341
+ silhouette is the shell's `.skyline-boot`, there from the mount until the first frame; the
342
+ markup's own `.skyline-trace` inside `#loading` stays hidden. The region drops `aria-busy`
286
343
  when `ready` settles. WebDriver and Playwright reach into open shadow roots; code run
287
344
  in the page reads `element.shadowRoot.querySelector(...)`.
288
345
 
@@ -3,3 +3,49 @@
3
3
  * directory. This module is for Node/Bun build scripts, never browser entrypoints.
4
4
  */
5
5
  export declare function copySkylineAssets(destination: string | URL): Promise<void>;
6
+ /** A `<link>` that starts one of the viewer's loads with the page, rather than once it mounts. */
7
+ export interface SkylinePreload {
8
+ /** `modulepreload` for the viewer's modules, `preload` for its stylesheets and fetched files. */
9
+ readonly rel: "modulepreload" | "preload";
10
+ readonly href: string;
11
+ /** A `preload`'s destination: `style` for a stylesheet, `fetch` for what the viewer fetches. */
12
+ readonly as?: "style" | "fetch";
13
+ /**
14
+ * Set on fetched files: the viewer fetches them in CORS mode, with same-origin credentials,
15
+ * which only an anonymous-CORS preload matches.
16
+ */
17
+ readonly crossorigin?: "anonymous";
18
+ /** Set on the stars, which show only after the first frame. */
19
+ readonly fetchpriority?: "low";
20
+ }
21
+ export interface SkylinePreloadOptions {
22
+ /**
23
+ * Whether the page passes `mountSkyline` its own Three.js (the `three` option). Without it,
24
+ * the list includes the engine copy in the assets, which the viewer then loads.
25
+ */
26
+ readonly three?: boolean;
27
+ }
28
+ /**
29
+ * Every file a viewer mounted with these assets loads to draw its first frame, and its stars, as
30
+ * links a host can put in the head of a page that mounts it, so they download alongside the
31
+ * host's own code instead of after it. `assetsUrl` is the browser URL the viewer mounts with,
32
+ * such as "/static/skyline/". Each link matches how the viewer requests its file, so the browser
33
+ * reuses the preloaded response rather than fetching it again. Put the links only on pages that
34
+ * mount a viewer within a few seconds of loading: the browser warns about preloads it never used.
35
+ */
36
+ export declare function skylinePreloads(assetsUrl: string | URL, { three }?: SkylinePreloadOptions): Promise<SkylinePreload[]>;
37
+ /** `skylinePreloads` as `<link>` elements, one per line, for a page's head. */
38
+ export declare function skylinePreloadLinks(assetsUrl: string | URL, options?: SkylinePreloadOptions): Promise<string>;
39
+ export interface SkylineBootOptions {
40
+ /** The loading palette, as `mountSkyline`'s `theme`: dark by default, as for a greyscale viewer. */
41
+ readonly theme?: "light" | "dark";
42
+ }
43
+ /**
44
+ * The viewer's loading silhouette as static HTML, for a host to paint before its code runs:
45
+ * an element that fills its parent, holding the silhouette in a declarative shadow root, styled
46
+ * and animated without script. Place it in an element positioned and sized exactly like the
47
+ * container the host mounts the viewer in, then pass that element to `mountSkyline` as
48
+ * `placeholder`, which continues the animation in step and removes it. Matches the default
49
+ * folded control bar. A browser without declarative shadow roots shows the backdrop alone.
50
+ */
51
+ export declare function skylineBootMarkup({ theme }?: SkylineBootOptions): string;
@@ -1,4 +1,6 @@
1
- import { cp, mkdir } from "node:fs/promises";
1
+ import { cp, mkdir, readFile } from "node:fs/promises";
2
+ import { bootHostStyle, bootHtml, bootTree, escapeHtml, PLACEHOLDER_ATTRIBUTE } from "./skyline-boot.js";
3
+ import { assetsBase } from "./skyline-shell.js";
2
4
  /**
3
5
  * Copies the viewer's complete asset directory into a host application's static
4
6
  * directory. This module is for Node/Bun build scripts, never browser entrypoints.
@@ -7,3 +9,51 @@ export async function copySkylineAssets(destination) {
7
9
  await mkdir(destination, { recursive: true });
8
10
  await cp(new URL("../site/", import.meta.url), destination, { recursive: true });
9
11
  }
12
+ /**
13
+ * Every file a viewer mounted with these assets loads to draw its first frame, and its stars, as
14
+ * links a host can put in the head of a page that mounts it, so they download alongside the
15
+ * host's own code instead of after it. `assetsUrl` is the browser URL the viewer mounts with,
16
+ * such as "/static/skyline/". Each link matches how the viewer requests its file, so the browser
17
+ * reuses the preloaded response rather than fetching it again. Put the links only on pages that
18
+ * mount a viewer within a few seconds of loading: the browser warns about preloads it never used.
19
+ */
20
+ export async function skylinePreloads(assetsUrl, { three = false } = {}) {
21
+ // Checked by the shell's own rule, which `mountSkyline` applies, against a stand-in page address.
22
+ assetsBase({ baseURI: "https://host.invalid/" }, assetsUrl);
23
+ const text = String(assetsUrl);
24
+ const base = text.endsWith("/") ? text : `${text}/`;
25
+ const { modules, engine } = JSON.parse(await readFile(new URL("../site/viewer-modules.json", import.meta.url), "utf8"));
26
+ const module = (file) => ({ rel: "modulepreload", href: base + file });
27
+ const fetched = (file) => ({ rel: "preload", href: base + file, as: "fetch", crossorigin: "anonymous" });
28
+ return [
29
+ // The viewer's code, its stylesheets and the scene's markup head the chain the shell
30
+ // starts; the module list and the modules it names follow.
31
+ module("skyline-viewer.js"),
32
+ { rel: "preload", href: `${base}study.css`, as: "style" },
33
+ { rel: "preload", href: `${base}viewer.css`, as: "style" },
34
+ fetched("skyline-3d.html"),
35
+ fetched("viewer-modules.json"),
36
+ ...modules.map(module),
37
+ ...(three || !engine ? [] : [module(engine)]),
38
+ { ...fetched("stars.svg"), fetchpriority: "low" },
39
+ ];
40
+ }
41
+ /** `skylinePreloads` as `<link>` elements, one per line, for a page's head. */
42
+ export async function skylinePreloadLinks(assetsUrl, options = {}) {
43
+ return (await skylinePreloads(assetsUrl, options))
44
+ .map((link) => `<link${Object.entries(link).map(([name, value]) => ` ${name}="${escapeHtml(String(value))}"`).join("")}>`)
45
+ .join("\n");
46
+ }
47
+ /**
48
+ * The viewer's loading silhouette as static HTML, for a host to paint before its code runs:
49
+ * an element that fills its parent, holding the silhouette in a declarative shadow root, styled
50
+ * and animated without script. Place it in an element positioned and sized exactly like the
51
+ * container the host mounts the viewer in, then pass that element to `mountSkyline` as
52
+ * `placeholder`, which continues the animation in step and removes it. Matches the default
53
+ * folded control bar. A browser without declarative shadow roots shows the backdrop alone.
54
+ */
55
+ export function skylineBootMarkup({ theme = "dark" } = {}) {
56
+ if (theme !== "light" && theme !== "dark")
57
+ throw new TypeError('Skyline theme must be "light" or "dark".');
58
+ return `<div ${PLACEHOLDER_ATTRIBUTE} aria-hidden="true" style="${escapeHtml(bootHostStyle(theme))}"><template shadowrootmode="open">${bootHtml(bootTree(theme))}</template></div>`;
59
+ }
@@ -0,0 +1,5 @@
1
+ export declare const bootOutline: {
2
+ readonly width: 1000;
3
+ readonly height: 379.13;
4
+ readonly d: "M0 379.1h24v-61.4l12.3-6.3h38.4l2.5 1.3l.3-1h8.3l.3-4.2l44.1 .5l.2 1h.5l.2-1h29.3l.5 4.7h15.8l.2 7.2h7.7l.9 1.1h16.8l.2-1.3l.7-.4l.5 2.7l3 6.6l.2-3.1h1.4l.2 9.2h2.1l.3 4.1h17.4l.2-28.7h3.8l.2-9.9h3.7l.2-27h1.9l.2-7.8h1l.2-.8h3.2l.3 1.1h1.8l.3-1.1h1.8l.3 1.1l1.1-.3l.3-.8h2l.3 .8h.7l.4 7.8h1.7l.2 36.9l.5-2.6h2.1l.2 2.6h1.1l.3-2.6h2.5l.3 17.1h4.2l.2-12.4h1.6l.2-6.9h14l.2 3.5h10.9l.3-6.5h10.4l.3-78.7h10.9l.2-7.3h7.5l.2-2.3l17.7 .2l.2 9.4h2.8l.2 19.1h28.6l.2 129.7h4.5l.2-117.1l18.8-28.2l.7-.2l2.8 19h.5l.2-19h.5l26.9 29.2l.3 49h4.8l.3-107.7h67.1l.3 24.5h13.9l.2-146.9h19.3l.3-9h12.3l.9-50h1.2l.9 50h7l.2 15.1h.9l.3 68l6.3-9.1l.4-14.4l.5-1.1l.2-5.6l1.2 21.2l13.2 19.1l2.8 9.3l.2 3.4h1.7l.2 3.4h1.4l.2 3.4h1.4l.3 3.4h1.4l.2 145.5l.5 41.6h2.5l.2-281.8h13.3l.2-5.4h18.1l.3-13.9h.9l.2 13.9h18.2l.2 5.4h13l.3 109.8l.2-12.6h70.4l.5 12.6h6.5l.2 6.1h43.5l.4 8.2l.3 76.1h4.9l.2-10.6h11.6l.2-2.5h18.2l.2 2.5h10.2l.3 6.4h.4l.3 4.2h5.5l.3-28h37.9l.2 119.5h41.6l.2-81.3h1.9l.7-15h12.8l.7 15h4.1l.7-6.2h14.2l.7 6.2h1.6l.3 9.2h12.5l.7-7.5h11.9l.7 7.5h1.6v72.1h24";
5
+ };
@@ -0,0 +1,4 @@
1
+ // Generated by scripts/checks/skyline-loading.ts from the mapped buildings: the level skyline
2
+ // elevation the shell paints while a viewer loads (skyline-boot.ts). After changing a building,
3
+ // run that script; `bun run check` fails while this is stale.
4
+ export const bootOutline = { width: 1000, height: 379.13, d: "M0 379.1h24v-61.4l12.3-6.3h38.4l2.5 1.3l.3-1h8.3l.3-4.2l44.1 .5l.2 1h.5l.2-1h29.3l.5 4.7h15.8l.2 7.2h7.7l.9 1.1h16.8l.2-1.3l.7-.4l.5 2.7l3 6.6l.2-3.1h1.4l.2 9.2h2.1l.3 4.1h17.4l.2-28.7h3.8l.2-9.9h3.7l.2-27h1.9l.2-7.8h1l.2-.8h3.2l.3 1.1h1.8l.3-1.1h1.8l.3 1.1l1.1-.3l.3-.8h2l.3 .8h.7l.4 7.8h1.7l.2 36.9l.5-2.6h2.1l.2 2.6h1.1l.3-2.6h2.5l.3 17.1h4.2l.2-12.4h1.6l.2-6.9h14l.2 3.5h10.9l.3-6.5h10.4l.3-78.7h10.9l.2-7.3h7.5l.2-2.3l17.7 .2l.2 9.4h2.8l.2 19.1h28.6l.2 129.7h4.5l.2-117.1l18.8-28.2l.7-.2l2.8 19h.5l.2-19h.5l26.9 29.2l.3 49h4.8l.3-107.7h67.1l.3 24.5h13.9l.2-146.9h19.3l.3-9h12.3l.9-50h1.2l.9 50h7l.2 15.1h.9l.3 68l6.3-9.1l.4-14.4l.5-1.1l.2-5.6l1.2 21.2l13.2 19.1l2.8 9.3l.2 3.4h1.7l.2 3.4h1.4l.2 3.4h1.4l.3 3.4h1.4l.2 145.5l.5 41.6h2.5l.2-281.8h13.3l.2-5.4h18.1l.3-13.9h.9l.2 13.9h18.2l.2 5.4h13l.3 109.8l.2-12.6h70.4l.5 12.6h6.5l.2 6.1h43.5l.4 8.2l.3 76.1h4.9l.2-10.6h11.6l.2-2.5h18.2l.2 2.5h10.2l.3 6.4h.4l.3 4.2h5.5l.3-28h37.9l.2 119.5h41.6l.2-81.3h1.9l.7-15h12.8l.7 15h4.1l.7-6.2h14.2l.7 6.2h1.6l.3 9.2h12.5l.7-7.5h11.9l.7 7.5h1.6v72.1h24" };
@@ -0,0 +1,29 @@
1
+ export type BootTheme = "light" | "dark";
2
+ /** An element: its tag, its attributes, and its children or text. */
3
+ export type BootNode = readonly [tag: string, attributes: Readonly<Record<string, string>>, children?: readonly BootNode[] | string];
4
+ /** The inline style of the element that holds a viewer, or a host's placeholder, as it boots. */
5
+ export declare function bootHostStyle(theme: BootTheme): string;
6
+ /** The silhouette's styles: self-contained, so they need neither study.css nor viewer.css. */
7
+ export declare const BOOT_CSS: string;
8
+ /**
9
+ * The silhouette: one element holding its styles, the outline under the curtain, and a faint
10
+ * rail of the whole outline over both, so the shape shows from the start.
11
+ */
12
+ export declare function bootTree(theme: BootTheme): BootNode;
13
+ /**
14
+ * Builds the silhouette for `root`, without parsing markup, so a page's Trusted Types policy
15
+ * allows it. Its styles go to `root` as a constructed stylesheet, which a content security
16
+ * policy without inline styles still allows, so the silhouette never lands unstyled in the
17
+ * viewer's layout; a browser without them gets the `<style>` element. Returns the element to
18
+ * append, and a function that removes it and its styles.
19
+ */
20
+ export declare function createBoot(root: ShadowRoot, theme: BootTheme): {
21
+ element: HTMLElement;
22
+ remove(): void;
23
+ };
24
+ /** Escapes text for an HTML attribute value or text. */
25
+ export declare const escapeHtml: (text: string) => string;
26
+ /** The silhouette as HTML, for a page's markup. */
27
+ export declare function bootHtml(node: BootNode): string;
28
+ /** The attribute that marks a host's placeholder, whose shadow root holds a silhouette. */
29
+ export declare const PLACEHOLDER_ATTRIBUTE = "data-skyline-boot";
@@ -0,0 +1,113 @@
1
+ // The loading silhouette a viewer shows from the moment it mounts until its first frame. The
2
+ // shell (skyline-shell.ts), which hosts bundle, paints it synchronously, before any asset
3
+ // arrives; a host can paint the same silhouette in its HTML before its own code runs
4
+ // (`skylineBootMarkup` in package-assets.ts), which `mountSkyline` then continues in step.
5
+ // Both build it from the one description here, with their own styles, so the two match pixel
6
+ // for pixel. Importing it never touches the DOM.
7
+ //
8
+ // The outline draws itself left to right as a curtain of the backdrop's colour slides off it,
9
+ // then fades and repeats. Only transform and opacity animate, which the browser runs off the
10
+ // main thread, so it keeps moving while the scene's first frame is being built.
11
+ import { bootOutline } from "./skyline-boot-outline.js";
12
+ // Set on the viewer's element, and on a host's placeholder, where it outranks the page's rules:
13
+ // `all: initial` keeps the host's rules for its elements off it, so nothing they set is
14
+ // inherited, `direction` (which `all` leaves) keeps a right-to-left page from mirroring the
15
+ // layout, and the rest fills the container. The boot palette follows; keep it in sync with
16
+ // viewer.css and index.html.
17
+ const HOST_STYLE = "all:initial;direction:ltr;display:block;position:relative;isolation:isolate;width:100%;height:100%;overflow:hidden";
18
+ const PALETTE = {
19
+ dark: { backdrop: "#000", text: "#aaa", ink: "#ddd" },
20
+ light: { backdrop: "#f5f5f5", text: "#444", ink: "#555" },
21
+ };
22
+ /** The inline style of the element that holds a viewer, or a host's placeholder, as it boots. */
23
+ export function bootHostStyle(theme) {
24
+ return `${HOST_STYLE};color-scheme:${theme};background-color:${PALETTE[theme].backdrop};color:${PALETTE[theme].text}`;
25
+ }
26
+ // The outline's proportions, for sizing it within the viewer.
27
+ const ratio = bootOutline.width / bootOutline.height;
28
+ // The folded control bar's height: its top border, padding and 28px star, and its bottom
29
+ // padding, which grows with a phone's home indicator (study.css's .control-bar). The outline
30
+ // stands on the bar, where the skyline will, and the bar arrives without moving it.
31
+ const bar = "calc(34px + max(5px, env(safe-area-inset-bottom)))";
32
+ // The curtain's soft leading edge, and the room the clip leaves for the stroke's round caps.
33
+ const edge = 24, bleed = 8;
34
+ const period = "4s";
35
+ /** The silhouette's styles: self-contained, so they need neither study.css nor viewer.css. */
36
+ export const BOOT_CSS = `.skyline-boot,.skyline-boot *{box-sizing:border-box;margin:0;padding:0}
37
+ .skyline-boot{position:absolute;inset:0;z-index:1;display:block;overflow:hidden;pointer-events:none;container-type:size;--boot-ink:${PALETTE.dark.ink};--boot-backdrop:${PALETTE.dark.backdrop}}
38
+ .skyline-boot[data-theme="light"]{--boot-ink:${PALETTE.light.ink};--boot-backdrop:${PALETTE.light.backdrop}}
39
+ .skyline-boot[hidden]{display:none}
40
+ .skyline-boot-frame{position:absolute;left:0;right:0;bottom:${bar};display:block;width:min(480px,80cqw,calc(30cqh * ${ratio.toFixed(4)}));aspect-ratio:${bootOutline.width}/${bootOutline.height};margin:0 auto;animation:skyline-boot-fade ${period} linear infinite}
41
+ .skyline-boot-clip{position:absolute;inset:-${bleed}px;display:block;overflow:hidden}
42
+ .skyline-boot-clip>svg{position:absolute;top:${bleed}px;left:${bleed}px;display:block;width:calc(100% - ${2 * bleed}px);height:calc(100% - ${2 * bleed}px);overflow:visible;fill:none;stroke:var(--boot-ink);stroke-width:1.6px;stroke-linejoin:round;stroke-linecap:round}
43
+ .skyline-boot-clip path{vector-effect:non-scaling-stroke}
44
+ .skyline-boot-rail{opacity:0.24}
45
+ .skyline-boot-curtain{position:absolute;inset:0;display:block;background:linear-gradient(to right,transparent,var(--boot-backdrop) ${edge}px);will-change:transform;animation:skyline-boot-sweep ${period} linear infinite}
46
+ @keyframes skyline-boot-sweep{0%{transform:translateX(-${edge}px)}78%,100%{transform:translateX(100%)}}
47
+ @keyframes skyline-boot-fade{0%{opacity:0}5%,88%{opacity:1}100%{opacity:0}}
48
+ @media (prefers-reduced-motion:reduce){.skyline-boot-frame{animation:none;opacity:0.65}.skyline-boot-curtain{display:none;animation:none}}`;
49
+ /**
50
+ * The silhouette: one element holding its styles, the outline under the curtain, and a faint
51
+ * rail of the whole outline over both, so the shape shows from the start.
52
+ */
53
+ export function bootTree(theme) {
54
+ const outline = (name) => ["svg", { class: `skyline-boot-${name}`, viewBox: `0 0 ${bootOutline.width} ${bootOutline.height}`, "aria-hidden": "true", focusable: "false" }, [["path", { d: bootOutline.d }]]];
55
+ return ["div", { class: "skyline-boot", "data-theme": theme, "aria-hidden": "true" }, [
56
+ ["style", {}, BOOT_CSS],
57
+ ["div", { class: "skyline-boot-frame" }, [
58
+ ["div", { class: "skyline-boot-clip" }, [outline("line"), ["div", { class: "skyline-boot-curtain" }, []], outline("rail")]],
59
+ ]],
60
+ ]];
61
+ }
62
+ const SVG = "http://www.w3.org/2000/svg";
63
+ /**
64
+ * Builds the silhouette for `root`, without parsing markup, so a page's Trusted Types policy
65
+ * allows it. Its styles go to `root` as a constructed stylesheet, which a content security
66
+ * policy without inline styles still allows, so the silhouette never lands unstyled in the
67
+ * viewer's layout; a browser without them gets the `<style>` element. Returns the element to
68
+ * append, and a function that removes it and its styles.
69
+ */
70
+ export function createBoot(root, theme) {
71
+ const document = root.ownerDocument;
72
+ const Sheet = document.defaultView?.CSSStyleSheet;
73
+ let sheet;
74
+ if (Sheet && "adoptedStyleSheets" in root && "replaceSync" in Sheet.prototype) {
75
+ sheet = new Sheet();
76
+ sheet.replaceSync(BOOT_CSS);
77
+ root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
78
+ }
79
+ const build = ([tag, attributes, children], svg) => {
80
+ const inSvg = svg || tag === "svg";
81
+ const node = inSvg ? document.createElementNS(SVG, tag) : document.createElement(tag);
82
+ for (const [name, value] of Object.entries(attributes))
83
+ node.setAttribute(name, value);
84
+ if (typeof children === "string")
85
+ node.textContent = children;
86
+ else
87
+ for (const child of children ?? [])
88
+ if (!(sheet && child[0] === "style"))
89
+ node.append(build(child, inSvg));
90
+ return node;
91
+ };
92
+ const element = build(bootTree(theme), false);
93
+ return {
94
+ element,
95
+ remove() {
96
+ element.remove();
97
+ if (sheet)
98
+ root.adoptedStyleSheets = root.adoptedStyleSheets.filter((adopted) => adopted !== sheet);
99
+ },
100
+ };
101
+ }
102
+ /** Escapes text for an HTML attribute value or text. */
103
+ export const escapeHtml = (text) => text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
104
+ /** The silhouette as HTML, for a page's markup. */
105
+ export function bootHtml(node) {
106
+ const [tag, attributes, children] = node;
107
+ const attributeText = Object.entries(attributes).map(([name, value]) => ` ${name}="${escapeHtml(value)}"`).join("");
108
+ // Style text is CSS, not HTML, and comes only from BOOT_CSS; any other text is escaped.
109
+ const content = typeof children === "string" ? (tag === "style" ? children : escapeHtml(children)) : (children ?? []).map(bootHtml).join("");
110
+ return `<${tag}${attributeText}>${content}</${tag}>`;
111
+ }
112
+ /** The attribute that marks a host's placeholder, whose shadow root holds a silhouette. */
113
+ export const PLACEHOLDER_ATTRIBUTE = "data-skyline-boot";
@@ -33,6 +33,15 @@ export interface SkylineOptions {
33
33
  * assets' copy, which needs no `three` dependency.
34
34
  */
35
35
  readonly three?: SkylineThree | PromiseLike<SkylineThree>;
36
+ /**
37
+ * The loading silhouette the host painted in its HTML before its code ran, from
38
+ * `skylineBootMarkup` in `@a2f0/skyline/build`, or an element holding it, placed and sized
39
+ * like `container`. The viewer paints its own silhouette over the same place, continues the
40
+ * animation where the placeholder reached, and removes the placeholder, all before the
41
+ * browser next paints. For the default folded control bar: with `controls: "open"`, it is
42
+ * removed and the scene's own outline shows once its markup arrives.
43
+ */
44
+ readonly placeholder?: Element | null;
36
45
  }
37
46
  /**
38
47
  * Mounts the complete interactive viewer filling `container`, inside a shadow root. It
@@ -40,4 +49,4 @@ export interface SkylineOptions {
40
49
  * size and placement: give it an explicit height. Call `destroy` when the hosting component
41
50
  * leaves.
42
51
  */
43
- export declare function mountSkyline(container: HTMLElement, { assetsUrl, title, controls, colour, theme, attribution, three }: SkylineOptions): SkylineInstance;
52
+ export declare function mountSkyline(container: HTMLElement, { assetsUrl, title, controls, colour, theme, attribution, three, placeholder }: SkylineOptions): SkylineInstance;
@@ -8,10 +8,10 @@ import { mountViewer } from "./skyline-shell.js";
8
8
  * size and placement: give it an explicit height. Call `destroy` when the hosting component
9
9
  * leaves.
10
10
  */
11
- export function mountSkyline(container, { assetsUrl, title = "Interactive Chicago skyline", controls = "closed", colour = false, theme, attribution = true, three }) {
11
+ export function mountSkyline(container, { assetsUrl, title = "Interactive Chicago skyline", controls = "closed", colour = false, theme, attribution = true, three, placeholder }) {
12
12
  if (controls !== "open" && controls !== "closed")
13
13
  throw new TypeError('Skyline controls must be "open" or "closed".');
14
14
  if (typeof colour !== "boolean")
15
15
  throw new TypeError("Skyline colour must be a boolean.");
16
- return mountViewer(container, { assetsUrl, title, controls, colour, theme, attribution, navigation: false, page: false, three });
16
+ return mountViewer(container, { assetsUrl, title, controls, colour, theme, attribution, navigation: false, page: false, three, placeholder });
17
17
  }
@@ -26,6 +26,13 @@ export interface ViewerContext {
26
26
  readonly page: boolean;
27
27
  /** The host's Three.js, for the scene to share (three-engine.ts). */
28
28
  readonly three?: SkylineThree | PromiseLike<SkylineThree> | undefined;
29
+ /**
30
+ * The loading silhouette the shell painted at mount (skyline-boot.ts), with a folded bar: the
31
+ * viewer hides it whenever the scene isn't booting in view, and hides the scene markup's own
32
+ * outline, so one silhouette shows throughout. Absent with an open bar, whose height only the
33
+ * scene's markup knows: the markup's outline then shows, as on skyline-3d.html.
34
+ */
35
+ readonly boot?: HTMLElement | undefined;
29
36
  }
30
37
  /** What the viewer module exports. */
31
38
  export interface ViewerModule {
@@ -58,8 +65,13 @@ export interface SkylineInstance {
58
65
  */
59
66
  destroy(): void;
60
67
  }
61
- export interface ViewerOptions extends Omit<ViewerContext, "assets" | "signal" | "theme"> {
68
+ export interface ViewerOptions extends Omit<ViewerContext, "assets" | "signal" | "theme" | "boot"> {
62
69
  readonly theme?: "light" | "dark" | undefined;
70
+ /**
71
+ * The host's own loading silhouette, painted from `skylineBootMarkup` before its code ran, or
72
+ * an element holding it: the viewer continues its animation in step and removes it.
73
+ */
74
+ readonly placeholder?: Element | null | undefined;
63
75
  /** The assets' directory; relative URLs resolve against the container's document. */
64
76
  readonly assetsUrl: string | URL;
65
77
  /** The viewer's accessible name: `element` is a region with this label. */
@@ -71,4 +83,4 @@ export declare function assetsBase(document: Document, assetsUrl: string | URL):
71
83
  * Mounts the viewer in a new element appended to `container`, and returns at once: the
72
84
  * element is in place, and `ready` settles once the 3D skyline shows or can't start.
73
85
  */
74
- export declare function mountViewer(container: HTMLElement, { assetsUrl, title, ...options }: ViewerOptions): SkylineInstance;
86
+ export declare function mountViewer(container: HTMLElement, { assetsUrl, title, placeholder, ...options }: ViewerOptions): SkylineInstance;
@@ -1,3 +1,13 @@
1
+ // Mounts the viewer into a shadow root in the host's document: the shell that both the
2
+ // package's `mountSkyline` (skyline-package.ts) and the site's own index.html
3
+ // (viewer-page.ts) use. It validates the assets URL, makes the element and its shadow root,
4
+ // loads the viewer's stylesheets and its module from the assets, and owns the instance's
5
+ // lifetime. Importing it never touches the DOM.
6
+ //
7
+ // The viewer module (skyline-viewer.ts, served as skyline-viewer.js) renders only inside the
8
+ // root it is given, resolves every URL from `ViewerContext.assets`, and releases what it holds
9
+ // when `ViewerContext.signal` aborts.
10
+ import { bootHostStyle, createBoot, PLACEHOLDER_ATTRIBUTE } from "./skyline-boot.js";
1
11
  // The viewer's code and styles, relative to the assets.
2
12
  const VIEWER = "skyline-viewer.js";
3
13
  const STYLESHEETS = ["study.css", "viewer.css"];
@@ -46,11 +56,6 @@ function loaded(link) {
46
56
  link.addEventListener("error", () => reject(new Error(`Couldn't load ${link.href}.`)), { once: true });
47
57
  });
48
58
  }
49
- // Set on the element itself, where it outranks the host page's rules: `all: initial` keeps
50
- // the host's rules for its elements (a `div` or `*` rule) off it, so nothing they set is
51
- // inherited into the viewer, `direction` (which `all` leaves) keeps a right-to-left page from
52
- // mirroring the viewer's layout, and the rest sizes it to fill the container.
53
- const HOST_STYLE = "all:initial;direction:ltr;display:block;position:relative;isolation:isolate;width:100%;height:100%;overflow:hidden";
54
59
  // The failure message's own look, since the viewer's stylesheets may be what failed.
55
60
  const FAILURE_STYLE = "box-sizing:border-box;height:100%;margin:0;padding:24px;background:inherit;color:inherit;font:12px/1.5 ui-monospace,monospace";
56
61
  /** Shows why the viewer couldn't start, in place of the viewer. */
@@ -61,23 +66,40 @@ function showFailure(root, error) {
61
66
  message.textContent = `The Chicago skyline couldn't start: ${error instanceof Error ? error.message : String(error)}`;
62
67
  root.replaceChildren(...[...root.querySelectorAll("link")].filter((link) => link.sheet), message);
63
68
  }
69
+ /**
70
+ * Starts the silhouette's animations where the host's placeholder has reached, so swapping one
71
+ * for the other shows no jump. Animations pair by name; any the placeholder lacks, as with
72
+ * reduced motion, start afresh.
73
+ */
74
+ function continuePlaceholder(boot, placeholder) {
75
+ const painted = placeholder.matches(`[${PLACEHOLDER_ATTRIBUTE}]`) ? placeholder : placeholder.querySelector(`[${PLACEHOLDER_ATTRIBUTE}]`);
76
+ const theirs = painted?.shadowRoot?.querySelector(".skyline-boot")?.getAnimations?.({ subtree: true }) ?? [];
77
+ // Only CSS animations have names; anything else a page animates is left alone.
78
+ const name = (animation) => animation.animationName || undefined;
79
+ for (const ours of boot.getAnimations?.({ subtree: true }) ?? []) {
80
+ const match = name(ours) && theirs.find((animation) => name(animation) === name(ours));
81
+ if (match && match.startTime != null)
82
+ ours.startTime = match.startTime;
83
+ }
84
+ }
64
85
  /**
65
86
  * Mounts the viewer in a new element appended to `container`, and returns at once: the
66
87
  * element is in place, and `ready` settles once the 3D skyline shows or can't start.
67
88
  */
68
- export function mountViewer(container, { assetsUrl, title, ...options }) {
89
+ export function mountViewer(container, { assetsUrl, title, placeholder, ...options }) {
69
90
  const document = container.ownerDocument;
70
91
  const assets = assetsBase(document, assetsUrl);
71
92
  const theme = options.theme === undefined ? (options.colour ? "light" : "dark") : options.theme;
72
93
  if (theme !== "light" && theme !== "dark")
73
94
  throw new TypeError('Skyline theme must be "light" or "dark".');
95
+ // The placeholder is removed once the viewer is in place, which must not take the viewer with it.
96
+ if (placeholder != null && placeholder.nodeType !== 1)
97
+ throw new TypeError("Skyline placeholder must be an element.");
98
+ if (placeholder?.contains(container))
99
+ throw new TypeError("Skyline placeholder must not hold the container.");
74
100
  const element = document.createElement("div");
75
- element.style.cssText = HOST_STYLE;
76
101
  // Paint before any assets arrive, including when a stylesheet fails to load.
77
- // Keep the boot palette in sync with viewer.css and index.html.
78
- element.style.colorScheme = theme;
79
- element.style.backgroundColor = theme === "light" ? "#f5f5f5" : "#000";
80
- element.style.color = theme === "light" ? "#444" : "#aaa";
102
+ element.style.cssText = bootHostStyle(theme);
81
103
  element.setAttribute("role", "region");
82
104
  element.setAttribute("aria-label", title);
83
105
  element.setAttribute("aria-busy", "true");
@@ -89,7 +111,18 @@ export function mountViewer(container, { assetsUrl, title, ...options }) {
89
111
  return link;
90
112
  });
91
113
  root.append(...stylesheets);
114
+ // The loading silhouette, at once, with no network: it stands on the folded bar, which a bar
115
+ // that starts open would cover.
116
+ const silhouette = options.controls === "closed" ? createBoot(root, theme) : undefined;
117
+ const boot = silhouette?.element;
118
+ if (boot)
119
+ root.append(boot);
92
120
  container.append(element);
121
+ if (placeholder) {
122
+ if (boot)
123
+ continuePlaceholder(boot, placeholder);
124
+ placeholder.remove();
125
+ }
93
126
  // A host's engine that fails to load is the viewer's to report, once its scene asks for it
94
127
  // (three-engine.ts); until then, or if the instance never gets that far, its rejection is
95
128
  // handled here rather than left unhandled.
@@ -97,7 +130,7 @@ export function mountViewer(container, { assetsUrl, title, ...options }) {
97
130
  Promise.resolve(options.three).catch(() => { });
98
131
  const controller = new AbortController();
99
132
  let destroyed = false, mounted = false;
100
- const context = { ...options, theme, assets, signal: controller.signal };
133
+ const context = { ...options, theme, assets, signal: controller.signal, boot };
101
134
  if (options.three)
102
135
  sharedEngines.add(assets.href);
103
136
  const preloads = preloadModules(document, assets, sharedEngines.has(assets.href), controller.signal);
@@ -131,8 +164,10 @@ export function mountViewer(container, { assetsUrl, title, ...options }) {
131
164
  });
132
165
  });
133
166
  // The element and the console report a failure, so a host that ignores `ready` sees no
134
- // unhandled rejection.
135
- ready.then(dropPreloads, dropPreloads);
167
+ // unhandled rejection. The viewer owns the silhouette's visibility while it boots; the
168
+ // shell owns its removal, which also covers a viewer from older assets that doesn't know it.
169
+ const settle = () => { dropPreloads(); silhouette?.remove(); };
170
+ ready.then(settle, settle);
136
171
  return {
137
172
  element,
138
173
  ready,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a2f0/skyline",
3
- "version": "0.2.74",
3
+ "version": "0.2.75",
4
4
  "description": "Interactive Chicago skyline viewer and reusable Three.js building models",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
package/site/index.html CHANGED
@@ -10,9 +10,11 @@
10
10
  <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
11
11
  <title>Chicago Skyline — Living Night</title>
12
12
  <!-- viewer-page.js mounts the viewer from skyline-viewer.js, its styles and skyline-3d.html,
13
- and starts the scene's code; fetch them alongside it, with the list of the scene's
14
- modules that the viewer's shell preloads. -->
13
+ and starts the scene's code; fetch them alongside it, with the shell's loading
14
+ silhouette and the list of the scene's modules that the viewer's shell preloads. -->
15
15
  <link rel="modulepreload" href="skyline-shell.js" />
16
+ <link rel="modulepreload" href="skyline-boot.js" />
17
+ <link rel="modulepreload" href="skyline-boot-outline.js" />
16
18
  <link rel="modulepreload" href="skyline-viewer.js" />
17
19
  <link rel="preload" href="viewer-modules.json" as="fetch" crossorigin="anonymous" />
18
20
  <link rel="preload" href="viewer.css" as="style" />
@@ -40,7 +42,7 @@
40
42
  color: #aaa;
41
43
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
42
44
  }
43
- /* Keep the boot palette in sync with skyline-shell.ts and viewer.css. */
45
+ /* Keep the boot palette in sync with skyline-boot.ts and viewer.css. */
44
46
  :root[data-theme="light"] { color-scheme: light; background: #f5f5f5; }
45
47
  :root[data-theme="light"] .notice { color: #444; }
46
48
  </style>
@@ -0,0 +1,4 @@
1
+ // Generated by scripts/checks/skyline-loading.ts from the mapped buildings: the level skyline
2
+ // elevation the shell paints while a viewer loads (skyline-boot.ts). After changing a building,
3
+ // run that script; `bun run check` fails while this is stale.
4
+ export const bootOutline = { width: 1000, height: 379.13, d: "M0 379.1h24v-61.4l12.3-6.3h38.4l2.5 1.3l.3-1h8.3l.3-4.2l44.1 .5l.2 1h.5l.2-1h29.3l.5 4.7h15.8l.2 7.2h7.7l.9 1.1h16.8l.2-1.3l.7-.4l.5 2.7l3 6.6l.2-3.1h1.4l.2 9.2h2.1l.3 4.1h17.4l.2-28.7h3.8l.2-9.9h3.7l.2-27h1.9l.2-7.8h1l.2-.8h3.2l.3 1.1h1.8l.3-1.1h1.8l.3 1.1l1.1-.3l.3-.8h2l.3 .8h.7l.4 7.8h1.7l.2 36.9l.5-2.6h2.1l.2 2.6h1.1l.3-2.6h2.5l.3 17.1h4.2l.2-12.4h1.6l.2-6.9h14l.2 3.5h10.9l.3-6.5h10.4l.3-78.7h10.9l.2-7.3h7.5l.2-2.3l17.7 .2l.2 9.4h2.8l.2 19.1h28.6l.2 129.7h4.5l.2-117.1l18.8-28.2l.7-.2l2.8 19h.5l.2-19h.5l26.9 29.2l.3 49h4.8l.3-107.7h67.1l.3 24.5h13.9l.2-146.9h19.3l.3-9h12.3l.9-50h1.2l.9 50h7l.2 15.1h.9l.3 68l6.3-9.1l.4-14.4l.5-1.1l.2-5.6l1.2 21.2l13.2 19.1l2.8 9.3l.2 3.4h1.7l.2 3.4h1.4l.2 3.4h1.4l.3 3.4h1.4l.2 145.5l.5 41.6h2.5l.2-281.8h13.3l.2-5.4h18.1l.3-13.9h.9l.2 13.9h18.2l.2 5.4h13l.3 109.8l.2-12.6h70.4l.5 12.6h6.5l.2 6.1h43.5l.4 8.2l.3 76.1h4.9l.2-10.6h11.6l.2-2.5h18.2l.2 2.5h10.2l.3 6.4h.4l.3 4.2h5.5l.3-28h37.9l.2 119.5h41.6l.2-81.3h1.9l.7-15h12.8l.7 15h4.1l.7-6.2h14.2l.7 6.2h1.6l.3 9.2h12.5l.7-7.5h11.9l.7 7.5h1.6v72.1h24" };
@@ -0,0 +1,113 @@
1
+ // The loading silhouette a viewer shows from the moment it mounts until its first frame. The
2
+ // shell (skyline-shell.ts), which hosts bundle, paints it synchronously, before any asset
3
+ // arrives; a host can paint the same silhouette in its HTML before its own code runs
4
+ // (`skylineBootMarkup` in package-assets.ts), which `mountSkyline` then continues in step.
5
+ // Both build it from the one description here, with their own styles, so the two match pixel
6
+ // for pixel. Importing it never touches the DOM.
7
+ //
8
+ // The outline draws itself left to right as a curtain of the backdrop's colour slides off it,
9
+ // then fades and repeats. Only transform and opacity animate, which the browser runs off the
10
+ // main thread, so it keeps moving while the scene's first frame is being built.
11
+ import { bootOutline } from "./skyline-boot-outline.js";
12
+ // Set on the viewer's element, and on a host's placeholder, where it outranks the page's rules:
13
+ // `all: initial` keeps the host's rules for its elements off it, so nothing they set is
14
+ // inherited, `direction` (which `all` leaves) keeps a right-to-left page from mirroring the
15
+ // layout, and the rest fills the container. The boot palette follows; keep it in sync with
16
+ // viewer.css and index.html.
17
+ const HOST_STYLE = "all:initial;direction:ltr;display:block;position:relative;isolation:isolate;width:100%;height:100%;overflow:hidden";
18
+ const PALETTE = {
19
+ dark: { backdrop: "#000", text: "#aaa", ink: "#ddd" },
20
+ light: { backdrop: "#f5f5f5", text: "#444", ink: "#555" },
21
+ };
22
+ /** The inline style of the element that holds a viewer, or a host's placeholder, as it boots. */
23
+ export function bootHostStyle(theme) {
24
+ return `${HOST_STYLE};color-scheme:${theme};background-color:${PALETTE[theme].backdrop};color:${PALETTE[theme].text}`;
25
+ }
26
+ // The outline's proportions, for sizing it within the viewer.
27
+ const ratio = bootOutline.width / bootOutline.height;
28
+ // The folded control bar's height: its top border, padding and 28px star, and its bottom
29
+ // padding, which grows with a phone's home indicator (study.css's .control-bar). The outline
30
+ // stands on the bar, where the skyline will, and the bar arrives without moving it.
31
+ const bar = "calc(34px + max(5px, env(safe-area-inset-bottom)))";
32
+ // The curtain's soft leading edge, and the room the clip leaves for the stroke's round caps.
33
+ const edge = 24, bleed = 8;
34
+ const period = "4s";
35
+ /** The silhouette's styles: self-contained, so they need neither study.css nor viewer.css. */
36
+ export const BOOT_CSS = `.skyline-boot,.skyline-boot *{box-sizing:border-box;margin:0;padding:0}
37
+ .skyline-boot{position:absolute;inset:0;z-index:1;display:block;overflow:hidden;pointer-events:none;container-type:size;--boot-ink:${PALETTE.dark.ink};--boot-backdrop:${PALETTE.dark.backdrop}}
38
+ .skyline-boot[data-theme="light"]{--boot-ink:${PALETTE.light.ink};--boot-backdrop:${PALETTE.light.backdrop}}
39
+ .skyline-boot[hidden]{display:none}
40
+ .skyline-boot-frame{position:absolute;left:0;right:0;bottom:${bar};display:block;width:min(480px,80cqw,calc(30cqh * ${ratio.toFixed(4)}));aspect-ratio:${bootOutline.width}/${bootOutline.height};margin:0 auto;animation:skyline-boot-fade ${period} linear infinite}
41
+ .skyline-boot-clip{position:absolute;inset:-${bleed}px;display:block;overflow:hidden}
42
+ .skyline-boot-clip>svg{position:absolute;top:${bleed}px;left:${bleed}px;display:block;width:calc(100% - ${2 * bleed}px);height:calc(100% - ${2 * bleed}px);overflow:visible;fill:none;stroke:var(--boot-ink);stroke-width:1.6px;stroke-linejoin:round;stroke-linecap:round}
43
+ .skyline-boot-clip path{vector-effect:non-scaling-stroke}
44
+ .skyline-boot-rail{opacity:0.24}
45
+ .skyline-boot-curtain{position:absolute;inset:0;display:block;background:linear-gradient(to right,transparent,var(--boot-backdrop) ${edge}px);will-change:transform;animation:skyline-boot-sweep ${period} linear infinite}
46
+ @keyframes skyline-boot-sweep{0%{transform:translateX(-${edge}px)}78%,100%{transform:translateX(100%)}}
47
+ @keyframes skyline-boot-fade{0%{opacity:0}5%,88%{opacity:1}100%{opacity:0}}
48
+ @media (prefers-reduced-motion:reduce){.skyline-boot-frame{animation:none;opacity:0.65}.skyline-boot-curtain{display:none;animation:none}}`;
49
+ /**
50
+ * The silhouette: one element holding its styles, the outline under the curtain, and a faint
51
+ * rail of the whole outline over both, so the shape shows from the start.
52
+ */
53
+ export function bootTree(theme) {
54
+ const outline = (name) => ["svg", { class: `skyline-boot-${name}`, viewBox: `0 0 ${bootOutline.width} ${bootOutline.height}`, "aria-hidden": "true", focusable: "false" }, [["path", { d: bootOutline.d }]]];
55
+ return ["div", { class: "skyline-boot", "data-theme": theme, "aria-hidden": "true" }, [
56
+ ["style", {}, BOOT_CSS],
57
+ ["div", { class: "skyline-boot-frame" }, [
58
+ ["div", { class: "skyline-boot-clip" }, [outline("line"), ["div", { class: "skyline-boot-curtain" }, []], outline("rail")]],
59
+ ]],
60
+ ]];
61
+ }
62
+ const SVG = "http://www.w3.org/2000/svg";
63
+ /**
64
+ * Builds the silhouette for `root`, without parsing markup, so a page's Trusted Types policy
65
+ * allows it. Its styles go to `root` as a constructed stylesheet, which a content security
66
+ * policy without inline styles still allows, so the silhouette never lands unstyled in the
67
+ * viewer's layout; a browser without them gets the `<style>` element. Returns the element to
68
+ * append, and a function that removes it and its styles.
69
+ */
70
+ export function createBoot(root, theme) {
71
+ const document = root.ownerDocument;
72
+ const Sheet = document.defaultView?.CSSStyleSheet;
73
+ let sheet;
74
+ if (Sheet && "adoptedStyleSheets" in root && "replaceSync" in Sheet.prototype) {
75
+ sheet = new Sheet();
76
+ sheet.replaceSync(BOOT_CSS);
77
+ root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
78
+ }
79
+ const build = ([tag, attributes, children], svg) => {
80
+ const inSvg = svg || tag === "svg";
81
+ const node = inSvg ? document.createElementNS(SVG, tag) : document.createElement(tag);
82
+ for (const [name, value] of Object.entries(attributes))
83
+ node.setAttribute(name, value);
84
+ if (typeof children === "string")
85
+ node.textContent = children;
86
+ else
87
+ for (const child of children ?? [])
88
+ if (!(sheet && child[0] === "style"))
89
+ node.append(build(child, inSvg));
90
+ return node;
91
+ };
92
+ const element = build(bootTree(theme), false);
93
+ return {
94
+ element,
95
+ remove() {
96
+ element.remove();
97
+ if (sheet)
98
+ root.adoptedStyleSheets = root.adoptedStyleSheets.filter((adopted) => adopted !== sheet);
99
+ },
100
+ };
101
+ }
102
+ /** Escapes text for an HTML attribute value or text. */
103
+ export const escapeHtml = (text) => text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
104
+ /** The silhouette as HTML, for a page's markup. */
105
+ export function bootHtml(node) {
106
+ const [tag, attributes, children] = node;
107
+ const attributeText = Object.entries(attributes).map(([name, value]) => ` ${name}="${escapeHtml(value)}"`).join("");
108
+ // Style text is CSS, not HTML, and comes only from BOOT_CSS; any other text is escaped.
109
+ const content = typeof children === "string" ? (tag === "style" ? children : escapeHtml(children)) : (children ?? []).map(bootHtml).join("");
110
+ return `<${tag}${attributeText}>${content}</${tag}>`;
111
+ }
112
+ /** The attribute that marks a host's placeholder, whose shadow root holds a silhouette. */
113
+ export const PLACEHOLDER_ATTRIBUTE = "data-skyline-boot";
@@ -1,3 +1,13 @@
1
+ // Mounts the viewer into a shadow root in the host's document: the shell that both the
2
+ // package's `mountSkyline` (skyline-package.ts) and the site's own index.html
3
+ // (viewer-page.ts) use. It validates the assets URL, makes the element and its shadow root,
4
+ // loads the viewer's stylesheets and its module from the assets, and owns the instance's
5
+ // lifetime. Importing it never touches the DOM.
6
+ //
7
+ // The viewer module (skyline-viewer.ts, served as skyline-viewer.js) renders only inside the
8
+ // root it is given, resolves every URL from `ViewerContext.assets`, and releases what it holds
9
+ // when `ViewerContext.signal` aborts.
10
+ import { bootHostStyle, createBoot, PLACEHOLDER_ATTRIBUTE } from "./skyline-boot.js";
1
11
  // The viewer's code and styles, relative to the assets.
2
12
  const VIEWER = "skyline-viewer.js";
3
13
  const STYLESHEETS = ["study.css", "viewer.css"];
@@ -46,11 +56,6 @@ function loaded(link) {
46
56
  link.addEventListener("error", () => reject(new Error(`Couldn't load ${link.href}.`)), { once: true });
47
57
  });
48
58
  }
49
- // Set on the element itself, where it outranks the host page's rules: `all: initial` keeps
50
- // the host's rules for its elements (a `div` or `*` rule) off it, so nothing they set is
51
- // inherited into the viewer, `direction` (which `all` leaves) keeps a right-to-left page from
52
- // mirroring the viewer's layout, and the rest sizes it to fill the container.
53
- const HOST_STYLE = "all:initial;direction:ltr;display:block;position:relative;isolation:isolate;width:100%;height:100%;overflow:hidden";
54
59
  // The failure message's own look, since the viewer's stylesheets may be what failed.
55
60
  const FAILURE_STYLE = "box-sizing:border-box;height:100%;margin:0;padding:24px;background:inherit;color:inherit;font:12px/1.5 ui-monospace,monospace";
56
61
  /** Shows why the viewer couldn't start, in place of the viewer. */
@@ -61,23 +66,40 @@ function showFailure(root, error) {
61
66
  message.textContent = `The Chicago skyline couldn't start: ${error instanceof Error ? error.message : String(error)}`;
62
67
  root.replaceChildren(...[...root.querySelectorAll("link")].filter((link) => link.sheet), message);
63
68
  }
69
+ /**
70
+ * Starts the silhouette's animations where the host's placeholder has reached, so swapping one
71
+ * for the other shows no jump. Animations pair by name; any the placeholder lacks, as with
72
+ * reduced motion, start afresh.
73
+ */
74
+ function continuePlaceholder(boot, placeholder) {
75
+ const painted = placeholder.matches(`[${PLACEHOLDER_ATTRIBUTE}]`) ? placeholder : placeholder.querySelector(`[${PLACEHOLDER_ATTRIBUTE}]`);
76
+ const theirs = painted?.shadowRoot?.querySelector(".skyline-boot")?.getAnimations?.({ subtree: true }) ?? [];
77
+ // Only CSS animations have names; anything else a page animates is left alone.
78
+ const name = (animation) => animation.animationName || undefined;
79
+ for (const ours of boot.getAnimations?.({ subtree: true }) ?? []) {
80
+ const match = name(ours) && theirs.find((animation) => name(animation) === name(ours));
81
+ if (match && match.startTime != null)
82
+ ours.startTime = match.startTime;
83
+ }
84
+ }
64
85
  /**
65
86
  * Mounts the viewer in a new element appended to `container`, and returns at once: the
66
87
  * element is in place, and `ready` settles once the 3D skyline shows or can't start.
67
88
  */
68
- export function mountViewer(container, { assetsUrl, title, ...options }) {
89
+ export function mountViewer(container, { assetsUrl, title, placeholder, ...options }) {
69
90
  const document = container.ownerDocument;
70
91
  const assets = assetsBase(document, assetsUrl);
71
92
  const theme = options.theme === undefined ? (options.colour ? "light" : "dark") : options.theme;
72
93
  if (theme !== "light" && theme !== "dark")
73
94
  throw new TypeError('Skyline theme must be "light" or "dark".');
95
+ // The placeholder is removed once the viewer is in place, which must not take the viewer with it.
96
+ if (placeholder != null && placeholder.nodeType !== 1)
97
+ throw new TypeError("Skyline placeholder must be an element.");
98
+ if (placeholder?.contains(container))
99
+ throw new TypeError("Skyline placeholder must not hold the container.");
74
100
  const element = document.createElement("div");
75
- element.style.cssText = HOST_STYLE;
76
101
  // Paint before any assets arrive, including when a stylesheet fails to load.
77
- // Keep the boot palette in sync with viewer.css and index.html.
78
- element.style.colorScheme = theme;
79
- element.style.backgroundColor = theme === "light" ? "#f5f5f5" : "#000";
80
- element.style.color = theme === "light" ? "#444" : "#aaa";
102
+ element.style.cssText = bootHostStyle(theme);
81
103
  element.setAttribute("role", "region");
82
104
  element.setAttribute("aria-label", title);
83
105
  element.setAttribute("aria-busy", "true");
@@ -89,7 +111,18 @@ export function mountViewer(container, { assetsUrl, title, ...options }) {
89
111
  return link;
90
112
  });
91
113
  root.append(...stylesheets);
114
+ // The loading silhouette, at once, with no network: it stands on the folded bar, which a bar
115
+ // that starts open would cover.
116
+ const silhouette = options.controls === "closed" ? createBoot(root, theme) : undefined;
117
+ const boot = silhouette?.element;
118
+ if (boot)
119
+ root.append(boot);
92
120
  container.append(element);
121
+ if (placeholder) {
122
+ if (boot)
123
+ continuePlaceholder(boot, placeholder);
124
+ placeholder.remove();
125
+ }
93
126
  // A host's engine that fails to load is the viewer's to report, once its scene asks for it
94
127
  // (three-engine.ts); until then, or if the instance never gets that far, its rejection is
95
128
  // handled here rather than left unhandled.
@@ -97,7 +130,7 @@ export function mountViewer(container, { assetsUrl, title, ...options }) {
97
130
  Promise.resolve(options.three).catch(() => { });
98
131
  const controller = new AbortController();
99
132
  let destroyed = false, mounted = false;
100
- const context = { ...options, theme, assets, signal: controller.signal };
133
+ const context = { ...options, theme, assets, signal: controller.signal, boot };
101
134
  if (options.three)
102
135
  sharedEngines.add(assets.href);
103
136
  const preloads = preloadModules(document, assets, sharedEngines.has(assets.href), controller.signal);
@@ -131,8 +164,10 @@ export function mountViewer(container, { assetsUrl, title, ...options }) {
131
164
  });
132
165
  });
133
166
  // The element and the console report a failure, so a host that ignores `ready` sees no
134
- // unhandled rejection.
135
- ready.then(dropPreloads, dropPreloads);
167
+ // unhandled rejection. The viewer owns the silhouette's visibility while it boots; the
168
+ // shell owns its removal, which also covers a viewer from older assets that doesn't know it.
169
+ const settle = () => { dropPreloads(); silhouette?.remove(); };
170
+ ready.then(settle, settle);
136
171
  return {
137
172
  element,
138
173
  ready,
@@ -31,13 +31,33 @@ function assertNever(value) {
31
31
  throw new Error(`Unhandled scene event: ${String(value)}`);
32
32
  }
33
33
  const SCENE_FAILURE = "The 3D preview could not load. Check that WebGL 2 is enabled, then reload. You can still compare the SVG or return to the skyline.";
34
+ /**
35
+ * Resolves once the browser has painted, or after a moment in a hidden page, which paints
36
+ * nothing, or at once when `signal` aborts, releasing its timers and frame callback.
37
+ */
38
+ function afterPaint(signal) {
39
+ return new Promise((resolve) => {
40
+ let timer = setTimeout(done, 100);
41
+ const frame = requestAnimationFrame(() => {
42
+ clearTimeout(timer);
43
+ timer = setTimeout(done, 0);
44
+ });
45
+ signal.addEventListener("abort", done, { once: true });
46
+ function done() {
47
+ clearTimeout(timer);
48
+ cancelAnimationFrame(frame);
49
+ signal.removeEventListener("abort", done);
50
+ resolve();
51
+ }
52
+ });
53
+ }
34
54
  function element(document, tag, properties = {}, attributes = {}) {
35
55
  const created = Object.assign(document.createElement(tag), properties);
36
56
  for (const [name, value] of Object.entries(attributes))
37
57
  created.setAttribute(name, value);
38
58
  return created;
39
59
  }
40
- export function mount(root, { assets, signal, controls, colour, theme, attribution, navigation, page, three }) {
60
+ export function mount(root, { assets, signal, controls, colour, theme, attribution, navigation, page, three, boot }) {
41
61
  const document = root.ownerDocument;
42
62
  const host = root.host;
43
63
  const listening = { signal };
@@ -69,6 +89,11 @@ export function mount(root, { assets, signal, controls, colour, theme, attributi
69
89
  const toolbar = navigation ? createToolbar() : null;
70
90
  function updateView() {
71
91
  viewer.toggleAttribute("data-booting", sceneStatus !== "ready" && mode === "3d");
92
+ // The shell's silhouette shows while the scene boots in view, and leaves at its first
93
+ // frame, or once it fails or loses its graphics context: a function of the scene's state
94
+ // and the mode alone. The shell removes it once `ready` settles.
95
+ if (boot)
96
+ boot.hidden = sceneStatus !== "booting" || mode !== "3d";
72
97
  scene.hidden = mode !== "3d";
73
98
  if (mode === "original" && !drawing.getAttribute("src"))
74
99
  drawing.src = new URL("skyline-original-fit.svg", assets).href;
@@ -176,6 +201,10 @@ export function mount(root, { assets, signal, controls, colour, theme, attributi
176
201
  // no fold, and the credit left out, as skyline-3d.html's own script does for its address.
177
202
  for (const child of markup.body.children)
178
203
  scene.append(document.importNode(child, true));
204
+ // The shell's silhouette is already in place; the markup's own outline stays, invisible,
205
+ // for the rules that keep the scene's notes clear of it.
206
+ if (boot)
207
+ scene.querySelector("#loading .skyline-trace")?.style.setProperty("visibility", "hidden");
179
208
  if (controls === "open") {
180
209
  const toggle = scene.querySelector("#menu-toggle");
181
210
  toggle.setAttribute("aria-expanded", "true");
@@ -191,6 +220,10 @@ export function mount(root, { assets, signal, controls, colour, theme, attributi
191
220
  signal.addEventListener("abort", () => sceneLife.abort(signal.reason), { once: true });
192
221
  try {
193
222
  const { startSkyline3d } = await sceneCode;
223
+ // The scene builds its first frame in one long task. Without the shell's silhouette,
224
+ // whose animation runs off the main thread, let the markup's outline paint first.
225
+ if (!boot)
226
+ await afterPaint(signal);
194
227
  if (signal.aborted)
195
228
  return new Promise(() => { });
196
229
  await new Promise((resolve) => {
package/site/study.css CHANGED
@@ -125,7 +125,9 @@ body.skyline-3d { height: 100vh; height: 100dvh; }
125
125
  .skyline-3d .study-annotations { inset: 0; }
126
126
  /* A level skyline elevation traces itself while the scene boots. It is inline so it
127
127
  appears before the model graph loads; errors replace it with the normal loading text.
128
- Its baseline stands on the toolbar. Scaled strokes and glow keep the outline legible. */
128
+ Its baseline stands on the toolbar. Scaled strokes and glow keep the outline legible.
129
+ In the viewer with a folded bar, the shell's own silhouette (skyline-boot.ts) shows from
130
+ the mount instead, and this one stays hidden. */
129
131
  .skyline-3d .loading:has(.skyline-trace) { inset: 0; margin: 0; display: grid; place-items: end center; padding: 32px 24px 0; pointer-events: none; }
130
132
  .skyline-trace { display: block; width: min(240px, calc(100% / 3)); max-height: 14cqh; overflow: visible; fill: none; stroke: #ddd; stroke-width: 4.2; stroke-linejoin: round; stroke-linecap: round; }
131
133
  .skyline-trace-rail { opacity: 0.08; }
package/site/viewer.css CHANGED
@@ -51,7 +51,7 @@
51
51
  .stars { transition: none; }
52
52
  }
53
53
 
54
- /* Keep this boot palette in sync with skyline-shell.ts and index.html. It gives
54
+ /* Keep this boot palette in sync with skyline-boot.ts and index.html. It gives
55
55
  way to the scene's own sky at the first frame. The original drawing always
56
56
  keeps its dark sky, including when opened during loading or after a failure. */
57
57
  .skyline-viewer[data-theme="light"][data-booting] { color-scheme: light; background: #f5f5f5; color: #444; }