Files
personal/components/dom/ScrambleReveal.tsx
T
virtheart 744ec0becc feat: add glass models with advanced refractive shader and integrate into hero scene
- Introduced GlassModels component with custom vertex and fragment shaders for realistic glass effects.
- Updated HeroScene to include HelloModel and CursorStar components, utilizing the new glass shader.
- Enhanced Header component with ScrambleReveal for dynamic text display and local clock functionality.
- Implemented useIsMobile hook to adapt rendering based on screen size.
- Adjusted StickerLayer to ensure falling stickers are captured in the glass models' FBO pass.
2026-08-31 23:39:58 +08:00

141 lines
4.1 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, useMemo, useRef, useState } from "react";
/**
* 1:1 re-implementation of the original scramble-reveal text (chunk
* 2689132c4e070b68.js component `d`):
*
* - charset `ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+-=?/<>[]{}` picked
* randomly per render while a character is "scrambling"
* - char i starts scrambling at startDelayMs + i*letterDelayMs and settles
* after a window of 4*letterDelayMs (2 phases × 2*letterDelayMs)
* - phase 0 color #c0fe04, phase 1 color #607F02 (light theme) unless
* scrambleColors is false
* - refresh tick every 40ms; the whole text settles at
* startDelayMs + (n-1)*letterDelayMs + 4*letterDelayMs
* - starts once when the element enters the viewport (threshold 0.1)
*/
const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+-=?/<>[]{}";
const COLORS_LIGHT = ["#c0fe04", "#607F02"];
const randomChar = () => CHARSET[Math.floor(Math.random() * CHARSET.length)];
type Props = {
text?: string;
parts?: string[];
className?: string;
startDelayMs?: number;
letterDelayMs?: number;
scrambleColors?: boolean;
};
export default function ScrambleReveal({
text,
parts,
className,
startDelayMs = 0,
letterDelayMs = 80,
scrambleColors = true,
}: Props) {
const strings = useMemo(
() => (parts ? parts : [text ?? ""]),
[parts, text]
);
const ref = useRef<HTMLSpanElement>(null);
const [entered, setEntered] = useState(false);
const [startedAt, setStartedAt] = useState<number | null>(null);
const [elapsed, setElapsed] = useState(0);
const F = strings.join("").length;
const phaseLen = 2 * letterDelayMs; // j
const scrambleWindow = 2 * phaseLen; // D
const total = startDelayMs + Math.max(0, F - 1) * letterDelayMs + scrambleWindow;
useEffect(() => {
const el = ref.current;
if (!el) return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setEntered(true);
observer.disconnect();
}
},
{ threshold: 0.1 }
);
observer.observe(el);
return () => observer.disconnect();
}, []);
useEffect(() => {
if (!entered || startedAt !== null) return;
const t0 = performance.now();
setStartedAt(t0);
setElapsed(0);
}, [entered, startedAt]);
useEffect(() => {
if (startedAt === null) return;
if (elapsed >= total) return;
const interval = window.setInterval(() => {
setElapsed(performance.now() - startedAt);
}, 40);
return () => window.clearInterval(interval);
}, [startedAt, elapsed, total]);
const settled = startedAt !== null && elapsed >= total;
const V = Math.max(0, elapsed - startDelayMs);
const scrambling = startedAt !== null && !settled;
if (settled || !entered) {
return (
<span ref={ref} className={className}>
{strings.map((s, i) => (
<span key={i}>{entered ? s : ""}</span>
))}
</span>
);
}
// recompute random chars on each elapsed tick
let index = 0;
return (
<span ref={ref} className={className}>
{strings.map((s, si) => (
<span key={si}>
{s.split("").map((ch, ci) => {
const i = index++;
if (ch === "\n") return <br key={`br-${si}-${ci}`} />;
if (ch === " ") return <span key={`sp-${si}-${ci}`}> </span>;
const n = i * letterDelayMs;
if (V < n) {
return (
<span key={`c-${si}-${ci}`} style={{ opacity: 0 }}>
{ch}
</span>
);
}
if (V < n + scrambleWindow) {
const phase = Math.min(
1,
Math.floor((V - n) / phaseLen)
);
const color = scrambleColors ? COLORS_LIGHT[phase] : undefined;
return (
<span
key={`c-${si}-${ci}`}
style={color ? { color } : undefined}
>
{randomChar()}
</span>
);
}
return <span key={`c-${si}-${ci}`}>{ch}</span>;
})}
</span>
))}
</span>
);
}