跳至正文
获取 Pro
AI 代理A.02

点阵加载器

FreeRegistry
components/linyi/dot-matrix-loader.tsx
"use client";
 
import { useEffect, useRef, useState } from "react";
import { cn } from "@/lib/cn";
 
export type DotMatrixMode = "wave" | "orbit" | "scan";
 
export interface DotMatrixLoaderProps {
mode?: DotMatrixMode;
speed?: number;
gridSize?: number;
showInlineControls?: boolean;
className?: string;
}
 
const MODES: { id: DotMatrixMode; label: string }[] = [
{ id: "wave", label: "波纹" },
{ id: "orbit", label: "轨道" },
{ id: "scan", label: "扫描" },
];
 
export function DotMatrixLoader({
mode: propMode = "wave",
speed: propSpeed = 1.2,
gridSize: rawGridSize = 7,
showInlineControls = false,
className,
}: DotMatrixLoaderProps) {
const gridSize = Math.max(3, Math.min(11, Math.round(rawGridSize)));
const [localMode, setLocalMode] = useState<DotMatrixMode>(propMode);
const [localSpeed, setLocalSpeed] = useState(propSpeed);
 
useEffect(() => {
setLocalMode(propMode);
}, [propMode]);
 
useEffect(() => {
setLocalSpeed(propSpeed);
}, [propSpeed]);
 
const activeMode = showInlineControls ? localMode : propMode;
const activeSpeed = showInlineControls ? localSpeed : propSpeed;
 
const wrapRef = useRef<HTMLDivElement>(null);
const dotsRef = useRef<(HTMLSpanElement | null)[]>([]);
const configRef = useRef({ mode: activeMode, speed: activeSpeed, gridSize });
configRef.current = { mode: activeMode, speed: activeSpeed, gridSize };
 
useEffect(() => {
const wrap = wrapRef.current;
let t = 0;
let raf = 0;
let visible = true;
const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;
 
const renderFrame = (advance: boolean) => {
const { mode, speed, gridSize: g } = configRef.current;
const center = (g - 1) / 2;
if (advance) t += 0.055 * speed;
 
const total = g * g;
for (let i = 0; i < total; i++) {
const el = dotsRef.current[i];
if (!el) continue;
const r = Math.floor(i / g);
const c = i % g;
let v = 0;
 
if (mode === "wave") {
v = (Math.sin(t * 2 - Math.hypot(r - center, c - center) * 0.9) + 1) / 2;
} else if (mode === "orbit") {
const angle = Math.atan2(r - center, c - center);
const dist = Math.hypot(r - center, c - center);
v = Math.exp(-Math.pow(dist - center * 0.72, 2) * 1.4) * ((Math.cos(angle - t * 2.2) + 1) / 2);
} else {
v = Math.exp(-Math.pow(c - ((t * 2.2) % g), 2) * 0.8) * (0.65 + 0.35 * Math.sin(t * 3 + r));
}
 
el.style.backgroundColor = v > 0.78 ? "var(--accent)" : "var(--fg-primary)";
el.style.opacity = (0.1 + v * 0.88).toFixed(2);
}
};
 
const tick = () => {
if (!visible) return;
renderFrame(true);
raf = requestAnimationFrame(tick);
};
 
if (reduced) {
t = 1.2;
renderFrame(false);
return;
}
 
const io =
wrap && typeof IntersectionObserver !== "undefined"
? new IntersectionObserver((entries) => {
const isIntersecting = entries[0]?.isIntersecting ?? true;
if (isIntersecting && !visible) {
visible = true;
raf = requestAnimationFrame(tick);
} else if (!isIntersecting && visible) {
visible = false;
cancelAnimationFrame(raf);
}
})
: null;
if (wrap && io) io.observe(wrap);
 
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
io?.disconnect();
};
}, [activeMode, gridSize]);
 
const total = gridSize * gridSize;
dotsRef.current.length = total;
 
return (
<div
ref={wrapRef}
role="status"
aria-label="点阵加载中"
className={cn("flex flex-col items-center justify-center gap-3.5 w-full", className)}
>
{showInlineControls && (
<div className="w-full flex items-center justify-between gap-2">
<div className="inline-flex items-center gap-1 p-0.5 rounded-pill bg-surface border border-line text-[11px]">
{MODES.map((item) => {
const selected = localMode === item.id;
return (
<button
key={item.id}
type="button"
onClick={() => setLocalMode(item.id)}
className={cn(
"px-2.5 py-0.5 rounded-pill cursor-pointer transition-colors",
selected ? "bg-well text-fg font-medium" : "text-fg-muted hover:text-fg-secondary"
)}
>
{item.label}
</button>
);
})}
</div>
<span className="font-mono-num text-[10px] text-fg-muted">
{gridSize}×{gridSize}
</span>
</div>
)}
 
<div
className="grid gap-2 p-3.5 rounded-well bg-surface border border-line shadow-panel"
style={{ gridTemplateColumns: `repeat(${gridSize}, minmax(0, 1fr))` }}
>
{Array.from({ length: total }).map((_, idx) => (
<span
key={idx}
ref={(el) => {
dotsRef.current[idx] = el;
}}
className="w-2 h-2 rounded-[2px] block bg-fg opacity-15"
/>
))}
</div>
 
{showInlineControls && (
<div className="w-full flex items-center gap-3 text-[11px]">
<input
type="range"
min={0.4}
max={2.6}
step={0.1}
value={localSpeed}
onChange={(e) => setLocalSpeed(parseFloat(e.target.value))}
className="spec-slider flex-1"
aria-label="频率"
/>
<span className="font-mono-num w-8 text-right text-fg-secondary">{localSpeed.toFixed(1)}x</span>
</div>
)}
</div>
);
}