Repository navigation
perf(desk): show the desk without waiting for web fonts #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
beecd9a
38b5ca6
533abf0
9fcc82c
cc41f04
7bec9a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,11 +105,15 @@ function go(to: Cam, opts: { instant?: boolean; stiffness?: number } = {}): Prom | |
| let raf = 0; | ||
| let last = 0; | ||
| let introTimer = 0; | ||
| // Until the visitor moves the camera, the opening framing is still ours to correct. | ||
| let untouched = true; | ||
|
|
||
| // The intro's pull-back is the only motion the visitor didn't ask for; any input cancels it. | ||
| // The intro's pull-back, and the re-frame once fonts arrive, are the only motion the visitor | ||
| // didn't ask for; any input cancels both. | ||
| function cancelIntro() { | ||
| clearTimeout(introTimer); | ||
| introTimer = 0; | ||
| untouched = false; | ||
| } | ||
|
|
||
| // Stops the camera outright: no frame, no pending intro, no promise left waiting. | ||
|
|
@@ -548,7 +552,8 @@ function setMode(mode: "desk" | "list", intro = false) { | |
| place(); | ||
| measure(); | ||
| drawMinimap(); | ||
| const overview = narrow() ? shelfView("home") : fitBox(everything()); | ||
| // Computed when needed, not now: web fonts may still be swapping in and resizing items. | ||
| const overview = openingView; | ||
| if (intro && !reduced.matches) { | ||
| // Start nose to nose with the seal, then pull back to show the whole desk. | ||
| const seal = document.querySelector<HTMLElement>(".home-seal") as HTMLElement; | ||
|
|
@@ -560,9 +565,12 @@ function setMode(mode: "desk" | "list", intro = false) { | |
| h: seal.offsetHeight * 0.4, | ||
| }; | ||
| go(fitBox(face, 0, 0, 0), { instant: true }); | ||
| introTimer = window.setTimeout(() => go(overview, { stiffness: 2.2 }), 650); | ||
| introTimer = window.setTimeout(() => { | ||
| introTimer = 0; | ||
| go(overview(), { stiffness: 2.2 }); | ||
| }, 650); | ||
| } else { | ||
| go(overview, { instant: true }); | ||
| go(overview(), { instant: true }); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -606,9 +614,20 @@ const resized = new ResizeObserver((entries) => { | |
| }); | ||
| for (const el of items) resized.observe(el); | ||
|
|
||
| async function start() { | ||
| await document.fonts.ready; | ||
| setMode(recalled() === "list" ? "list" : "desk", true); | ||
| html.classList.add("ready"); | ||
| function openingView(): Cam { | ||
| return narrow() ? shelfView("home") : fitBox(everything()); | ||
| } | ||
| start(); | ||
|
|
||
| // No waiting for web fonts: the desk shows as soon as the script runs (it holds the page's | ||
| // largest image), and the ResizeObserver above re-measures items as the fonts swap in. | ||
| setMode(recalled() === "list" ? "list" : "desk", true); | ||
| html.classList.add("ready"); | ||
| // Once the fonts have swapped in, frame the desk again from the real sizes, unless the visitor | ||
| // has already moved, a site is open, or the intro is still about to compute it anyway. | ||
| document.fonts.ready.then(() => { | ||
| if (!html.classList.contains("desk") || !untouched || introTimer || liveFrom) return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Reset it inside the timeout callback: introTimer = window.setTimeout(() => { introTimer = 0; go(overview(), { stiffness: 2.2 }); }, 650);
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 533abf0. |
||
| // Measure here rather than wait for the ResizeObserver, which may not have run yet. | ||
| measure(); | ||
| drawMinimap(); | ||
| go(openingView(), { stiffness: 2.2 }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested fix: re-measure here before framing, so it doesn't depend on observer ordering: document.fonts.ready.then(() => {
if (!html.classList.contains("desk") || !untouched || introTimer || liveFrom) return;
measure();
go(openingView(), { stiffness: 2.2 });
});( Minor: with the intro running, a font swap that lands after the timer has fired (
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 9fcc82c: measure() and the minimap run before the re-frame. |
||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.