Files
personal/components/canvas/HeroScene.tsx
T
virtheart 3957e36c41 feat: 详情页 MDX 组件 1:1(目录/锚点/代码块/页脚)与首页层叠修复
- 详情页补齐原站 MDX 组件:左侧目录(2xl 固定定位、滚动高亮 top≤96、
  点击平滑滚动)、标题锚点(hover #、点击复制链接 + replaceState)、
  代码块(语言标签 + COPY/COPIED + 原站 prism token HTML 按行号渲染,
  --code-* 配色变量)、页脚 Metadata(实时视口尺寸 / 正文字符数)与
  Work/Contact 按钮(sessionStorage hq:pendingScrollAnchor → 首页过渡后
  滚动到对应区块,SmoothScroller 消费)
- content/workArticles.ts 重新提取:内联 strong/em/code 片段、标题 id、
  代码块语言 + 高亮 HTML + 纯文本(bs4 全量解析 6 篇文章)
- 详情页不再显示十字网格(BackgroundGrid 按 pathname 门控,置于全部
  hooks 之后避免 hook 数量变化崩溃)
- 首页层叠修复:WebGL 容器改为原站的 top-0 left-0 -z-1 fixed(此前
  z-auto fixed 按 CSS 绘制顺序盖住 banner 文案与作品说明文字),footer
  改为原站的 z-10 relative,body 背景移至 html 根节点(否则普通流 body
  背景会盖住 -z-1 场景)
2026-09-01 22:15:12 +08:00

188 lines
7.6 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 { useEffect, useRef } from "react";
import { usePathname } from "next/navigation";
import { Canvas, useThree } from "@react-three/fiber";
import { PerspectiveCamera } from "@react-three/drei";
import { StickerDrop, StickerCamera } from "@/components/canvas/StickerDrop";
import { HelloModel, CursorStar, FooterModel } from "@/components/canvas/GlassModels";
import { HyperspaceModel } from "@/components/canvas/HyperspaceModel";
import ImageLayers from "@/components/canvas/ImageLayers";
import BackgroundFX from "@/components/canvas/BackgroundFX";
import DotOverlay from "@/components/canvas/DotOverlay";
import PostFX from "@/components/canvas/PostFX";
import { useAppReady } from "@/components/dom/LoadingOverlay";
import { useIsMobile } from "@/lib/useIsMobile";
/**
* R3F's initial measurement can race page CSS on first load: the canvas
* mounts before styles are applied, react-use-measure reports the canvas
* element's intrinsic size (300×150 → 1440×150) instead of the fixed
* inset-0 container (100vh), and R3F writes that stale size into
* canvas.style — permanently, because its ResizeObserver never fires
* again (the container never actually changes size afterwards). Any real
* window resize re-measures and self-heals. This guard re-runs R3F's own
* size pipeline when it detects that mismatch, so the canvas is always
* sized to its container.
*/
function CanvasSizeGuard() {
const gl = useThree((s) => s.gl);
const setSize = useThree((s) => s.setSize);
useEffect(() => {
const canvas = gl.domElement;
let raf = 0;
let ticks = 0;
const fix = () => {
const parent = canvas.parentElement;
if (parent) {
const cw = parent.clientWidth;
const ch = parent.clientHeight;
const styleW = parseFloat(canvas.style.width) || 0;
const styleH = parseFloat(canvas.style.height) || 0;
if (
cw > 0 &&
ch > 0 &&
(Math.abs(styleW - cw) > 1 || Math.abs(styleH - ch) > 1)
) {
// Re-run R3F's sizing: store.setSize updates size state + viewport
// (camera aspect, consumers), gl.setSize resizes the drawing
// buffer and canvas style.
gl.setSize(cw, ch, true);
setSize(cw, ch);
}
}
ticks += 1;
if (ticks < 40) raf = requestAnimationFrame(fix);
};
raf = requestAnimationFrame(fix);
return () => cancelAnimationFrame(raf);
}, [gl, setSize]);
return null;
}
/**
* Hero canvas: animated background + falling stickers + glass models +
* DOM-tracked image layers.
*
* - BackgroundFX renders the original sz multi-pass gradient at
* renderOrder -10; the glass FBO pass (layer-0 mask) captures it, which
* is what tints the glass models blue.
* - Stickers live on layer 0; glass models on layer 0+10 (10 excluded from
* the FBO pass — no self-refraction).
* - Models animate only from scroll (entry/after targets, damp 6); the
* camera intro dolly starts once `ready` (loading gate).
*/
export function HeroScene() {
const isMobile = useIsMobile();
const ready = useAppReady();
const hostRef = useRef<HTMLDivElement>(null);
// Original canvas lifecycle (module 34841 `canvas/set`): the canvas is
// mounted for the home page and the fullscreen transitions; on detail
// routes it is unmounted once the route reveal completes. Our DOM-based
// transition overlay covers navigation, so the canvas simply renders
// home-only — no background FX / stickers / models leak onto detail pages.
const pathname = usePathname();
const home = pathname === "/";
// Watchdog (outside the Canvas so it runs even before R3F configures):
// R3F's react-use-measure can capture a stale intrinsic-size measurement
// on first load (canvas 300×150 before page CSS applies) and its
// ResizeObserver then never fires again — the canvas stays squished
// until a real window resize. react-use-measure independently listens
// to window 'resize' (debounced 0ms), so dispatching one forces a fresh
// measurement and reconfigures R3F with the correct container size.
useEffect(() => {
const host = hostRef.current;
if (!host) return;
const fix = () => {
const canvas = host.querySelector("canvas");
if (!canvas) return;
const cw = host.clientWidth;
const ch = host.clientHeight;
const sw = parseFloat(canvas.style.width) || 0;
const sh = parseFloat(canvas.style.height) || 0;
if (
cw > 0 &&
ch > 0 &&
(Math.abs(sw - cw) > 1 || Math.abs(sh - ch) > 1)
) {
window.dispatchEvent(new Event("resize"));
}
};
const raf = requestAnimationFrame(() => requestAnimationFrame(fix));
const iv = window.setInterval(fix, 250);
document.fonts?.ready?.then(fix).catch(() => {});
return () => {
cancelAnimationFrame(raf);
window.clearInterval(iv);
};
}, []);
if (!home) return null;
return (
// 1:1 original stacking (chunk 4d3f3b68): the WebGL host is
// "top-0 left-0 -z-1 fixed w-full h-dvh lg:h-screen" — a NEGATIVE
// z-index, so every in-flow text/section paints ABOVE the scene. A
// z-auto fixed host would paint above normal-flow content (CSS paint
// order) and hide the banner copy + work captions.
<div
ref={hostRef}
className="top-0 left-0 -z-1 fixed w-full h-dvh lg:h-screen pointer-events-none"
>
<Canvas
dpr={[1, 2]}
gl={{ alpha: true, antialias: true }}
// R3F forces pointerEvents:auto on its container div, which would
// swallow every pointer event for the DOM beneath the fixed canvas
// (work-card hovers, link hovers). All pointer tracking in this port
// is window-level (lib/pointer), so the canvas is fully transparent
// to hit-testing — matching the original site.
style={{ pointerEvents: "none" }}
>
<CanvasSizeGuard />
{/* Camera rig: horizontal FOV 60/38, distance lerp(24, 32, sP),
intro dolly z+8 → A (customCubic, 1.2s, ready-gated), mouse
parallax (desktop). */}
<PerspectiveCamera makeDefault position={[0, 0, 32]} near={0.1} far={200} />
<StickerCamera ready={ready} />
{/* Animated background (vignette/swirl/sine/shatter/bokeh → output) */}
<BackgroundFX />
{/* Scroll dot-overlay (original s1): page-bg-colored 4px dot grid
whose opacity = sT(sP) — melts the WebGL scene into the page
background after ~0.75 viewport of scroll (this is the original
"stickers fade out on scroll" behavior); reopens near footer. */}
<DotOverlay />
{/* Falling stickers: 1:1 re-implementation of the original particle system.
Live on layer 0 — visible to the main camera and captured by the
glass FBO pass (which renders with a layer-0 camera mask). */}
<StickerDrop />
{/* DOM-tracked image layers (about + selected-work cards), rendered
above stickers via renderOrder 20. */}
<ImageLayers />
{/* Glass hero models (hello + blue cursor star + footer cnt), refractive tint shader */}
<HelloModel isMobile={isMobile} />
<CursorStar isMobile={isMobile} />
<FooterModel />
{/* Hyper-space arrow (original cC/cx): cursor.glb stripes that scale
up to engulf the viewport while the hyper-space section scrolls
through; reports progress to the arrow-fullscreen store. */}
<HyperspaceModel />
{/* Post FX (original cG): lens-flare star streaks + pointer-driven
fluid refraction + lime dot-trail indicator. */}
<PostFX />
</Canvas>
</div>
);
}
export default HeroScene;