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

苏作花窗借景框

FreeRegistry
components/linyi/lattice-aperture.tsx
motion
"use client";
 
import * as React from "react";
import { motion, useMotionValue, useSpring, useTransform } from "motion/react";
import { cn } from "@/lib/cn";
import { SPRING_SMOOTH, SPRING_SNAPPY } from "@/lib/motion";
 
export type ApertureShape = "moon" | "begonia" | "octagon" | "vase";
 
export interface LatticeApertureProps {
aperture?: ApertureShape;
onApertureChange?: (next: ApertureShape) => void;
depth?: number;
title?: string;
subtitle?: string;
compact?: boolean;
className?: string;
}
 
const APERTURE_LABELS: { id: ApertureShape; label: string }[] = [
{ id: "moon", label: "月洞" },
{ id: "begonia", label: "海棠" },
{ id: "octagon", label: "八角" },
{ id: "vase", label: "净瓶" },
];
 
function getMaskPath(shape: ApertureShape): string {
switch (shape) {
case "begonia":
return "M160 22 C192 22 204 42 228 42 C254 42 268 58 268 85 C268 112 254 128 228 128 C204 128 192 148 160 148 C128 148 116 128 92 128 C66 128 52 112 52 85 C52 58 66 42 92 42 C116 42 128 22 160 22 Z";
case "octagon":
return "M114 20 L206 20 L262 56 L262 114 L206 150 L114 150 L58 114 L58 56 Z";
case "vase":
return "M138 18 L182 18 L178 44 C208 56 226 78 222 108 C218 134 196 152 160 152 C124 152 102 134 98 108 C94 78 112 56 142 44 Z";
default:
return "M160 20 A65 65 0 1 0 160 150 A65 65 0 1 0 160 20 Z";
}
}
 
