feat: 添加手写签名动画组件 SvgSignature,并更新样式变量以支持主题

This commit is contained in:
2026-09-01 00:40:42 +08:00
parent 3c766a4967
commit 4599a49879
3 changed files with 183 additions and 3 deletions
+171
View File
@@ -0,0 +1,171 @@
"use client";
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
/**
* Hand-written signature draw-on animation — 1:1 port of the original `fC`
* component + `useHasEnteredViewport` hook (chunk 4d3f3b68dbbde33a.js).
*
* - 4 neon (#C0FE04) stroke paths, drawn sequentially.
* - Each path's length is measured with getTotalLength(); the draw duration
* is length / 720 seconds, strokes start after a 0.5s delay with a 0.03s
* gap, all fed to the CSS animation via --path-dur / --path-delay custom
* properties (dasharray/dashoffset set inline).
* - The `is-drawing` class toggles with viewport presence (once:false), so
* the animation replays on every re-entry; prefers-reduced-motion shows
* the finished signature immediately.
*/
type SignPath = { order: number; d: string; strokeWidth: number };
const SIGN_PATHS: SignPath[] = [
{
order: 0,
d: "M138.27 11.7729C123.15 39.3885 106.223 85.497 102.06 100.029C98.6588 111.899 98.3721 128.792 98.6271 131.165",
strokeWidth: 4,
},
{
order: 1,
d: "M78.2326 42.073C68.2519 91.6846 24.5171 161.888 11.6117 145.082C-3.90668 124.872 84.4229 80.042 149.127 70.3141C129.181 76.883 121.731 89.3385 127.224 93.3199C137.212 100.559 148.931 80.9071 154.826 68.4373C154.826 68.4373 145.919 84.0047 152.863 86.4553C163.666 90.2674 183.35 47.449 193.768 55.6123C200.863 61.1719 187.995 78.0438 180.889 75.6465C176.521 74.173 179.98 64.5401 184.583 59.6902C186.629 62.1747 192.878 65.6969 201.5 59.9093C210.123 54.1218 217.989 47.6358 220.844 45.1163",
strokeWidth: 4,
},
{
order: 2,
d: "M235.554 43.4299C221.979 37.3731 206.4 60.4017 215.719 63.1233C224.115 65.5752 234.431 48.0119 239.203 40.1227C237.612 42.7522 234.822 53.6736 235.156 66.1976C235.574 81.8524 228.174 116.927 217.431 114.674C206.687 112.422 217.712 80.3645 242.778 57.3701C262.83 38.9746 269.549 28.9006 270.402 26.163C266.375 32.0516 260.249 44.2468 267.959 45.919C275.669 47.5912 298.148 19.8335 308.423 5.74565",
strokeWidth: 4,
},
{
order: 3,
d: "M274.89 10.4194L274.409 16.157",
strokeWidth: 5,
},
];
/** Verbatim CSS injected by the original component (style tag before the svg). */
const SVG_SIGN_CSS = `
.svg-sign__path {
opacity: 0;
fill: none;
stroke-linecap: butt;
}
.svg-sign.is-drawing .svg-sign__path {
animation:
svg-sign-show 0s linear var(--path-delay) forwards,
svg-sign-draw var(--path-dur) cubic-bezier(0.65, 0, 0.35, 1) var(--path-delay) forwards;
}
@keyframes svg-sign-draw {
to { stroke-dashoffset: 0; }
}
@keyframes svg-sign-show {
to { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
.svg-sign.is-drawing .svg-sign__path {
animation: none;
stroke-dashoffset: 0;
opacity: 1;
}
}
`;
/** Original hook (module 19668): viewport presence with replay support. */
function useHasEnteredViewport({
once = true,
threshold = 0.1,
root = null,
rootMargin,
}: {
once?: boolean;
threshold?: number;
root?: Element | Document | null;
rootMargin?: string;
} = {}) {
const [node, setNode] = useState<Element | null>(null);
const [hasEntered, setHasEntered] = useState(false);
const ref = useCallback((el: Element | null) => setNode(el), []);
useEffect(() => {
if (!node || (once && hasEntered)) return;
const observer = new IntersectionObserver(
([entry]) => {
if (!entry) return;
if (entry.isIntersecting) {
setHasEntered(true);
if (once) observer.disconnect();
} else if (!once) {
setHasEntered(false);
}
},
{ threshold, root, rootMargin }
);
observer.observe(node);
return () => observer.disconnect();
}, [node, once, hasEntered, threshold, root, rootMargin]);
return { ref, hasEntered };
}
export default function SvgSignature({ className }: { className?: string }) {
const pathsRef = useRef<(SVGPathElement | null)[]>([]);
const [armed, setArmed] = useState(false);
const paths = useMemo(
() => [...SIGN_PATHS].sort((a, b) => a.order - b.order),
[]
);
const { ref: svgRef, hasEntered: hasEnteredViewport } = useHasEnteredViewport({
once: false,
threshold: 0.15,
rootMargin: "0px 0px -8% 0px",
});
useLayoutEffect(() => {
let delay = 0.5;
for (const path of pathsRef.current) {
if (!path) continue;
const len = path.getTotalLength();
const dur = len / 720;
path.style.setProperty("--path-len", String(len));
path.style.setProperty("--path-dur", `${dur}s`);
path.style.setProperty("--path-delay", `${delay}s`);
path.style.strokeDasharray = `${len}`;
path.style.strokeDashoffset = `${len}`;
delay += dur + 0.03;
}
setArmed(true);
}, [paths]);
return (
<>
<style>{SVG_SIGN_CSS}</style>
<svg
ref={svgRef}
viewBox="0 0 320 154"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={`svg-sign${armed && hasEnteredViewport ? " is-drawing" : ""}${className ? ` ${className}` : ""}`}
aria-hidden="true"
>
{paths.map((p, i) => (
<path
key={p.order}
ref={(el) => {
pathsRef.current[i] = el;
}}
className="svg-sign__path"
d={p.d}
stroke="#C0FE04"
strokeWidth={p.strokeWidth}
fill="none"
/>
))}
</svg>
</>
);
}