feat: 添加滚动环境和媒体状态管理,优化头部组件功能,支持背景音乐和主题切换
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { Canvas } from "@react-three/fiber";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { Canvas, useThree } from "@react-three/fiber";
|
||||
import { PerspectiveCamera } from "@react-three/drei";
|
||||
|
||||
import { StickerDrop, StickerCamera } from "@/components/canvas/StickerDrop";
|
||||
@@ -12,6 +13,52 @@ 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.
|
||||
@@ -27,14 +74,53 @@ import { useIsMobile } from "@/lib/useIsMobile";
|
||||
export function HeroScene() {
|
||||
const isMobile = useIsMobile();
|
||||
const ready = useAppReady();
|
||||
const hostRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// 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);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
// z-30 keeps it above the BackgroundGrid (z-20) to prevent mix-blend-difference from making it gray
|
||||
<div className="z-30 fixed inset-0 w-full h-full pointer-events-none">
|
||||
<div
|
||||
ref={hostRef}
|
||||
className="z-30 fixed inset-0 w-full h-full pointer-events-none"
|
||||
>
|
||||
<Canvas
|
||||
dpr={[1, 2]}
|
||||
gl={{ alpha: true, antialias: true }}
|
||||
>
|
||||
<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). */}
|
||||
|
||||
Reference in New Issue
Block a user