export function LatticeAperture({
aperture,
onApertureChange,
depth = 14,
title = "沧浪借景",
subtitle = "SUZHOU GARDEN",
compact = false,
className,
}: LatticeApertureProps) {
const [internalShape, setInternalShape] = React.useState<ApertureShape>(aperture ?? "moon");
const uid = React.useId().replace(/:/g, "");
 
const rawX = useMotionValue(0);
const rawY = useMotionValue(0);
const springX = useSpring(rawX, SPRING_SMOOTH);
const springY = useSpring(rawY, SPRING_SMOOTH);
 
const farX = useTransform(springX, (v) => -v * depth * 0.5);
const farY = useTransform(springY, (v) => -v * depth * 0.4);
const haloX = useTransform(springX, (v) => -v * depth * 0.85);
const haloY = useTransform(springY, (v) => -v * depth * 0.65);
const midX = useTransform(springX, (v) => -v * depth * 1.25);
const midY = useTransform(springY, (v) => -v * depth * 0.9);
const nearX = useTransform(springX, (v) => -v * depth * 1.65);
const nearY = useTransform(springY, (v) => -v * depth * 1.15);
 
React.useEffect(() => {
if (aperture) setInternalShape(aperture);
}, [aperture]);
 
const activeShape = aperture ?? internalShape;
const pathD = getMaskPath(activeShape);
 
const selectShape = (next: ApertureShape) => {
setInternalShape(next);
onApertureChange?.(next);
};
 
const handleMove = (e: React.PointerEvent<HTMLDivElement>) => {
const r = e.currentTarget.getBoundingClientRect();
if (!r.width || !r.height) return;
rawX.set((e.clientX - r.left) / r.width - 0.5);
rawY.set((e.clientY - r.top) / r.height - 0.5);
};
 
const handleLeave = () => {
rawX.set(0);
rawY.set(0);
};
 
return (
<div className={cn("w-full space-y-2.5", compact ? "max-w-[272px]" : "max-w-[440px]", className)}>
<div
onPointerMove={handleMove}
onPointerLeave={handleLeave}
className={cn(
"relative overflow-hidden rounded-[18px] border border-line-strong select-none cursor-crosshair",
compact ? "h-[146px]" : "h-[214px]"
)}
style={{ background: "var(--bg-well)" }}
>
{/* Atmospheric Soft Ink-Wash Scenery: Paper Morning vs Obsidian Night-Moon (端砚夜月) */}
<motion.div
style={{ x: farX, y: farY }}
className="absolute -inset-6 bg-[radial-gradient(120%_100%_at_50%_18%,#EAE6DA_0%,#D9D5C9_48%,#BFC5BF_100%)] dark:bg-[radial-gradient(120%_100%_at_50%_18%,#1E222B_0%,#14171D_50%,#0C0E12_100%)] transition-colors duration-500"
/>
 
{/* Light: Warm Vermilion Sun Halo | Dark: Pale-Gold Full Moon Halo (端砚夜月) */}
<motion.div
style={{ x: haloX, y: haloY }}
className="pointer-events-none absolute -inset-6 bg-[radial-gradient(circle_42px_at_66%_36%,rgba(194,59,38,0.24)_0%,rgba(194,59,38,0.06)_46%,transparent_74%)] dark:bg-[radial-gradient(circle_14px_at_66%_34%,rgba(250,242,220,0.88)_0%,rgba(243,230,196,0.42)_44%,rgba(220,204,166,0.12)_76%,transparent_100%),radial-gradient(circle_64px_at_66%_34%,rgba(243,232,204,0.22)_0%,transparent_72%)]"
/>
 
{/* Soft Midground Celadon Mist Wash (Light: Song Celadon | Dark: Moonlit Pine-Jade Mist) */}
<motion.div
style={{ x: midX, y: midY, filter: "blur(6px)" }}
className="pointer-events-none absolute -inset-8 bg-[radial-gradient(64%_48%_at_34%_82%,rgba(58,92,86,0.36)_0%,rgba(58,92,86,0.12)_48%,transparent_76%),radial-gradient(58%_42%_at_78%_86%,rgba(34,42,39,0.44)_0%,rgba(34,42,39,0.14)_52%,transparent_78%)] dark:bg-[radial-gradient(64%_48%_at_34%_82%,rgba(74,163,142,0.22)_0%,rgba(74,163,142,0.06)_48%,transparent_76%),radial-gradient(58%_42%_at_78%_86%,rgba(14,18,22,0.72)_0%,rgba(14,18,22,0.24)_52%,transparent_78%)]"
/>
 
{/* Foreground Soft Ink Foliage Shadow */}
<motion.div
style={{ x: nearX, y: nearY, filter: "blur(5px)" }}
className="pointer-events-none absolute -inset-8 bg-[radial-gradient(36%_60%_at_24%_96%,rgba(22,20,18,0.56)_0%,rgba(22,20,18,0.16)_52%,transparent_74%)] dark:bg-[radial-gradient(38%_62%_at_24%_96%,rgba(6,7,9,0.84)_0%,rgba(6,7,9,0.32)_52%,transparent_76%)]"
/>
 
{/* Plaster Wall Cutout Aperture with Realistic Inner Bevel Depth */}
<svg
viewBox="0 0 320 170"
preserveAspectRatio="none"
className="pointer-events-none absolute inset-0 h-full w-full"
>
<defs>
<mask id={`aperture-mask-${uid}`}>
<rect width="320" height="170" fill="white" />
<path d={pathD} fill="black" />
</mask>
<filter id={`plaster-grain-${uid}`}>
<feTurbulence type="fractalNoise" baseFrequency="0.65" numOctaves="3" seed="9" />
<feColorMatrix type="saturate" values="0.1" />
</filter>
</defs>
{/* Wall body */}
<rect
width="320"
height="170"
fill="var(--bg-surface)"
mask={`url(#aperture-mask-${uid})`}
/>
{/* Subtle lime-plaster mineral texture on wall */}
<rect
width="320"
height="170"
filter={`url(#plaster-grain-${uid})`}
opacity="0.06"
mask={`url(#aperture-mask-${uid})`}
/>
{/* Recessed bevel shadow around wall cutout */}
<path
d={pathD}
fill="none"
stroke="rgba(34, 28, 20, 0.24)"
strokeWidth="3.5"
className="dark:stroke-[rgba(0,0,0,0.62)]"
style={{ filter: "blur(1.6px)" }}
/>
<path d={pathD} fill="none" stroke="var(--line-strong)" strokeWidth="1" />
</svg>
 
{/* Minimal corner inscription */}
<div className="pointer-events-none absolute bottom-2.5 left-3.5 right-3.5 flex items-end justify-between">
<span className="font-serif text-xs font-medium tracking-wider text-fg">{title}</span>
{!compact && (
<span className="font-mono-num text-[10px] text-fg-muted">{subtitle}</span>
)}
</div>
</div>
 
<div className="flex items-center justify-between gap-2 rounded-pill border border-line bg-surface px-2 py-1">
<div role="tablist" aria-label="花窗窗型" className="flex items-center gap-1">
{APERTURE_LABELS.map((item) => {
const active = activeShape === item.id;
return (
<button
key={item.id}
type="button"
role="tab"
aria-selected={active}
onClick={() => selectShape(item.id)}
className={cn(
"relative rounded-pill px-2.5 py-1 text-[11px] font-medium cursor-pointer transition-colors",
active ? "text-fg-inverse" : "text-fg-secondary hover:text-fg"
)}
>
{active && (
<motion.span
layoutId={`lattice-aperture-pill-${uid}`}
transition={SPRING_SNAPPY}
className="absolute inset-0 rounded-pill bg-inverse"
/>
)}
<span className="relative z-10">{item.label}</span>
</button>
);
})}
</div>
{!compact && <span className="font-mono-num text-[10px] pr-1.5 text-fg-muted">移步换景</span>}
</div>
</div>
);
}