HEL-822: land HEL-821 A1+A2 source onto PR#4 base (db571ba)

- 13 modified / 8 added / 1 deleted per CHANGES.json (author package 76bc120b)
- docs/requirements v1.10 + prototypes/hel-799 + PROVENANCE from bundle, verified byte-identical to 63799f0
- AGENTS.md + scripts/check_requirements.py restored from governance commit 63799f0

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
白栖知
2026-10-24 06:10:53 +08:00
co-authored by multica-agent
parent db571ba204
commit 003d8c2cae
78 changed files with 11055 additions and 433 deletions
+72 -9
View File
@@ -1,16 +1,79 @@
import { useState } from 'react';
import type { SceneDesc } from '../domain/model';
import { Icon } from '../shared/ui';
import { useEffect, useLayoutEffect, useRef, useState, type CSSProperties } from 'react';
import type { SceneDesc, Theme } from '../domain/model';
import { Background } from './Background';
import { RoomScene, type SpaceMode } from './roomScene';
export interface PresenterProps {
scene: SceneDesc;
space: SpaceMode;
theme: Theme;
// Bumped by keyboard shortcuts (Esc): the camera lands at once instead of travelling.
instant: number;
real?: boolean;
onClose: () => void;
closeLabel: string;
presence: string;
observation: string;
onApproach: () => void;
}
// Only consumes scene. No life writes, no synthesis, no microphone/camera APIs.
export function FallbackPresenter({ scene, onClose, closeLabel, presence, observation, real = false }: PresenterProps) {
// One shared room (HEL-799, approved 01a16810): far = her room and the rainy city, middle = her, near = your table.
// Only consumes scene. No life writes, no synthesis, no microphone/camera APIs. The two stills are reference frames,
// not her real daily video; the camera move is the approved push-in, not body motion.
export function Presenter({ scene, space, theme, instant, real = false, observation, onApproach }: PresenterProps) {
const [failed, setFailed] = useState(false);
return <><section className="portal" aria-label="她的视窗"><div className="life">{!failed && <><img className="img-room" src="/assets/her-room.jpg" alt="卧室窗边的人物参考静帧" onError={() => setFailed(true)}/><img className="img-talk" src="/assets/her-talk.jpg" alt="同一人物的交谈近景参考静帧" onError={() => setFailed(true)}/></>}</div>{failed && <div className="empty">画面暂时没打开,文字仍可使用。</div>}<div className="tint"/><div className="doing"><span className="chip glass"><Icon name="book"/>{scene.activity}</span><span className="tag-demo">{real ? '生活尚未接通' : '演示状态'}</span></div><div className="topleft"><span className="chip">{presence}</span>{observation && <span className="chip observe show">{observation}</span>}</div><button className="close" onClick={onClose}><Icon name="back"/>{closeLabel}</button><span className="mark">示意画面 · 参考静帧</span></section><div className="mini-cap"><b>她此刻 · {scene.activity}</b>{real ? '生活尚未接通' : '来自演示生活状态'}<br />画面为示意静帧<button className="btn-text" onClick={onClose}>回到她身边</button></div></>;
const cam = useRef<HTMLDivElement>(null), room = useRef<HTMLDivElement>(null), talk = useRef<HTMLDivElement>(null), moon = useRef<HTMLElement>(null), near = useRef<HTMLDivElement>(null), hit = useRef<HTMLButtonElement>(null), rainRoom = useRef<HTMLCanvasElement>(null), rainTalk = useRef<HTMLCanvasElement>(null);
const controller = useRef<RoomScene | null>(null), first = useRef(true), lastInstant = useRef(instant);
useLayoutEffect(() => {
if (!cam.current || !room.current || !talk.current || !moon.current || !near.current || !hit.current || !rainRoom.current || !rainTalk.current) return;
const scene = new RoomScene({ cam: cam.current, room: room.current, talk: talk.current, moon: moon.current, near: near.current, hit: hit.current, rainRoom: rainRoom.current, rainTalk: rainTalk.current });
controller.current = scene;
(window as unknown as { __room?: () => unknown }).__room = () => scene.info();
return () => { scene.destroy(); controller.current = null; };
}, []);
useLayoutEffect(() => {
// First paint and keyboard jumps land directly; every other change travels on the spring.
const jump = first.current || lastInstant.current !== instant;
first.current = false; lastInstant.current = instant;
controller.current?.go(space, jump);
}, [space, instant]);
const firstTheme = useRef(true);
useLayoutEffect(() => { controller.current?.setTheme(theme, firstTheme.current); firstTheme.current = false; }, [theme]);
// The reduced-motion preference changes the camera from travelling to crossfading; redraw at rest.
useEffect(() => { const q = matchMedia('(prefers-reduced-motion: reduce)'), redo = () => controller.current?.resize(); q.addEventListener('change', redo); return () => q.removeEventListener('change', redo); }, []);
const fail = () => setFailed(true);
return <>
{failed && <Background/>}
<div className={`room ${failed ? 'failed' : ''}`} aria-hidden="true">
<div className="r-cam" ref={cam}>
<div className="plate plate-room" ref={room}><div className="po">
{!failed && <img className="photo" src="/assets/her-room.jpg" alt="" draggable={false} onError={fail}/>}
<div className="r-tint moon"><i className="night"/><i className="night2"/></div>
<div className="r-tint fog"><i className="night"/><i className="night2"/></div>
<canvas className="r-rain" ref={rainRoom}/>
<i className="r-mist m1"/><i className="r-mist m2"/>
<i className="flame-glow" style={{ left: 931, top: 500 }}/>
<i className="spill"/>
</div></div>
<div className="plate plate-talk" ref={talk}><div className="po">
{!failed && <img className="photo" src="/assets/her-talk.jpg" alt="" draggable={false} onError={fail}/>}
<div className="r-tint moon"><i className="night"/><i className="night2"/><i className="beam"/></div>
<div className="r-tint fog"><i className="night"/><i className="night2"/><i className="beam"/></div>
<canvas className="r-rain" ref={rainTalk}/>
<i className="r-mist m1"/><i className="r-mist m2"/>
<i className="flame-glow" style={{ left: 89, top: 738 }}/><i className="flame-glow" style={{ left: -12, top: 704 }}/>
<i className="spill"/>
{[[1180, 330, 17, -3], [1290, 240, 21, -9], [1400, 420, 19, -14], [1230, 520, 24, -6]].map(([x, y, d, dl]) => <i key={x} className="mote" style={{ '--x': `${x}px`, '--y': `${y}px`, '--d': `${d}s`, '--dl': `${dl}s` } as CSSProperties}/>)}
</div></div>
<i className="r-moon" ref={moon}/>
</div>
<div className="r-near" ref={near}>
<canvas className="near-c" data-theme-layer="moon"/><canvas className="near-c" data-theme-layer="fog"/>
<i className="steam s1"/><i className="steam s2"/><i className="candle"/>
</div>
<div className="r-vignette"/><div className="r-veil"/>
</div>
<button className="her-hit" ref={hit} aria-label="走近她,开始交谈" tabIndex={space === 'home' ? 0 : -1} disabled={space !== 'home'} onClick={onApproach}><span>和她说话</span></button>
<div className="room-labels">
{failed && <span className="chip glass">画面暂时没打开,文字仍可使用。</span>}
{observation && <span className="chip glass observe show">{observation}</span>}
</div>
<p className="ref-note" data-activity={scene.activity}>{real ? '生活尚未接通' : '演示状态'} · 人物为旧参考静帧 · 不代表新日常 · 未接入真实动态</p>
</>;
}
+171
View File
@@ -0,0 +1,171 @@
import type { Theme } from '../domain/model';
// Ambient life of the room (HEL-799 ambient.js): drawn, never taken from her photo, never a weather claim.
// Rain falls only inside each still's window panes; city windows blink; a few drops slide down the glass.
const COLORS: Record<Theme, number[]> = { moon: [190, 210, 255], fog: [228, 204, 255] };
// Plate px. clip: the glass; city: where lit windows may blink.
const CFG = {
room: { clip: [655, 0, 545, 545], city: [660, 215, 530, 320], n: 110, seed: 11 },
talk: { clip: [0, 0, 640, 600], city: [0, 200, 640, 360], n: 150, seed: 7 },
} as const;
type PlateKey = keyof typeof CFG;
interface Streak { x: number; y: number; len: number; spd: number; a: number }
interface Light { x: number; y: number; p: number; f: number; warm: boolean }
interface Slide { x: number; y: number; spd: number; r: number }
interface Layer { ctx: CanvasRenderingContext2D; streaks: Streak[]; lights: Light[]; slides: Slide[]; drops: HTMLCanvasElement; scale: number }
const rng = (seed: number) => () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const sstep = (k: number) => k * k * (3 - 2 * k);
export class Rain {
private layers: Partial<Record<PlateKey, Layer>> = {};
private mix = 0; private mixFrom = 0; private mixTo = 0; private mixStart = 0;
private visible: Record<PlateKey, boolean> = { room: true, talk: false };
private dirty = true; private lastDraw = 0; private res = 0;
constructor(private canvases: Record<PlateKey, HTMLCanvasElement>) {}
private build(name: PlateKey, scale: number) {
const c = CFG[name], cv = this.canvases[name], r = rng(c.seed), ctx = cv.getContext('2d');
if (!ctx) return;
cv.width = 1600 * scale; cv.height = 1000 * scale;
const [x, y, w, h] = c.clip;
const streaks = Array.from({ length: c.n }, () => ({ x: x + r() * (w + 60), y: y + r() * h, len: 10 + r() * 24, spd: 520 + r() * 420, a: .07 + r() * .17 }));
const lights = Array.from({ length: 30 }, () => ({ x: c.city[0] + r() * c.city[2], y: c.city[1] + r() * c.city[3], p: r() * 6.28, f: .25 + r() * .7, warm: r() > .35 }));
const slides = Array.from({ length: 4 }, () => ({ x: x + r() * w, y: y + r() * h, spd: 6 + r() * 14, r: 1.6 + r() * 1.4 }));
// Static droplets on the glass, rendered once.
const drops = document.createElement('canvas'); drops.width = cv.width; drops.height = cv.height;
const d = drops.getContext('2d');
if (d) {
d.scale(scale, scale);
for (let i = 0; i < 70; i++) {
const px = x + r() * w, py = y + r() * h, rr = 1 + r() * 2.4, g = d.createRadialGradient(px - rr * .3, py - rr * .3, 0, px, py, rr);
g.addColorStop(0, 'rgba(230,240,255,.45)'); g.addColorStop(1, 'rgba(160,190,255,0)');
d.fillStyle = g; d.beginPath(); d.arc(px, py, rr, 0, 7); d.fill();
}
}
this.layers[name] = { ctx, streaks, lights, slides, drops, scale };
}
private paint(name: PlateKey, dt: number, now: number) {
const s = this.layers[name]; if (!s) return;
const { ctx, scale } = s, [x, y, w, h] = CFG[name].clip;
const col = COLORS.moon.map((m, i) => Math.round(m + (COLORS.fog[i] - m) * this.mix));
ctx.setTransform(scale, 0, 0, scale, 0, 0);
ctx.clearRect(x - 2, y - 2, w + 4, h + 4);
ctx.save(); ctx.beginPath(); ctx.rect(x, y, w, h); ctx.clip();
for (const l of s.lights) {
const a = Math.max(0, Math.sin(l.p + now / 1000 * l.f)) * .55;
if (a < .02) continue;
ctx.fillStyle = l.warm ? `rgba(255,214,150,${a.toFixed(3)})` : `rgba(${col[0]},${col[1]},${col[2]},${(a * .8).toFixed(3)})`;
ctx.fillRect(l.x, l.y, 2.2, 2.6);
}
ctx.lineWidth = .8; ctx.lineCap = 'round';
for (const k of s.streaks) {
k.y += k.spd * dt; k.x -= k.spd * dt * .18;
if (k.y > y + h) { k.y = y - k.len; k.x = x + Math.random() * (w + 60); }
ctx.strokeStyle = `rgba(${col[0]},${col[1]},${col[2]},${k.a.toFixed(3)})`;
ctx.beginPath(); ctx.moveTo(k.x, k.y); ctx.lineTo(k.x - k.len * .18, k.y + k.len); ctx.stroke();
}
ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(s.drops, 0, 0); ctx.setTransform(scale, 0, 0, scale, 0, 0);
for (const q of s.slides) {
q.y += q.spd * dt; if (q.y > y + h) { q.y = y; q.x = x + Math.random() * w; }
ctx.fillStyle = 'rgba(225,236,255,.35)'; ctx.beginPath(); ctx.arc(q.x, q.y, q.r, 0, 7); ctx.fill();
ctx.fillStyle = 'rgba(225,236,255,.08)'; ctx.fillRect(q.x - .5, q.y - 26, 1, 26);
}
ctx.restore();
}
draw(now: number, tier: number, still: boolean, hidden: boolean) {
const want = tier > 0 ? .5 : 1;
if (want !== this.res) { this.res = want; this.build('room', want); this.build('talk', want); this.dirty = true; }
if (this.mixStart) { const k = Math.min(1, (now - this.mixStart) / 1000); this.mix = this.mixFrom + (this.mixTo - this.mixFrom) * sstep(k); if (k >= 1) this.mixStart = 0; this.dirty = true; }
// Reduced motion: the rain stays one still frame.
if (still) { if (!this.dirty) return; this.dirty = false; this.paint('room', 0, 0); this.paint('talk', 0, 0); return; }
if (hidden) return;
if (tier > 1 && now - this.lastDraw < 32) return; // ~30fps on slow devices
const dt = this.lastDraw ? Math.min(.05, (now - this.lastDraw) / 1000) : 0; this.lastDraw = now;
if (this.visible.room) this.paint('room', dt, now);
if (this.visible.talk) this.paint('talk', dt, now);
}
view(talkOpacity: number) { this.visible.room = talkOpacity < .998; this.visible.talk = talkOpacity > .002; this.dirty = true; }
setTheme(theme: Theme, instant: boolean) {
this.mixTo = theme === 'fog' ? 1 : 0;
if (instant) { this.mix = this.mixTo; this.mixStart = 0; this.dirty = true; return; }
this.mixFrom = this.mix; this.mixStart = performance.now();
}
resume() { this.lastDraw = 0; }
}
// Your side of the table: drawn per theme at the viewport's own size, so the cup and candle never stretch.
const PAL = {
moon: { top: ['#3b2c30', '#221a24', '#0b0a14'], cool: '150,180,255', warm: '255,190,130', grain: ['#c9b8ff', '#ffd9b0'], rim: ['170,200,255', '240,230,255', '255,205,150'], cup: ['#5a6aa0', '#d9def2', '#9ea6c8', '#3a3f66'], handle: '#aeb6d8', bokeh: ['160,190,255', '255,200,140'] },
fog: { top: ['#3a2a3e', '#231a30', '#0e0a18'], cool: '200,170,255', warm: '255,180,175', grain: ['#e2c4ff', '#ffd2d8'], rim: ['205,180,255', '246,226,255', '255,192,176'], cup: ['#7a62a8', '#e6dcf2', '#b0a2c8', '#4a3a66'], handle: '#c2b4d8', bokeh: ['205,170,255', '255,186,200'] },
};
type Palette = typeof PAL.moon;
function anchors(W: number, H: number) {
const sx = W / 1600, sy = H / 1000, u = Math.min(1.35, Math.max(.42, Math.min(sx, sy))), phone = W <= 720 || W / H < .9;
return { sx, sy, u, cup: phone ? [34, H - 8] : [210 * sx, 985 * sy], candle: phone ? [W - 26, H - 30] : [1400 * sx, 966 * sy] };
}
function paintNear(cv: HTMLCanvasElement, pal: Palette, W: number, H: number, dpr: number) {
cv.width = Math.round(W * dpr); cv.height = Math.round(H * dpr);
const A = anchors(W, H), c = cv.getContext('2d');
if (!c) return A;
const { sx, sy, u } = A, px = (n: number) => n * dpr;
c.setTransform(dpr, 0, 0, dpr, 0, 0);
const ex = W / 2, ey = 1585 * sy, rx = 1300 * Math.max(sx, .32), ry = 775 * sy;
// Table top: one arc of a large round table, the part nearest to you.
c.save(); c.filter = `blur(${px(2.5 * u)}px)`;
c.beginPath(); c.ellipse(ex, ey, rx, ry, 0, Math.PI, 2 * Math.PI); c.lineTo(W + 100, H + 100); c.lineTo(-100, H + 100); c.closePath();
const top = c.createLinearGradient(0, ey - ry - 30 * sy, 0, H);
top.addColorStop(0, pal.top[0]); top.addColorStop(.3, pal.top[1]); top.addColorStop(1, pal.top[2]);
c.fillStyle = top; c.fill(); c.clip();
let g = c.createRadialGradient(300 * sx, 860 * sy, 0, 300 * sx, 860 * sy, 520 * u);
g.addColorStop(0, `rgba(${pal.cool},.22)`); g.addColorStop(1, `rgba(${pal.cool},0)`); c.fillStyle = g; c.fillRect(0, 0, W, H);
g = c.createRadialGradient(1260 * sx, 840 * sy, 0, 1260 * sx, 840 * sy, 560 * u);
g.addColorStop(0, `rgba(${pal.warm},.26)`); g.addColorStop(1, `rgba(${pal.warm},0)`); c.fillStyle = g; c.fillRect(0, 0, W, H);
c.globalAlpha = .06;
for (let i = 0; i < 26; i++) {
c.strokeStyle = pal.grain[i % 2]; c.lineWidth = 1;
c.beginPath(); c.ellipse(ex, ey, Math.max(10, rx - i * 9 * sx), Math.max(10, ry - (i * 6 + 14) * sy), 0, Math.PI * 1.08, Math.PI * 1.92); c.stroke();
}
c.restore();
c.save(); c.filter = `blur(${px(1)}px)`;
const rim = c.createLinearGradient(0, 0, W, 0);
rim.addColorStop(0, `rgba(${pal.rim[0]},.05)`); rim.addColorStop(.25, `rgba(${pal.rim[0]},.45)`);
rim.addColorStop(.55, `rgba(${pal.rim[1]},.3)`); rim.addColorStop(.8, `rgba(${pal.rim[2]},.55)`); rim.addColorStop(1, `rgba(${pal.rim[2]},.08)`);
c.strokeStyle = rim; c.lineWidth = 2;
c.beginPath(); c.ellipse(ex, ey, rx, ry, 0, Math.PI * 1.03, Math.PI * 1.97); c.stroke();
c.restore();
// Your cup (nearest, most out of focus). Drawn in HEL-798 units around its base.
c.save(); c.translate(A.cup[0], A.cup[1]); c.scale(u, u); c.translate(-210, -985); c.filter = `blur(${px(5 * u)}px)`;
c.fillStyle = '#0d0c18'; c.beginPath(); c.ellipse(205, 985, 150, 34, 0, 0, 7); c.fill();
const cup = c.createLinearGradient(100, 0, 320, 0);
cup.addColorStop(0, pal.cup[0]); cup.addColorStop(.35, pal.cup[1]); cup.addColorStop(.7, pal.cup[2]); cup.addColorStop(1, pal.cup[3]);
c.fillStyle = cup; c.beginPath(); c.moveTo(108, 830); c.bezierCurveTo(108, 950, 140, 985, 210, 985); c.bezierCurveTo(280, 985, 312, 950, 312, 830); c.closePath(); c.fill();
c.strokeStyle = pal.handle; c.lineWidth = 16; c.beginPath(); c.arc(320, 885, 34, -Math.PI / 2.2, Math.PI / 2.2); c.stroke();
c.fillStyle = '#3a2416'; c.beginPath(); c.ellipse(210, 830, 102, 18, 0, 0, 7); c.fill();
c.fillStyle = 'rgba(255,200,140,.35)'; c.beginPath(); c.ellipse(240, 828, 46, 7, 0, 0, 7); c.fill();
c.restore();
// Your small candle, answering the one beside her.
c.save(); c.translate(A.candle[0], A.candle[1]); c.scale(u, u); c.translate(-1400, -966); c.filter = `blur(${px(3.5 * u)}px)`;
const glass = c.createLinearGradient(1350, 0, 1450, 0);
glass.addColorStop(0, 'rgba(255,190,130,.25)'); glass.addColorStop(.5, 'rgba(255,225,180,.55)'); glass.addColorStop(1, 'rgba(255,170,110,.22)');
c.fillStyle = glass; c.beginPath(); c.moveTo(1356, 850); c.lineTo(1444, 850); c.lineTo(1440, 952); c.quadraticCurveTo(1400, 966, 1360, 952); c.closePath(); c.fill();
c.fillStyle = 'rgba(255,240,215,.35)'; c.beginPath(); c.ellipse(1400, 850, 44, 9, 0, 0, 7); c.fill();
c.restore();
// Near bokeh (depth of field).
c.save(); c.globalCompositeOperation = 'screen';
const bok: [number, number, number, number, number][] = [[60, 120, 70, 0, .1], [1530, 300, 90, 1, .09], [1480, 980, 120, 1, .1], [560, 990, 80, 0, .08], [30, 560, 60, 0, .08]];
for (const [x, y, r0, k, a] of bok) {
const r = r0 * u, X = x * sx, Y = y * sy, col = pal.bokeh[k];
g = c.createRadialGradient(X, Y, r * .6, X, Y, r);
g.addColorStop(0, `rgba(${col},${a})`); g.addColorStop(.9, `rgba(${col},${a * .8})`); g.addColorStop(1, `rgba(${col},0)`);
c.fillStyle = g; c.beginPath(); c.arc(X, Y, r, 0, 7); c.fill();
}
c.restore();
return A;
}
export function drawNear(host: HTMLElement, W: number, H: number, tier: number) {
const dpr = Math.min(tier > 0 ? 1 : 2, window.devicePixelRatio || 1);
let A: ReturnType<typeof anchors> | undefined;
for (const cv of host.querySelectorAll<HTMLCanvasElement>('canvas[data-theme-layer]')) A = paintNear(cv, PAL[cv.dataset.themeLayer as Theme], W, H, dpr);
if (!A) return;
host.style.setProperty('--u', A.u.toFixed(3));
host.style.setProperty('--steam-x', (A.cup[0] + 5 * A.u).toFixed(1) + 'px'); host.style.setProperty('--steam-y', (A.cup[1] - 330 * A.u).toFixed(1) + 'px');
host.style.setProperty('--candle-x', A.candle[0].toFixed(1) + 'px'); host.style.setProperty('--candle-y', (A.candle[1] - 116 * A.u).toFixed(1) + 'px');
}
+76
View File
@@ -0,0 +1,76 @@
// One room, one camera (HEL-799 scene.js, approved 01a16810). Pure geometry, no DOM.
// Two approved reference stills of the same person, room and clothes are aligned on her face, so the camera can
// move continuously from "sitting across the room" (room still) to "sitting across the table" (talk still, = the
// approved HEL-798 composition). Only transform and opacity move per frame; the face itself is never warped.
export type PlateName = 'room' | 'talk';
export interface Placement { s: number; o: [number, number] }
export interface CameraPose { room: Placement; talk: Placement }
// Plate px of the 1600×1000 design. FACE: centre of her face in each still. K: her face is K× larger on the talk
// plate than on the room plate (measured from eye spacing and hair width on the two stills: 3.8 native × 1.12 / 1.4).
export const FACE: Record<PlateName, [number, number]> = { room: [1275, 347], talk: [856, 315] };
export const K = 3.04;
// Picture bounds of each plate in design px (the room still is shown at 2240×1260, the talk still at 1792×1008).
export const BOUNDS: Record<PlateName, [number, number, number, number]> = { room: [-640, -112, 1600, 1148], talk: [-118, -10, 1674, 998] };
export const MOON: Record<PlateName, [number, number, number]> = { room: [1082, 98, 30], talk: [404, 92, 44] };
// Her figure on the room plate, for the tap target.
export const HER: [number, number, number, number] = [1195, 235, 1445, 700];
export const smoothstep = (a: number, b: number, x: number) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };
export const lerp = (a: number, b: number, t: number) => a + (b - a) * t;
export const isPhone = (vw: number, vh: number) => vw <= 720 || vw / vh < .9;
export class Camera {
// Overscan kept for pointer parallax.
constructor(public vw: number, public vh: number, public overscan = 12) {}
private shot(kind: PlateName) {
const phone = isPhone(this.vw, this.vh);
if (kind === 'room') return { ax: phone ? .6 : 1275 / 1600, ay: phone ? .25 : .347 };
return { ax: phone ? .5 : 856 / 1600, ay: phone ? .27 : .315 };
}
// How far a plate must shift so its picture still covers the viewport (plus parallax overscan).
private cover(s: number, o: [number, number], p: PlateName): [number, number] {
const [x0, y0, x1, y1] = BOUNDS[p], c: [number, number] = [0, 0], M = this.overscan;
const fit = (pos: number, a0: number, a1: number, size: number, i: 0 | 1) => {
const min = size + M - s * a1, max = -M - s * a0;
if (min > max) { c[i] = (min + max) / 2 - pos; return; }
if (pos < min) c[i] = min - pos; else if (pos > max) c[i] = max - pos;
};
fit(o[0], x0, x1, this.vw, 0); fit(o[1], y0, y1, this.vh, 1);
return c;
}
rest(kind: PlateName): Placement {
const sh = this.shot(kind), base = Math.max(this.vw / 1600, this.vh / 1000), f = FACE[kind];
const o: [number, number] = [sh.ax * this.vw - base * f[0], sh.ay * this.vh - base * f[1]];
const c = this.cover(base, o, kind);
return { s: base, o: [o[0] + c[0], o[1] + c[1]] };
}
// Camera at parameter x: zoom in log-space, face travels in a straight line on screen.
at(x: number): CameraPose {
const A = this.rest('room'), B = this.rest('talk');
const sRB = B.s * K;
const phA = [A.o[0] + A.s * FACE.room[0], A.o[1] + A.s * FACE.room[1]];
const phB = [B.o[0] + B.s * FACE.talk[0], B.o[1] + B.s * FACE.talk[1]];
const sR = A.s * Math.pow(sRB / A.s, x), sT = sR / K;
const ph = [lerp(phA[0], phB[0], x), lerp(phA[1], phB[1], x)];
const oR: [number, number] = [ph[0] - sR * FACE.room[0], ph[1] - sR * FACE.room[1]];
const oT: [number, number] = [ph[0] - sT * FACE.talk[0], ph[1] - sT * FACE.talk[1]];
const cR = this.cover(sR, oR, 'room'), cT = this.cover(sT, oT, 'talk'), w = smoothstep(.35, .8, x);
const c = [lerp(cR[0], cT[0], w), lerp(cR[1], cT[1], w)];
return { room: { s: sR, o: [oR[0] + c[0], oR[1] + c[1]] }, talk: { s: sT, o: [oT[0] + c[0], oT[1] + c[1]] } };
}
}
export const mapPoint = (tr: Placement, p: readonly [number, number]): [number, number] => [tr.o[0] + tr.s * p[0], tr.o[1] + tr.s * p[1]];
// Critically damped spring: interruptible, keeps its velocity when the target changes mid-way (≈0.85s to settle).
export const SPRING_W = 7.2;
export function springStep(t: number, v: number, target: number, dt: number) {
// Fixed small sub-steps keep the spring in real time even when frames arrive late.
for (let left = dt; left > 0; left -= 1 / 240) {
const h = Math.min(left, 1 / 240), a = SPRING_W * SPRING_W * (target - t) - 2 * SPRING_W * v;
v += a * h; t += v * h;
}
const settled = Math.abs(target - t) < 4e-4 && Math.abs(v) < 4e-3;
return settled ? { t: target, v: 0, settled } : { t, v, settled };
}
+126
View File
@@ -0,0 +1,126 @@
import type { Theme } from '../domain/model';
import { Camera, FACE, HER, MOON, lerp, mapPoint, smoothstep, springStep, type CameraPose, type Placement } from './camera';
import { Rain, drawNear } from './ambient';
// Where you are in the shared space. home: across the room; talk/record: across the table; pane: the room steps back.
export type SpaceMode = 'home' | 'talk' | 'record' | 'pane';
export interface RoomElements {
cam: HTMLElement; room: HTMLElement; talk: HTMLElement; moon: HTMLElement; near: HTMLElement; hit: HTMLElement;
rainRoom: HTMLCanvasElement; rainTalk: HTMLCanvasElement;
}
const reducedQuery = () => matchMedia('(prefers-reduced-motion: reduce)');
export const motionOff = () => reducedQuery().matches || document.documentElement.classList.contains('reduced');
const put = (el: HTMLElement, tr: Placement) => { el.style.transform = `translate3d(${tr.o[0].toFixed(2)}px,${tr.o[1].toFixed(2)}px,0) scale(${tr.s.toFixed(5)})`; };
// Imperative per-frame controller; React owns the DOM, this only writes transform/opacity/visibility on it.
export class RoomScene {
private camera = new Camera(innerWidth, innerHeight);
private rain: Rain;
// t: camera (0 = across the room, 1 = across the table). b: how much of the close still shows (reduced motion).
private t = 0; private v = 0; private target = 0; private b = 0; private bFrom = 0; private bStart = 0; private settled = true;
private maskOn: boolean | null = null; private tier = 0; private forced = false; private samples: number[] = [];
private last = 0; private raf = 0; private alive = true;
private fine = matchMedia('(hover: hover) and (pointer: fine)');
constructor(private el: RoomElements) {
this.rain = new Rain({ room: el.rainRoom, talk: el.rainTalk });
addEventListener('pointermove', this.pointer, { passive: true });
addEventListener('resize', this.resize);
document.addEventListener('visibilitychange', this.visibility);
const tier = new URLSearchParams(location.search).get('tier');
if (tier && /^[0-2]$/.test(tier)) this.setTier(+tier, true);
this.resize();
this.raf = requestAnimationFrame(this.frame);
}
destroy() {
this.alive = false; cancelAnimationFrame(this.raf);
removeEventListener('pointermove', this.pointer); removeEventListener('resize', this.resize);
document.removeEventListener('visibilitychange', this.visibility);
}
go(mode: SpaceMode, instant = false) {
const nt = mode === 'talk' || mode === 'record' ? 1 : 0;
if (instant) { this.t = this.target = this.b = nt; this.v = 0; this.settled = true; this.apply(); return; }
if (nt === this.target && this.settled) return;
this.bFrom = this.b; this.bStart = performance.now();
this.target = nt; this.settled = false;
}
setTheme(theme: Theme, instant: boolean) { this.rain.setTheme(theme, instant || motionOff()); }
resize = () => {
this.camera.vw = innerWidth; this.camera.vh = innerHeight;
drawNear(this.el.near, innerWidth, innerHeight, this.tier); this.apply();
};
// Review hook (same as the prototype's __proto.scene): exposes where her face is and how far the move has got.
info() {
const pose = motionOff() ? { talk: this.camera.rest('talk') } : this.camera.at(this.t), s = pose.talk.s;
const cx = pose.talk.o[0] + s * FACE.talk[0], cy = pose.talk.o[1] + s * FACE.talk[1];
return { t: +this.t.toFixed(4), target: this.target, settled: this.settled, tier: this.tier, face: { x: +cx.toFixed(1), y: +cy.toFixed(1) }, head: { left: cx - 200 * s, right: cx + 200 * s, top: cy - 300 * s, bottom: cy + 140 * s }, talkOpacity: +this.el.talk.style.opacity };
}
private visibility = () => { this.last = 0; this.samples = []; this.rain.resume(); };
// Pointer parallax (fine pointers only): far things move a little, your table the other way.
private pointer = (e: PointerEvent) => {
if (!this.fine.matches || motionOff() || e.pointerType !== 'mouse') return;
const px = e.clientX / innerWidth - .5, py = e.clientY / innerHeight - .5;
this.el.cam.style.transform = `translate3d(${(-px * 16).toFixed(1)}px,${(-py * 10).toFixed(1)}px,0)`;
this.el.near.style.setProperty('translate', `${(px * 22).toFixed(1)}px ${(py * 10).toFixed(1)}px`);
};
private step(dt: number) {
if (motionOff()) {
this.t = this.target; this.v = 0;
const k = Math.min(1, (performance.now() - this.bStart) / 200);
this.b = lerp(this.bFrom, this.target, k); this.settled = k >= 1;
return;
}
const next = springStep(this.t, this.v, this.target, dt);
this.t = next.t; this.v = next.v; this.settled = next.settled; this.b = this.t;
}
private apply() {
const off = motionOff(), el = this.el;
// Reduced motion: both stills stay at rest and simply crossfade.
const pose: CameraPose = off ? { room: this.camera.rest('room'), talk: this.camera.rest('talk') } : this.camera.at(this.t);
put(el.room, pose.room); put(el.talk, pose.talk);
const blend = off ? this.b : this.t, useMask = !off && this.tier < 2;
const op = useMask ? smoothstep(.08, .45, blend) : smoothstep(off ? 0 : .3, off ? 1 : .8, blend);
el.talk.style.opacity = op.toFixed(3);
el.talk.style.visibility = op < .002 ? 'hidden' : 'visible';
el.room.style.visibility = blend > .999 ? 'hidden' : 'visible';
// The close still opens out from her face: a soft-edged circle that grows until it reaches the corners,
// intersected with soft picture edges that tighten to nothing as the picture reaches the screen edges.
// (Mask coordinates are in the plate box, which starts at the picture's corner: face + (118, 10).)
const wantMask = useMask && blend < .985 && op > .002, s = el.talk.style;
if (wantMask) {
const r = 150 + 1450 * smoothstep(.1, .97, blend), e = (260 * (1 - smoothstep(.7, .985, blend))).toFixed(0);
const edge = (d: string) => `linear-gradient(${d},transparent 0,#000 ${e}px,#000 calc(100% - ${e}px),transparent 100%)`;
const m = `radial-gradient(circle at ${FACE.talk[0] + 118}px ${FACE.talk[1] + 10}px,#000 ${r.toFixed(0)}px,transparent ${(r + 520).toFixed(0)}px),${edge('90deg')},${edge('180deg')}`;
s.setProperty('-webkit-mask-image', m); s.setProperty('mask-image', m);
if (!this.maskOn) { s.setProperty('-webkit-mask-composite', 'source-in, source-in'); s.setProperty('mask-composite', 'intersect'); }
this.maskOn = true;
} else if (this.maskOn !== false) { s.setProperty('-webkit-mask-image', 'none'); s.setProperty('mask-image', 'none'); this.maskOn = false; }
// The moon is far away: it drifts less than the room and hands over between the two windows.
const mw = off ? (this.b > .5 ? 1 : 0) : smoothstep(.2, .8, this.t);
const mr = mapPoint(pose.room, [MOON.room[0], MOON.room[1]]), mt = mapPoint(pose.talk, [MOON.talk[0], MOON.talk[1]]);
const size = lerp(MOON.room[2] * pose.room.s, MOON.talk[2] * pose.talk.s, mw);
el.moon.style.transform = `translate3d(${lerp(mr[0], mt[0], mw).toFixed(1)}px,${lerp(mr[1], mt[1], mw).toFixed(1)}px,0) scale(${(size / 44).toFixed(4)})`;
// Tap target over her (only reachable while you are across the room).
const a = mapPoint(pose.room, [HER[0], HER[1]]), z = mapPoint(pose.room, [HER[2], HER[3]]);
Object.assign(el.hit.style, { left: a[0] + 'px', top: a[1] + 'px', width: (z[0] - a[0]) + 'px', height: (z[1] - a[1]) + 'px' });
this.rain.view(op);
}
// Frame loop + automatic quality tiers (average frame interval while settled).
private frame = (now: number) => {
if (!this.alive) return;
const dt = this.last ? Math.min(.12, (now - this.last) / 1000) : 0; this.last = now;
if (!this.settled) { this.step(dt); this.apply(); }
this.rain.draw(now, this.tier, motionOff(), document.hidden);
if (!this.forced && this.settled && dt && !document.hidden) {
this.samples.push(dt); if (this.samples.length > 120) this.samples.shift();
if (this.samples.length === 120) {
const avg = this.samples.reduce((x, y) => x + y) / 120;
if (avg > .026 && this.tier < 2) { this.setTier(this.tier + 1); this.samples = []; }
}
}
this.raf = requestAnimationFrame(this.frame);
};
private setTier(n: number, force = false) {
this.tier = n; this.forced = this.forced || force; document.documentElement.dataset.tier = String(n);
drawNear(this.el.near, innerWidth, innerHeight, n); this.apply();
}
}