{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"lens-follow","type":"registry:ui","title":"Lens Follow","description":"A liquid glass lens that trails the cursor, magnifying and refracting the content beneath it with chromatic edges.","dependencies":["motion","react-use-measure"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/lens-follow.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { motion, useMotionValue, useReducedMotion, useSpring, useTransform, useVelocity } from \"motion/react\";\nimport { useEffect, useId, useRef, type PointerEvent, type ReactNode } from \"react\";\nimport useMeasure from \"react-use-measure\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface LensFollowProps {\n  /** Content the lens floats over. Give it its own background. */\n  children: ReactNode;\n  /** Lens diameter in px. */\n  size?: number;\n  /** Magnification inside the lens. */\n  zoom?: number;\n  /** Px of edge refraction (how hard the rim bends the image). */\n  refraction?: number;\n  /** Px of chromatic split between color channels at the rim. */\n  aberration?: number;\n  /** Squash and stretch the lens with its speed. */\n  wobble?: boolean;\n  className?: string;\n}\n\n/** Radial displacement map: neutral in the middle, bending outward toward the rim. */\nfunction lensMap(box: number, radius: number) {\n  const c = document.createElement(\"canvas\");\n  c.width = c.height = box;\n  const ctx = c.getContext(\"2d\")!;\n  const img = ctx.createImageData(box, box);\n  const mid = box / 2;\n  for (let y = 0; y < box; y++) {\n    for (let x = 0; x < box; x++) {\n      const nx = (x + 0.5 - mid) / radius;\n      const ny = (y + 0.5 - mid) / radius;\n      const d = Math.hypot(nx, ny);\n      // Cubic falloff keeps the center clear and piles distortion onto the rim.\n      const k = d > 1 ? 1 / d : d * d;\n      const i = (y * box + x) * 4;\n      img.data[i] = 128 + nx * k * 127;\n      img.data[i + 1] = 128 + ny * k * 127;\n      img.data[i + 2] = 128;\n      img.data[i + 3] = 255;\n    }\n  }\n  ctx.putImageData(img, 0, 0);\n  return c.toDataURL();\n}\n\nconst channel = (r: number, g: number, b: number) => `${r} 0 0 0 0  0 ${g} 0 0 0  0 0 ${b} 0 0  0 0 0 1 0`;\n\nexport function LensFollow({\n  children,\n  size = 200,\n  zoom = 1.35,\n  refraction = 60,\n  aberration = 6,\n  wobble = true,\n  className,\n}: LensFollowProps) {\n  const [measureRef, bounds] = useMeasure();\n  const mapRef = useRef<SVGFEImageElement>(null);\n  const placed = useRef(false);\n  const id = `fluid-glass-${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  const reduced = useReducedMotion();\n\n  // Filtered box is padded so the rim can sample pixels from outside the lens.\n  const pad = Math.ceil(refraction / 2 + aberration + 4);\n  const box = size + pad * 2;\n\n  const mx = useMotionValue(0);\n  const my = useMotionValue(0);\n  const spring = reduced ? { stiffness: 2000, damping: 100 } : { stiffness: 260, damping: 24, mass: 0.7 };\n  const cx = useSpring(mx, spring);\n  const cy = useSpring(my, spring);\n  const vx = useVelocity(cx);\n  const vy = useVelocity(cy);\n\n  const lensX = useTransform(cx, (v) => v - size / 2);\n  const lensY = useTransform(cy, (v) => v - size / 2);\n  const cloneX = useTransform(cx, (v) => box / 2 - v * zoom);\n  const cloneY = useTransform(cy, (v) => box / 2 - v * zoom);\n  const stretch = (a: number, b: number) =>\n    wobble && !reduced ? 1 + Math.min(Math.abs(a) / 5000, 0.18) - Math.min(Math.abs(b) / 9000, 0.09) : 1;\n  const scaleX = useTransform([vx, vy], ([a, b]: number[]) => stretch(a, b));\n  const scaleY = useTransform([vx, vy], ([a, b]: number[]) => stretch(b, a));\n\n  useEffect(() => {\n    mapRef.current?.setAttribute(\"href\", lensMap(box, size / 2));\n  }, [box, size]);\n\n  // Park the lens in the middle until the pointer shows up.\n  useEffect(() => {\n    if (placed.current || !bounds.width) return;\n    mx.jump(bounds.width / 2);\n    my.jump(bounds.height / 2);\n    cx.jump(bounds.width / 2);\n    cy.jump(bounds.height / 2);\n  }, [bounds.width, bounds.height, mx, my, cx, cy]);\n\n  const onMove = (e: PointerEvent<HTMLDivElement>) => {\n    const r = e.currentTarget.getBoundingClientRect();\n    placed.current = true;\n    mx.set(e.clientX - r.left);\n    my.set(e.clientY - r.top);\n  };\n\n  return (\n    <div ref={measureRef} onPointerMove={onMove} onPointerDown={onMove} className={cn(\"relative overflow-hidden\", className)}>\n      {children}\n\n      <motion.div\n        aria-hidden\n        className=\"pointer-events-none absolute left-0 top-0 rounded-full\"\n        style={{\n          width: size,\n          height: size,\n          x: lensX,\n          y: lensY,\n          scaleX,\n          scaleY,\n          boxShadow: \"0 24px 60px -12px rgba(0,0,0,.55), 0 4px 14px rgba(0,0,0,.25)\",\n        }}\n      >\n        <div className=\"absolute inset-0 overflow-hidden rounded-full\">\n          <div\n            className=\"absolute bg-background\"\n            style={{ left: -pad, top: -pad, width: box, height: box, overflow: \"hidden\", filter: `url(#${id})` }}\n          >\n            <motion.div\n              inert\n              className=\"absolute left-0 top-0\"\n              style={{ width: bounds.width, height: bounds.height, x: cloneX, y: cloneY, scale: zoom, originX: 0, originY: 0 }}\n            >\n              {children}\n            </motion.div>\n          </div>\n          {/* Specular rim and a soft highlight: what makes it read as glass. */}\n          <div\n            className=\"absolute inset-0 rounded-full\"\n            style={{\n              background:\n                \"radial-gradient(circle at 32% 24%, rgba(255,255,255,.42), rgba(255,255,255,0) 30%), radial-gradient(circle at 70% 82%, rgba(255,255,255,.14), rgba(255,255,255,0) 28%)\",\n              boxShadow:\n                \"inset 0 0 0 1px rgba(255,255,255,.35), inset 0 2px 1px rgba(255,255,255,.5), inset 0 -10px 24px rgba(0,0,0,.28), inset 0 12px 28px rgba(255,255,255,.12)\",\n            }}\n          />\n        </div>\n      </motion.div>\n\n      <svg aria-hidden width=\"0\" height=\"0\" className=\"absolute\">\n        <filter id={id} filterUnits=\"userSpaceOnUse\" x=\"0\" y=\"0\" width={box} height={box} colorInterpolationFilters=\"sRGB\">\n          <feImage ref={mapRef} x=\"0\" y=\"0\" width={box} height={box} preserveAspectRatio=\"none\" result=\"map\" />\n          <feDisplacementMap in=\"SourceGraphic\" in2=\"map\" scale={refraction} xChannelSelector=\"R\" yChannelSelector=\"G\" result=\"dr\" />\n          <feColorMatrix in=\"dr\" values={channel(1, 0, 0)} result=\"r\" />\n          <feDisplacementMap in=\"SourceGraphic\" in2=\"map\" scale={refraction + aberration} xChannelSelector=\"R\" yChannelSelector=\"G\" result=\"dg\" />\n          <feColorMatrix in=\"dg\" values={channel(0, 1, 0)} result=\"g\" />\n          <feDisplacementMap in=\"SourceGraphic\" in2=\"map\" scale={refraction + aberration * 2} xChannelSelector=\"R\" yChannelSelector=\"G\" result=\"db\" />\n          <feColorMatrix in=\"db\" values={channel(0, 0, 1)} result=\"b\" />\n          <feBlend in=\"r\" in2=\"g\" mode=\"screen\" result=\"rg\" />\n          <feBlend in=\"rg\" in2=\"b\" mode=\"screen\" />\n        </filter>\n      </svg>\n    </div>\n  );\n}\n"}]}