mirror of
https://github.com/hanzoai/site.git
synced 2026-11-20 21:21:13 +00:00
Part of hanzo-inc/app#445. - Launch (every model page): name, line, tags and ask rise in by CSS from the first paint; ready stops waiting for the poster after 2.5 s; each body child hydrates in its own Suspense boundary. - Opener: touch screens, Data Saver and reduced motion navigate plainly; a card picture not in hand within 400 ms navigates at once; a failed flight leaves no plane, veil or stopped scroll. - Mori viewport: mesh built on its first frame, 48 x 18 points on a phone or touch screen. - useLoop: Data Saver draws one still frame. - hanzo.css linked with its content hash, since the edge serves .css immutable. Testing: pnpm test 58/58, pnpm build + postbuild module test, cicd / test-amd64 pass; local export checked in headless Chrome at 375 px (words at first paint, plain tap navigation, no new console errors). Risk: low; hero entrance timing and the card transition on touch screens change, content does not. How to revert: revert this commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
226 lines
14 KiB
TypeScript
226 lines
14 KiB
TypeScript
'use client';
|
|
/* Launch: the v2 model-launch shell for Hanzo's named models (/enso /kai /zen /iro /koma /oto /koe /uta /mori).
|
|
Owned by the Zen-names chat (wave 2, group 12). One file per piece; x-cray/Chapters.jsx is its partner.
|
|
|
|
Usage (a page's body file):
|
|
<Launch slug="enso" shape={ring} head={<><Lines/>, lede, actions…</>} headClass="eo-head">
|
|
{…chapters, demo, chart, spec, <ModelCards slug="enso" />, <Band … />}
|
|
</Launch>
|
|
- Stage: the model card's film lands full-bleed (the WebGL opener's contract: data-hero={slug}, then data-ready once
|
|
the poster decodes, or after READY_MS if it never does). On wide screens it settles into the page's own shape as you
|
|
scroll (a pinned scene); on phones nothing pins: it settles by itself a moment after arrival. Clip-path, pan and
|
|
zoom only. The page's headline then rises beside it, line by line. No eyebrow above the h1 (JAMES.md).
|
|
- The name, its line, its tags and the ask rise in by CSS from the first paint (Launch.css): they never wait for the
|
|
page's script. They used to wait for hydration and the poster, which on a phone was seconds of bare film with no
|
|
words and a page that ignored taps (hanzo-inc/app#445).
|
|
- The body hydrates apart from the stage: each child of Launch is its own Suspense boundary, so React hydrates them in
|
|
separate tasks after the stage and a tap is answered between them, not after the whole page.
|
|
- shape(t, W, H, wide) returns a CSS clip-path for t in 0..1 (0 = full-bleed). Helpers below: ring, box, poly.
|
|
- over(W, H, wide) (optional) draws a layer over the settled film (e.g. a cube's edges); it fades in with the headline.
|
|
- pos (optional): the film's object-position, e.g. '50% 88%' when the subject sits low in Hanzo's 4:5 frame.
|
|
- aim(W, H, wide) (optional) returns [dx, dy, zoom?]: how far the camera pans (and zooms, default 1.14) the film by
|
|
the time it settles, so the subject sits inside the shape. focus(fx, fy, tx, ty, z) works it out for you.
|
|
- ask = { label, to, id }: the page's one solid ask, in Hanzo's words, on the very first screen (white on the film);
|
|
the headline beside the settled film carries the same ask, so one solid button shows at a time.
|
|
- Colour: none of our own (JAMES.md 7, 0 UI hues). The film is Hanzo's own, never filtered or dimmed; the only
|
|
dimming is the gradient under the name. --ac / --ac-ink stay as neutral ink tokens for the page files.
|
|
- One solid fill: Hanzo's sticky header carries the solid "Try Hanzo" (MASTER 3.2 rule 6), so the page's own asks
|
|
are quiet (outline) and their seconds are links. The phone bar (Ask.jsx) carries the page's own ask where it covers nothing.
|
|
- One pause (the shared FilmPause, bottom right of the stage) stops every film and self-running demo on the page.
|
|
- Close runs the film back into the card it opened from. Reduced motion and JS off share one calm layout: the full
|
|
film with the name and the ask, then the headline, lede and actions below it, nothing pinned or hidden. */
|
|
import { Children, Suspense, useEffect, useRef, useState } from 'react';
|
|
import type { CSSProperties, ReactNode } from 'react';
|
|
import { motion, animate, useScroll, useTransform, useMotionValue, useMotionValueEvent, useReducedMotion, useInView } from 'motion/react';
|
|
import { EASE } from '../ui';
|
|
import { Img, Cta } from '../kit';
|
|
import ModelFilm, { FilmPause } from './ModelFilm';
|
|
import { useOpener } from '../Opener';
|
|
import { bySlug, media } from '../../lib/models';
|
|
import '../pages/model/Model.css';
|
|
import './Launch.css';
|
|
|
|
// each model's own film, exactly as Hanzo publishes it
|
|
export const film = (slug: string, ext: string) => media(slug, ext);
|
|
export const lerp = (a: number, b: number, t: number) => a + (b - a) * t;
|
|
export const clamp01 = (t: number) => Math.min(1, Math.max(0, t));
|
|
const settle = (t: number) => { const x = clamp01(t); return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2; };
|
|
|
|
// the poster is preloaded at high priority and normally decodes long before this; if it does not (a stalled request,
|
|
// a decode that never settles), the stage stops waiting for it and settles on the black ground
|
|
const READY_MS = 2500;
|
|
|
|
type Point = readonly [number, number];
|
|
/* a clip-path for t in 0..1 (0 = full-bleed) on a stage W by H px; wide is the desktop layout */
|
|
export type Shape = (t: number, W: number, H: number, wide: boolean) => string;
|
|
/* [dx, dy, zoom?] for the layout, or null for no pan */
|
|
export type Aim = (W: number, H: number, wide: boolean) => readonly number[] | null;
|
|
interface Size { W: number; H: number; wide: boolean }
|
|
|
|
/* shape helpers: each returns a function (t, W, H, wide) => clip-path */
|
|
// a circle that closes from beyond the corners onto (cx, cy) with radius r (px), per layout
|
|
export const ring = (at: (W: number, H: number, wide: boolean) => { x: number; y: number; r: number }): Shape => (t, W, H, wide) => {
|
|
const { x, y, r } = at(W, H, wide);
|
|
const far = Math.hypot(W, H);
|
|
return `circle(${lerp(far, r, t).toFixed(1)}px at ${lerp(W / 2, x, t).toFixed(1)}px ${lerp(H / 2, y, t).toFixed(1)}px)`;
|
|
};
|
|
// a rounded rectangle that closes onto {l, t, w, h, r} (px)
|
|
export const box = (at: (W: number, H: number, wide: boolean) => { t: number; l: number; w: number; h: number; r: number | string }): Shape => (t, W, H, wide) => {
|
|
const b = at(W, H, wide);
|
|
const top = lerp(0, b.t, t), left = lerp(0, b.l, t), right = lerp(0, W - b.l - b.w, t), bottom = lerp(0, H - b.t - b.h, t);
|
|
const r = typeof b.r === 'string' ? b.r : `${lerp(0, b.r, t).toFixed(1)}px`;
|
|
return `inset(${top.toFixed(1)}px ${right.toFixed(1)}px ${bottom.toFixed(1)}px ${left.toFixed(1)}px round ${r})`;
|
|
};
|
|
// any polygon: pts(W, H, wide) gives the settled outline in px (clockwise); the opening frame projects every point
|
|
// out from the outline's centre to well beyond the screen, so t = 0 covers it all and every frame between is the
|
|
// same polygon, which clip-path can interpolate
|
|
export const poly = (pts: (W: number, H: number, wide: boolean) => readonly Point[]): Shape => (t, W, H, wide) => {
|
|
const P = pts(W, H, wide);
|
|
const cx = P.reduce((s, p) => s + p[0], 0) / P.length, cy = P.reduce((s, p) => s + p[1], 0) / P.length;
|
|
const M = Math.max(W, H) * 0.6;
|
|
const out = P.map(([x, y]) => {
|
|
const dx = x - cx || 0.001, dy = y - cy || 0.001;
|
|
const k = Math.min(Math.abs(((dx > 0 ? W + M : -M) - cx) / dx), Math.abs(((dy > 0 ? H + M : -M) - cy) / dy));
|
|
const fx = cx + dx * k, fy = cy + dy * k;
|
|
return `${lerp(fx, x, t).toFixed(1)}px ${lerp(fy, y, t).toFixed(1)}px`;
|
|
});
|
|
return `polygon(${out.join(',')})`;
|
|
};
|
|
// aim helper: the film's subject sits at (fx, fy) of the 4:5 frame (fractions); land it on (tx, ty) px at zoom z.
|
|
// box = [l, t, r, b], the settled shape's bounds in px: the pan is held back so the zoomed film always covers it.
|
|
// pos = the film's object-position as fractions (Launch's pos prop), when the page moves it off centre.
|
|
// Returns [dx, dy, z] for Launch's aim.
|
|
export const focus = (fx: number, fy: number, tx: number, ty: number, z = 1.14, box?: readonly [number, number, number, number], pos: Point = [0.5, 0.5]) => (W: number, H: number): [number, number, number] => {
|
|
const s0 = Math.max(W / 1200, H / 1500), iw = 1200 * s0, ih = 1500 * s0;
|
|
const px = fx * iw - (iw - W) * pos[0], py = fy * ih - (ih - H) * pos[1], cx = W / 2, cy = H / 2;
|
|
let dx = tx - (cx + (px - cx) * z), dy = ty - (cy + (py - cy) * z);
|
|
if (box) {
|
|
dx = Math.min(Math.max(dx, box[2] - cx - cx * z), box[0] - cx + cx * z);
|
|
dy = Math.min(Math.max(dy, box[3] - cy - cy * z), box[1] - cy + cy * z);
|
|
}
|
|
return [dx, dy, z];
|
|
};
|
|
// densify a closed outline so the opening frame stays a full cover (n points per edge)
|
|
export const dense = (V: readonly Point[], n = 6) => V.flatMap((a, i) => { const b = V[(i + 1) % V.length]; return Array.from({ length: n }, (_, k): Point => [lerp(a[0], b[0], k / n), lerp(a[1], b[1], k / n)]); });
|
|
|
|
// a headline whose lines rise out of a mask, staggered, when its parent says so ([data-on]), or by itself the first
|
|
// time it scrolls into view (reveal)
|
|
interface LinesProps {
|
|
as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
|
|
id?: string;
|
|
className?: string;
|
|
lines: readonly ReactNode[];
|
|
reveal?: boolean;
|
|
}
|
|
|
|
export function Lines({ as: T = 'h2', id, className = '', lines, reveal }: LinesProps) {
|
|
const ref = useRef<HTMLHeadingElement>(null);
|
|
const seen = useInView(ref, { once: true, amount: 0.6 });
|
|
return (
|
|
<T id={id} ref={ref} className={'lv-lines ' + className} data-on={reveal && seen ? '' : undefined}>
|
|
{lines.map((l, i) => <span key={i} className="lv-ln"><span style={{ '--i': i } as CSSProperties}>{l}</span></span>)}
|
|
</T>
|
|
);
|
|
}
|
|
|
|
interface LaunchProps {
|
|
slug: string;
|
|
shape: Shape;
|
|
aim?: Aim;
|
|
over?: (W: number, H: number, wide: boolean) => ReactNode;
|
|
head?: ReactNode;
|
|
headClass?: string;
|
|
label?: string;
|
|
ask?: { label: string; to: string; id: string };
|
|
pos?: string;
|
|
children?: ReactNode;
|
|
}
|
|
|
|
export default function Launch({ slug, shape, aim, over, head, headClass = '', label, ask, pos, children }: LaunchProps) {
|
|
const m = bySlug[slug];
|
|
const opener = useOpener();
|
|
const stage = useRef<HTMLElement>(null), pin = useRef<HTMLDivElement>(null);
|
|
const [ready, setReady] = useState(false);
|
|
const [on, setOn] = useState(false);
|
|
const [size, setSize] = useState<Size | null>(null);
|
|
const calm = useReducedMotion();
|
|
const [still, setStill] = useState(false); // set after mount: the server cannot know, and hydration keeps its attributes
|
|
useEffect(() => setStill(!!calm), [calm]);
|
|
const narrow = size ? !size.wide : false;
|
|
|
|
// wide: the scroll drives the settle; narrow: time does (a moment after arrival), so phones never pin
|
|
const { scrollYProgress: p } = useScroll({ target: stage, offset: ['start 60px', 'end end'] });
|
|
const tv = useMotionValue(0);
|
|
const mode = useRef({ narrow: false, still: false });
|
|
mode.current = { narrow, still };
|
|
const t = useTransform([p, tv], ([a, b]: number[]) => (mode.current.still ? 0 : mode.current.narrow ? b : clamp01(a / 0.8))); // reduced motion: the calm layout (film with the name, then the headline below)
|
|
useEffect(() => {
|
|
if (!ready || !narrow || still) return;
|
|
const c = animate(tv, 1, { duration: 1.5, ease: EASE, delay: 1.1 });
|
|
return () => c.stop();
|
|
}, [ready, narrow, still, tv]);
|
|
const clip = useTransform(t, (v) => (!size || v <= 0.002 ? 'none' : shape(settle(v), size.W, size.H, size.wide)));
|
|
const zoom = useTransform(t, (v) => { if (!size) return 1; const a = aim ? aim(size.W, size.H, size.wide) : null; return 1 + (((a && a[2]) || 1.14) - 1) * v; });
|
|
// aim: the camera pans the film so its subject sits inside the settled shape ([dx, dy] px at t = 1, per layout)
|
|
const pan = (k: number) => (v: number) => { if (!size || !aim) return 0; const a = aim(size.W, size.H, size.wide); return (a ? a[k] : 0) * settle(v); };
|
|
const panX = useTransform(t, pan(0));
|
|
const panY = useTransform(t, pan(1));
|
|
const nameO = useTransform(t, [0, 0.28], [1, 0]);
|
|
const nameY = useTransform(t, [0, 0.28], [0, -72]);
|
|
|
|
useEffect(() => {
|
|
const img = stage.current?.querySelector<HTMLImageElement>('.lv-film img');
|
|
if (!img || !pin.current) return;
|
|
const done = () => setReady(true);
|
|
const late = setTimeout(done, READY_MS);
|
|
if (img.complete && img.naturalWidth > 0) done();
|
|
else if (img.complete) img.decode().then(done, done);
|
|
else { img.addEventListener('load', done, { once: true }); img.addEventListener('error', done, { once: true }); }
|
|
const ro = new ResizeObserver(([e]) => { const r = e.contentRect; setSize({ W: r.width, H: r.height, wide: r.width > 900 }); });
|
|
ro.observe(pin.current);
|
|
return () => { ro.disconnect(); clearTimeout(late); };
|
|
}, []);
|
|
useEffect(() => { if (still || t.get() > 0.7) setOn(true); }, [still, t, size]);
|
|
const [gone, setGone] = useState(false);
|
|
useEffect(() => { if (still) setGone(false); }, [still]);
|
|
useMotionValueEvent(t, 'change', (v) => { setGone(v > 0.2); if (v > 0.7) setOn(true); else if (v < 0.45) setOn(false); }); // back up the stage, the headline steps aside for the name
|
|
|
|
return (
|
|
<main className="x-model lv" data-cursor="" style={pos ? { '--fpos': pos } as CSSProperties : undefined} data-calm={still ? '' : undefined} data-narrow={narrow ? '' : undefined}>
|
|
<section className="lv-stage" ref={stage} data-hero={slug} data-ready={ready ? '' : undefined} aria-label={label || m.name}>
|
|
<div className="lv-pin" ref={pin}>
|
|
<motion.div className="lv-film" style={{ clipPath: clip }}>
|
|
<motion.div className="lv-film-in" style={{ scale: zoom, x: panX, y: panY }}>
|
|
<Img src={film(slug, 'webp')} w={1200} h={1500} priority />
|
|
<ModelFilm slug={slug} className="lv-film-v" />
|
|
</motion.div>
|
|
<motion.i className="lv-scrim" style={{ opacity: nameO }} aria-hidden="true" />
|
|
</motion.div>
|
|
<motion.div className="lv-name x-wrap" style={{ opacity: nameO, y: nameY }} data-gone={gone ? '' : undefined}>
|
|
<h1 className="lv-name-h">
|
|
<span className="x-line"><span className="x-line-in">{m.name}</span></span>
|
|
</h1>
|
|
<p className="lv-name-d">{m.desc}</p>
|
|
<p className="x-mtags static lv-name-t">{m.tags.map((x) => <span key={x}>{x}</span>)}</p>
|
|
{ask && <p className="lv-name-ask"><Cta to={ask.to} kind="quiet" id={ask.id}>{ask.label}</Cta></p>}
|
|
</motion.div>
|
|
{over && size && <div className="lv-over" data-on={on ? '' : undefined} aria-hidden="true">{over(size.W, size.H, size.wide)}</div>}
|
|
<div className={'lv-head ' + headClass} data-on={on ? '' : undefined}>{head}</div>
|
|
<motion.a
|
|
href="/"
|
|
className="x-mclose lv-close"
|
|
aria-label="Close and return to all models"
|
|
onClick={(e) => opener?.close(e, { key: slug, src: film(slug, 'webp') })}
|
|
initial={{ opacity: 0, scale: 0.6 }} animate={ready ? { opacity: 1, scale: 1 } : {}} transition={{ duration: 0.6, ease: EASE, delay: 0.5 }}
|
|
whileHover={{ rotate: 90 }} whileTap={{ scale: 0.9 }}
|
|
>
|
|
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M3.5 3.5l9 9M12.5 3.5l-9 9" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" /></svg>
|
|
</motion.a>
|
|
<FilmPause className="dark lv-pause" />
|
|
</div>
|
|
</section>
|
|
<div className="x-model-body lv-body">{Children.map(children, (c) => <Suspense>{c}</Suspense>)}</div>
|
|
</main>
|
|
);
|
|
}
|