Files
personal/components/dom/Header.tsx
T
virtheart 6b1f329231 feat: 移植 hyper-space 超空间区块(cursor.glb 条纹箭头 + 8vh 滚动叙事)与箭头全屏白模式
- HyperspaceModel.tsx:1:1 移植原站 cx——cursor.glb 合并几何、屏幕矩形追踪(12 帧缓存+滚动增量修正)、restScale 0.1→峰值 hypot(vw,vh)×1.64/radius 的吞噬缩放(damp 32/s)、shrink 折叠、条纹 shader(uStripeReveal 蓝底→黑底星空放射)、scaleSpinDegrees 180 收拢自旋、指针跟随高光
- HyperSpaceSection.tsx:8 视口高 + sticky,段位推进 seg0-primary/secondary(Innovate with purpose / human touch)→ seg1(进度环 + 四段主张)→ end(FUTURE-FIRST/ALWAYS);逐字符哈希延迟 stagger(hsstFadeIn/Out)
- arrowFullscreen.ts:原站 module 1111 进度 store(setDampedScaleT 阈值翻转订阅,header 0.5 阈值白模式、section 0.5 白模式同步)
- Header:箭头过阈值后整体转白(ARROW_FULLSCREEN_DOM_COLOR_TRANSITION)
- 修复:main 不再是滚动容器(原站 sticky 依赖内部滚动,我们的 Lenis 是窗口级,sticky 永不钉住导致末段文案不出现)
- scrollBus 增加 subscribeScroll/notifyScroll 供 DOM 侧响应式读取;useLightController 从 GlassModels 导出复用
- 验证:glError=0 无反馈环;段位推进/白模式触发与回落/区块顺序 全部 CDP 行为探针通过;build+tsc 通过
2026-09-01 17:38:56 +08:00

