跳至正文
获取 Pro
东方雅集E.19

墨迹显现

FreeRegistry
components/linyi/brush-reveal.tsx
"use client";
 
import * as React from "react";
import { cn } from "@/lib/cn";
 
/* Solid on the left, ragged brush edge with dry-brush hairs on the right (white = visible). */
const BRUSH_MASK =
"url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 100' preserveAspectRatio='none'><path d='M0 0H105C108 8 103 13 107 21C111 29 104 35 108 43C112 51 105 58 109 66C113 74 106 81 110 88C113 95 108 98 111 100H0Z' fill='white'/><path d='M105 9H118M107 26H125M106 39H114M108 55H128M107 70H117M109 83H126M108 94H115' stroke='white' stroke-width='2.4' stroke-linecap='round'/></svg>\")";
 
const HIDDEN = 135; // mask-position % where the stroke sits fully off the content
const ease = (t: number) => 1 - Math.pow(1 - t, 3.2);
 
export interface BrushRevealProps extends React.ComponentPropsWithoutRef<"div"> {
/** When to paint: on scroll into view, on mount, or only when `replayKey` changes. */
trigger?: "view" | "mount" | "manual";
/** Seconds. */
duration?: number;
/** Seconds. */
delay?: number;
/** Change this value to paint the stroke again. */
replayKey?: number | string;
/** Clicking the content paints it again. */
replayOnClick?: boolean;
onRevealed?: () => void;
}
 
/**
* 墨迹显现 —— content is revealed by a brush stroke sweeping left to right.
* Animates only `mask-position` on a CSS variable (no layout); the mask is removed once painted.
* Respects prefers-reduced-motion by showing the content immediately.
*/
export function BrushReveal({
trigger = "view",
duration = 1.2,
delay = 0,
replayKey,
replayOnClick = false,
onRevealed,
className,
style,
onClick,
children,
...props
}: BrushRevealProps) {
const ref = React.useRef<HTMLDivElement>(null);
const [masked, setMasked] = React.useState(true);
const raf = React.useRef(0);
const onRevealedRef = React.useRef(onRevealed);
onRevealedRef.current = onRevealed;
 
const paint = React.useCallback(() => {
const el = ref.current;
if (!el) return;
cancelAnimationFrame(raf.current);
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
setMasked(false);
onRevealedRef.current?.();
return;
}
setMasked(true);
el.style.setProperty("--brush", String(HIDDEN));
const start = performance.now() + delay * 1000;
const step = (now: number) => {
const t = Math.min(1, Math.max(0, (now - start) / (duration * 1000)));
el.style.setProperty("--brush", String(HIDDEN * (1 - ease(t))));
if (t < 1) raf.current = requestAnimationFrame(step);
else {
setMasked(false);
onRevealedRef.current?.();
}
};
raf.current = requestAnimationFrame(step);
}, [delay, duration]);
 
React.useEffect(() => {
const el = ref.current;
if (!el || trigger === "manual") return;
if (trigger === "mount") {
paint();
return;
}
const io = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
paint();
io.disconnect();
}
},
{ rootMargin: "0px 0px -12% 0px" }
);
io.observe(el);
return () => io.disconnect();
}, [trigger, paint]);
 
const first = React.useRef(true);
React.useEffect(() => {
if (first.current) {
first.current = false;
if (trigger !== "manual") return;
}
if (replayKey !== undefined) paint();
}, [replayKey, paint, trigger]);
 
React.useEffect(() => () => cancelAnimationFrame(raf.current), []);
 
const maskStyle: React.CSSProperties = masked
? ({
"--brush": HIDDEN,
WebkitMaskImage: BRUSH_MASK,
maskImage: BRUSH_MASK,
WebkitMaskSize: "200% 100%",
maskSize: "200% 100%",
WebkitMaskRepeat: "no-repeat",
maskRepeat: "no-repeat",
WebkitMaskPosition: "calc(var(--brush) * 1%) 0",
maskPosition: "calc(var(--brush) * 1%) 0",
} as React.CSSProperties)
: {};
 
return (
<div
ref={ref}
role={replayOnClick ? "button" : undefined}
tabIndex={replayOnClick ? 0 : undefined}
aria-label={replayOnClick ? "点击重绘墨迹" : undefined}
className={cn(replayOnClick && "cursor-pointer", className)}
style={{ ...maskStyle, ...style }}
onClick={(e) => {
if (replayOnClick) paint();
onClick?.(e);
}}
onKeyDown={(e) => {
if (replayOnClick && (e.key === "Enter" || e.key === " ")) {
e.preventDefault();
paint();
}
props.onKeyDown?.(e);
}}
{...props}
>
{children}
</div>
);
}