{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"facescape","type":"registry:ui","title":"Facescape","description":"A little face built from simple shapes that watches your cursor, lifts its brows and grins as you get close. Click to boop.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/facescape.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useId, useRef } from \"react\";\n\nexport interface FacescapeProps {\n  /** Face size in px. */\n  size?: number;\n  /** Head color. */\n  faceColor?: string;\n  /** Eyes, brows and mouth color. */\n  featureColor?: string;\n  /** Blush color. */\n  cheekColor?: string;\n  /** How strongly the face turns toward the cursor, 0-2. */\n  follow?: number;\n  /** Blink every few seconds. */\n  blink?: boolean;\n  /** Accessible description. */\n  label?: string;\n  className?: string;\n}\n\nconst lerp = (a: number, b: number, t: number) => a + (b - a) * t;\nconst clamp = (v: number, lo = 0, hi = 1) => Math.min(hi, Math.max(lo, v));\n\n// Mouth shapes: [half width, upper-lip bend, lower-lip bend]. Blended every frame.\nconst NEUTRAL = [15, 1, 5];\nconst GRIN = [23, 6, 26];\nconst OOH = [9, -11, 13];\n\n/**\n * A little face that watches the pointer inside its parent element: pupils track,\n * the head turns, brows lift and it grins as you get close. Click or press Enter to boop.\n */\nexport function Facescape({\n  size = 280,\n  faceColor = \"#ffd166\",\n  featureColor = \"#1c1917\",\n  cheekColor = \"#ff8fab\",\n  follow = 1,\n  blink = true,\n  label = \"Illustrated face that follows your cursor\",\n  className,\n}: FacescapeProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const svg = useRef<SVGSVGElement>(null);\n  const opts = useRef({ follow, blink });\n  const boop = useRef(0);\n  const shade = `facescape-${useId().replace(/:/g, \"\")}`;\n\n  useEffect(() => {\n    const el = root.current;\n    const s = svg.current;\n    if (!el || !s) return;\n    const q = <T extends Element>(sel: string) => s.querySelector(sel) as T;\n    const head = q<SVGGElement>(\"[data-head]\");\n    const features = q<SVGGElement>(\"[data-features]\");\n    const pupils = s.querySelectorAll<SVGGElement>(\"[data-pupil]\");\n    const lids = s.querySelectorAll<SVGGElement>(\"[data-eye]\");\n    const brows = s.querySelectorAll<SVGPathElement>(\"[data-brow]\");\n    const mouth = q<SVGPathElement>(\"[data-mouth]\");\n    const cheeks = q<SVGGElement>(\"[data-cheeks]\");\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const area = el.parentElement ?? el;\n\n    // Target (pointer) and smoothed state.\n    const target = { x: 0, y: 0, near: 0, active: false };\n    const cur = { x: 0, y: 0, near: 0, ooh: 0 };\n    let nextBlink = performance.now() + 1800;\n    let blinkAt = -1e9;\n    let raf = 0;\n    let visible = true;\n\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      const cx = r.left + r.width / 2;\n      const cy = r.top + r.height / 2;\n      const dx = e.clientX - cx;\n      const dy = e.clientY - cy;\n      const reach = Math.max(r.width, 1) * 1.6;\n      target.x = clamp(dx / reach, -1, 1);\n      target.y = clamp(dy / reach, -1, 1);\n      target.near = clamp(1 - Math.hypot(dx, dy) / reach);\n      target.active = true;\n    };\n    const onLeave = () => {\n      target.active = false;\n      target.near = 0;\n    };\n\n    const draw = (t: number) => {\n      const o = opts.current;\n      // Idle: glance around slowly when nobody is pointing.\n      const tx = target.active ? target.x : Math.sin(t / 1900) * 0.5;\n      const ty = target.active ? target.y : Math.sin(t / 2700) * 0.25 - 0.05;\n      const k = reduced ? 1 : 0.12;\n      cur.x = lerp(cur.x, tx, k);\n      cur.y = lerp(cur.y, ty, k);\n      cur.near = lerp(cur.near, target.near, reduced ? 1 : 0.08);\n      const booping = t < boop.current;\n      cur.ooh = lerp(cur.ooh, booping ? 1 : 0, reduced ? 1 : 0.2);\n\n      if (o.blink && !reduced && t > nextBlink) {\n        blinkAt = t;\n        // Occasionally a double blink.\n        nextBlink = t + (Math.random() < 0.2 ? 260 : 2200 + Math.random() * 3400);\n      }\n      const b = (t - blinkAt) / 170;\n      const lid = b < 1 ? 1 - Math.sin(b * Math.PI) * 0.92 : 1;\n\n      const f = o.follow;\n      head.setAttribute(\"transform\", `translate(${cur.x * 6 * f} ${cur.y * 5 * f}) rotate(${cur.x * 5 * f} 100 100)`);\n      features.setAttribute(\"transform\", `translate(${cur.x * 12 * f} ${cur.y * 9 * f})`);\n      for (const p of pupils) p.setAttribute(\"transform\", `translate(${cur.x * 7} ${cur.y * 8})`);\n      const squint = cur.near * 0.12 * (1 - cur.ooh);\n      const open = lid * (1 - squint) + cur.ooh * 0.15;\n      for (const l of lids) l.setAttribute(\"transform\", `translate(0 94) scale(1 ${open}) translate(0 -94)`);\n\n      const lift = cur.near * 5 + cur.ooh * 9;\n      const tilt = cur.near * 4 + cur.ooh * 6;\n      brows.forEach((b, i) => {\n        const side = i ? 1 : -1;\n        const cx = 100 + side * 28;\n        const y = 58 - lift - cur.y * 3;\n        const inner = cx - side * 13;\n        const outer = cx + side * 13;\n        b.setAttribute(\"d\", `M${inner} ${y + 2 - tilt * 0.3} Q${cx} ${y - 5 - tilt * 0.5} ${outer} ${y + 2 + tilt * 0.4}`);\n      });\n\n      const g = clamp(cur.near * 1.3) * (1 - cur.ooh);\n      const m = [0, 1, 2].map((i) => lerp(lerp(NEUTRAL[i], GRIN[i], g), OOH[i], cur.ooh));\n      const my = 132;\n      mouth.setAttribute(\"d\", `M${100 - m[0]} ${my} Q100 ${my + m[1]} ${100 + m[0]} ${my} Q100 ${my + m[2]} ${100 - m[0]} ${my}Z`);\n      cheeks.setAttribute(\"opacity\", String(0.35 + cur.near * 0.5));\n\n      if (visible) raf = requestAnimationFrame(draw);\n    };\n\n    const io = new IntersectionObserver(([e]) => {\n      visible = e.isIntersecting;\n      cancelAnimationFrame(raf);\n      if (visible) raf = requestAnimationFrame(draw);\n    });\n    io.observe(el);\n    area.addEventListener(\"pointermove\", onMove);\n    area.addEventListener(\"pointerleave\", onLeave);\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      area.removeEventListener(\"pointermove\", onMove);\n      area.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, []);\n\n  useEffect(() => {\n    opts.current = { follow, blink };\n  }, [follow, blink]);\n\n  const trigger = () => {\n    boop.current = performance.now() + 650;\n  };\n\n  return (\n    <div\n      ref={root}\n      role=\"img\"\n      aria-label={label}\n      tabIndex={0}\n      onPointerDown={trigger}\n      onKeyDown={(e) => {\n        if (e.key === \"Enter\" || e.key === \" \") {\n          e.preventDefault();\n          trigger();\n        }\n      }}\n      className={`relative cursor-pointer select-none rounded-full outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-4 focus-visible:ring-offset-background ${className ?? \"\"}`}\n      style={{ width: size, height: size }}\n    >\n      <svg ref={svg} viewBox=\"0 0 200 200\" className=\"size-full overflow-visible\" aria-hidden>\n        <ellipse cx=\"100\" cy=\"190\" rx=\"58\" ry=\"7\" fill=\"#000\" opacity=\".25\" />\n        <g data-head>\n          <rect x=\"18\" y=\"18\" width=\"164\" height=\"164\" rx=\"74\" fill={faceColor} />\n          {/* Soft top light and bottom shade give the flat shape some volume. */}\n          <rect x=\"18\" y=\"18\" width=\"164\" height=\"164\" rx=\"74\" fill={`url(#${shade})`} />\n          <g data-features>\n            <g data-cheeks fill={cheekColor} opacity=\".35\">\n              <ellipse cx=\"56\" cy=\"122\" rx=\"15\" ry=\"9\" />\n              <ellipse cx=\"144\" cy=\"122\" rx=\"15\" ry=\"9\" />\n            </g>\n            {[72, 128].map((cx) => (\n              <g key={cx} data-eye=\"\">\n                <ellipse cx={cx} cy=\"94\" rx=\"15\" ry=\"18\" fill=\"#fff\" />\n                <g data-pupil=\"\">\n                  <circle cx={cx} cy=\"95\" r=\"8.5\" fill={featureColor} />\n                  <circle cx={cx + 3} cy=\"91\" r=\"2.6\" fill=\"#fff\" />\n                </g>\n              </g>\n            ))}\n            <path data-brow=\"\" d=\"M85 60 Q72 53 59 60\" fill=\"none\" stroke={featureColor} strokeWidth=\"6\" strokeLinecap=\"round\" />\n            <path data-brow=\"\" d=\"M115 60 Q128 53 141 60\" fill=\"none\" stroke={featureColor} strokeWidth=\"6\" strokeLinecap=\"round\" />\n            <path d=\"M97 110 Q100 114 103 110\" fill=\"none\" stroke={featureColor} strokeOpacity=\".45\" strokeWidth=\"3.5\" strokeLinecap=\"round\" />\n            <path\n              data-mouth=\"\"\n              d=\"M85 132 Q100 133 115 132 Q100 137 85 132Z\"\n              fill={featureColor}\n              stroke={featureColor}\n              strokeWidth=\"4\"\n              strokeLinejoin=\"round\"\n            />\n          </g>\n        </g>\n        <defs>\n          <linearGradient id={shade} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n            <stop offset=\"0\" stopColor=\"#fff\" stopOpacity=\".35\" />\n            <stop offset=\".45\" stopColor=\"#fff\" stopOpacity=\"0\" />\n            <stop offset=\"1\" stopColor=\"#000\" stopOpacity=\".16\" />\n          </linearGradient>\n        </defs>\n      </svg>\n    </div>\n  );\n}\n"}]}