356 lines
13 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import ScrambleReveal from "@/components/dom/ScrambleReveal";
import { useShellMedia, type ThemeSetting } from "@/lib/shellMedia";
import { useBgm } from "@/lib/useBgm";
import { scrollEnv } from "@/lib/scrollEnv";
import {
useArrowFullscreenPastThreshold,
ARROW_FULLSCREEN_DOM_COLOR_TRANSITION,
} from "@/lib/arrowFullscreen";
/**
* Header — ported from the original (chunk 2689132c4e070b68.js):
*
* - THEME[L]/THEME[D]/THEME[A]: click cycles light→dark→system; global
* keyboard [L]/[D]/[A] (original ASCIIThemeToggle; theme rendering is
* light-only in this port, the setting is stored).
* - SOUND[|/-\] / SOUND[·]: background music (/bgm.mp3, loop, 0.35);
* spinner chars cycle at 130ms while playing; keyboard [S]
* (original ASCIISoundToggle); choice persisted.
* - Hovering THEME/SOUND increments a counter that reveals the fixed
* nav-hint panel (top-24): keyboard help + browser name + viewport
* size. Pointer-down or any shortcut key resets it; changing
* theme/sound also hides it (original P counter logic).
* - Bottom row: live clock, pointer coordinate readout (click → scroll
* to top), and — replacing the original RotatingGlobe — the
* VIRTHEART copyright line.
* - Keyboard [T] scroll to top, [B] scroll to bottom.
*/
const SPINNER = ["|", "/", "-", "\\"];
const dotted =
"relative before:content-[''] before:absolute before:inset-0 before:border-2 before:border-dotted before:border-transparent before:pointer-events-none before:transition-colors before:duration-200 lg:hover:before:border-l1";
function useBrowserName(): string {
const [name, setName] = useState("Unknown");
useEffect(() => {
const ua = navigator.userAgent;
let result = "Unknown";
if (/Edg\//.test(ua)) result = "Edge";
else if (/OPR\//.test(ua)) result = "Opera";
else if (/Firefox\//.test(ua)) result = "Firefox";
else if (/Chrome\//.test(ua)) result = "Chrome";
else if (/Safari\//.test(ua)) result = "Safari";
setName(result);
}, []);
return name;
}
export default function Header() {
const { theme, setTheme, soundEnabled, setSoundEnabled } = useShellMedia();
useBgm(soundEnabled);
// Arrow-fullscreen state: past the 0.5 reveal threshold the whole header
// (and the hyper-space section) turn white over the black stripe base
// (original X = useArrowFullscreenPastThreshold()).
const arrowWhite = useArrowFullscreenPastThreshold();
const [time, setTime] = useState("--:--");
const [coords, setCoords] = useState({ x: "0001", y: "0001" });
const [hintCount, setHintCount] = useState(0);
const [spinnerIndex, setSpinnerIndex] = useState(0);
const [toggleEpoch, setToggleEpoch] = useState(0);
const interactedRef = useRef(false);
const browserName = useBrowserName();
const [viewport, setViewport] = useState({ w: 0, h: 0 });
const year = new Date().getFullYear();
// Clock tick (original: minute interval).
useEffect(() => {
const tick = () => {
const d = new Date();
setTime(
`${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`
);
};
tick();
const interval = window.setInterval(tick, 60_000);
return () => window.clearInterval(interval);
}, []);
// Pointer coordinates (original usePointerPosition × viewport, y-up).
useEffect(() => {
const onMove = (e: PointerEvent) => {
const x = Math.round(e.clientX);
const y = Math.round(window.innerHeight - e.clientY);
setCoords({
x: String(Math.max(0, Math.min(9999, x))).padStart(4, "0"),
y: String(Math.max(0, Math.min(9999, y))).padStart(4, "0"),
});
};
window.addEventListener("pointermove", onMove, { passive: true });
return () => window.removeEventListener("pointermove", onMove);
}, []);
// Viewport size for the hint panel (original useWindowSize).
useEffect(() => {
const update = () =>
setViewport({ w: window.innerWidth, h: window.innerHeight });
update();
window.addEventListener("resize", update, { passive: true });
return () => window.removeEventListener("resize", update);
}, []);
// Spinner while sound is playing (original 130ms interval).
useEffect(() => {
if (!soundEnabled) {
setSpinnerIndex(0);
return;
}
const interval = window.setInterval(
() => setSpinnerIndex((i) => (i + 1) % SPINNER.length),
130
);
return () => window.clearInterval(interval);
}, [soundEnabled]);
const bumpHint = useCallback(() => setHintCount((c) => c + 1), []);
const sinkHint = useCallback(() => setHintCount((c) => Math.max(0, c - 1)), []);
const resetHint = useCallback(() => setHintCount(0), []);
// Changing theme/sound hides the hint panel (original effect).
const prevRef = useRef({ theme, soundEnabled });
useEffect(() => {
const prev = prevRef.current;
if (prev.theme !== theme || prev.soundEnabled !== soundEnabled) {
prevRef.current = { theme, soundEnabled };
setHintCount((c) => (c > 0 ? 0 : c));
}
}, [theme, soundEnabled]);
const cycleTheme = useCallback(() => {
interactedRef.current = true;
setToggleEpoch((e) => e + 1);
const order: ThemeSetting[] = ["light", "dark", "system"];
const next = order[(order.indexOf(theme) + 1) % order.length];
setTheme(next);
}, [theme, setTheme]);
const toggleSound = useCallback(() => {
interactedRef.current = true;
setToggleEpoch((e) => e + 1);
setSoundEnabled(!soundEnabled);
}, [soundEnabled, setSoundEnabled]);
// Global keyboard shortcuts: [L]/[D]/[A] theme, [S] sound, [T]/[B] edges.
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (
e.defaultPrevented ||
e.metaKey ||
e.ctrlKey ||
e.altKey ||
(e.target instanceof HTMLElement &&
["INPUT", "TEXTAREA", "SELECT"].includes(e.target.tagName))
) {
return;
}
const key = e.key.length === 1 ? e.key.toLowerCase() : "";
if (!key) return;
if (["l", "d", "a", "s"].includes(key)) resetHint();
if (key === "l") {
setTheme("light");
} else if (key === "d") {
setTheme("dark");
} else if (key === "a") {
setTheme("system");
} else if (key === "s") {
toggleSound();
} else if (key === "t") {
scrollEnv.scrollToTop("smooth");
} else if (key === "b") {
scrollEnv.scrollToBottom("smooth");
}
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [resetHint, setTheme, toggleSound]);
const themeLabel =
theme === "light" ? "THEME[L]" : theme === "dark" ? "THEME[D]" : "THEME[A]";
const soundLabel = soundEnabled
? `SOUND[${SPINNER[spinnerIndex]}]`
: "SOUND[\u00B7]";
const toggleCommon =
"inline-flex shrink-0 items-baseline gap-0 normal-case";
return (
<header
className={`z-50 fixed inset-0 flex flex-col justify-between font-mono-2 pointer-events-none ${ARROW_FULLSCREEN_DOM_COLOR_TRANSITION} ${
arrowWhite ? "text-white" : "text-l1"
}`}
>
<div className="flex justify-between items-center px-4 lg:px-14 py-4 lg:py-7 text-base">
<a
className={`${dotted} p-2 font-sans font-bold uppercase pointer-events-auto transition-colors duration-300 ease-out`}
style={{ fontVariationSettings: '"wght" 700, "wdth" 120' }}
href="/"
>
<ScrambleReveal text="haoqi" startDelayMs={300} scrambleColors={false} />
<ScrambleReveal text=".design" startDelayMs={300} scrambleColors={false} />
</a>
<div className="hidden lg:flex flex-wrap justify-between items-center gap-x-3 gap-y-2 pointer-events-auto basis-1/2 xl:basis-1/3">
<button
type="button"
className={`${dotted} p-2 uppercase cursor-pointer`}
onClick={() => scrollEnv.scrollToSection("#selected-work")}
>
<ScrambleReveal text="Work" startDelayMs={300} scrambleColors={false} />
</button>
<button
type="button"
className={`${dotted} p-2 uppercase cursor-pointer`}
onClick={() => scrollEnv.scrollToSection("#contact")}
>
<ScrambleReveal text="Contact" startDelayMs={300} scrambleColors={false} />
</button>
<span
role="button"
tabIndex={0}
aria-label={`Theme: ${theme}`}
className={`${dotted} ${toggleCommon} p-2 uppercase cursor-pointer pointer-events-auto`}
onClick={(e) => {
e.preventDefault();
cycleTheme();
}}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
cycleTheme();
}
}}
onPointerEnter={bumpHint}
onPointerLeave={sinkHint}
onPointerDown={resetHint}
>
<ScrambleReveal
key={`theme-${theme}-${toggleEpoch}`}
text={themeLabel}
startDelayMs={interactedRef.current ? 100 : 300}
scrambleColors={false}
/>
</span>
<span
role="button"
tabIndex={0}
aria-label={
soundEnabled ? "Sound playing, click to pause" : "Sound paused, click to play"
}
aria-pressed={soundEnabled}
className={`${dotted} ${toggleCommon} p-2 uppercase cursor-pointer pointer-events-auto`}
onClick={(e) => {
e.preventDefault();
toggleSound();
}}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
toggleSound();
}
}}
onPointerEnter={bumpHint}
onPointerLeave={sinkHint}
onPointerDown={resetHint}
>
<ScrambleReveal
key={`sound-${soundEnabled}-${toggleEpoch}`}
text={soundLabel}
startDelayMs={soundEnabled ? 100 : 300}
letterDelayMs={80}
scrambleColors={false}
/>
</span>
</div>
</div>
{/* Nav hint panel — revealed by hovering THEME/SOUND (original P>0,
fade .4s cubic-bezier(.25,1,.5,1) via AnimatePresence). */}
{hintCount > 0 && (
<div
className="hidden top-24 right-0 left-0 z-50 fixed lg:flex flex-row-reverse px-4 lg:px-14 w-full pointer-events-none"
style={{
animation: "vh-fade-in 0.4s cubic-bezier(0.25, 1, 0.5, 1) both",
}}
>
<div
className={`flex flex-col bg-be p-2 font-mono-2 xl:basis-1/3 basis-1/2 ${ARROW_FULLSCREEN_DOM_COLOR_TRANSITION} ${
arrowWhite ? "text-white" : "text-l1"
}`}
>
{/* Original hint-panel texts omit scrambleColors → the scramble
component's default (true) applies: chars flash the lime
phase colors #c0fe04 → #607F02 while revealing, and the
per-char 10ms stagger over the wrapped lines produces the
top-to-bottom green sweep. */}
<ScrambleReveal
text="Press [L] for light mode, [D] for dark mode, [A] for auto mode, or click THEME. Press [S] to pause or resume background music, or click SOUND; your choice is saved in this browser. [T] scroll to top, [B] scroll to bottom."
startDelayMs={0}
letterDelayMs={10}
/>
<br />
<span className="flex flex-row justify-between">
<ScrambleReveal
text={browserName}
startDelayMs={200}
letterDelayMs={10}
/>
<ScrambleReveal
text={`${viewport.w} \u00D7 ${viewport.h}`}
startDelayMs={200}
letterDelayMs={10}
/>
</span>
</div>
</div>
)}
<div className="flex justify-between px-4 lg:px-14 py-4 lg:py-7">
<span className="lg:hidden p-2 uppercase">
<ScrambleReveal text={time} startDelayMs={300} letterDelayMs={40} scrambleColors={false} />
</span>
<span className="hidden lg:inline p-2 uppercase">
<ScrambleReveal
text={`GMT+8 CN ${time}`}
startDelayMs={300}
letterDelayMs={40}
scrambleColors={false}
/>
</span>
<span
className={`${dotted} hidden lg:inline lg:bottom-7 lg:left-1/2 lg:fixed p-2 lg:-translate-x-1/2 cursor-pointer pointer-events-auto`}
onClick={() => scrollEnv.scrollToTop("smooth")}
>
<ScrambleReveal
text={`${coords.x} X ${coords.y} Y`}
startDelayMs={300}
letterDelayMs={40}
scrambleColors={false}
/>
</span>
{/* VIRTHEART copyright — replaces the original RotatingGlobe. */}
<span className="hidden lg:inline p-2 uppercase">
<ScrambleReveal
text={`VIRTHEART (c) ${year}`}
startDelayMs={300}
letterDelayMs={40}
scrambleColors={false}
/>
</span>
</div>
</header>
);
}