{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "halftone-field",
  "type": "registry:component",
  "title": "Halftone Field",
  "description": "A print-style halftone with two overprinted inks: the dots grow and shrink with a flowing field and with the cursor.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Backgrounds"
  ],
  "docs": "Installed in components/furin/halftone-field/. Docs, live controls and props table: https://furin.dev/components/halftone-field/",
  "files": [
    {
      "path": "registry/halftone-field/HalftoneField.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./HalftoneField.css\";\n\nexport interface HalftoneFieldProps {\n  /** Contenido que va encima del semitono. */\n  children?: ReactNode;\n  /** Color de la tinta principal. */\n  ink?: string;\n  /** Color de la segunda tinta (se sobreimprime con otro ángulo). */\n  accent?: string;\n  /** Color del fondo (el papel). */\n  background?: string;\n  /** Separación entre puntos de la trama, en px. */\n  spacing?: number;\n  /** Qué tan rápido se mueve el campo. */\n  speed?: number;\n  /** Radio de influencia del cursor, en px. */\n  cursorRadius?: number;\n  /** Cuánto pesa la segunda tinta (0 la apaga). */\n  accentAmount?: number;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nlet colorCtx: CanvasRenderingContext2D | null = null;\nconst RGB_CACHE = new Map<string, [number, number, number]>();\nfunction rgb(css: string, def: [number, number, number]): [number, number, number] {\n  const hit = RGB_CACHE.get(css);\n  if (hit) return hit;\n  try {\n    colorCtx ??= document.createElement(\"canvas\").getContext(\"2d\", { willReadFrequently: true })!;\n    colorCtx.clearRect(0, 0, 1, 1);\n    colorCtx.fillStyle = \"#010203\";\n    colorCtx.fillStyle = css;\n    colorCtx.fillRect(0, 0, 1, 1);\n    const d = colorCtx.getImageData(0, 0, 1, 1).data;\n    const out: [number, number, number] = [d[0], d[1], d[2]];\n    if (RGB_CACHE.size > 64) RGB_CACHE.clear();\n    RGB_CACHE.set(css, out);\n    return out;\n  } catch {\n    return def;\n  }\n}\n\nconst smooth = (a: number, b: number, x: number) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };\n\n/** Fondo de semitono de imprenta: dos tramas giradas que crecen y se encogen con un campo que fluye y con el cursor. */\nexport function HalftoneField({\n  children,\n  ink = \"#e9e4d8\",\n  accent = \"#d9573a\",\n  background = \"#0b0c0f\",\n  spacing = 11,\n  speed = 1,\n  cursorRadius = 150,\n  accentAmount = 1,\n  className = \"\",\n  style,\n}: HalftoneFieldProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const props = useRef({ ink, accent, spacing, speed, cursorRadius, accentAmount });\n  props.current = { ink, accent, spacing, speed, cursorRadius, accentAmount };\n  const wakeRef = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const canvas = document.createElement(\"canvas\");\n    canvas.className = \"furin-halftone-field__lienzo\";\n    canvas.setAttribute(\"aria-hidden\", \"true\");\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    root.insertBefore(canvas, root.firstChild);\n\n    const reducido = matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let w = 0, h = 0, dpr = 1, raf = 0, visible = true, muerto = false, ult = 0;\n    let t = reducido ? 3.4 : 2;\n    const ptr = { x: -999, y: -999, tx: -999, ty: -999, k: 0, kObj: 0, hay: false };\n\n    // Una trama: ángulo, separación relativa, fase del campo y tinta.\n    const trama = (ang: number, sep: number, fase: number, rgbT: [number, number, number], alfa: number, peso: number) => {\n      const p = props.current;\n      const s = p.spacing * sep;\n      const ca = Math.cos(ang), sa = Math.sin(ang);\n      const R = Math.hypot(w, h) / 2 + s;\n      const n = Math.ceil(R / s);\n      const cx = w / 2, cy = h / 2;\n      const rMax = s * 0.54;\n      const rad = p.cursorRadius;\n      const inv2 = 1 / (2 * rad * rad);\n      ctx.beginPath();\n      for (let j = -n; j <= n; j++) {\n        for (let i = -n; i <= n; i++) {\n          const x = cx + (i * ca - j * sa) * s, y = cy + (i * sa + j * ca) * s;\n          if (x < -s || x > w + s || y < -s || y > h + s) continue;\n          const u = x / 330, v = y / 330;\n          // Campo: dos ondas deformadas que se multiplican; da manchas grandes y suaves de tono.\n          const f = Math.sin(u * 1.35 + Math.sin(v * 1.15 + t * 0.55 + fase) * 1.25 + t * 0.4 + fase) * Math.sin(v * 1.75 + Math.sin(u * 0.95 - t * 0.5) * 1.4 - t * 0.3 - fase);\n          let tono = smooth(-0.32, 0.95, f) * peso;\n          // Atenúa el centro para que un titular respire sobre la trama.\n          const dx0 = (x - cx) / w, dy0 = (y - cy) / h;\n          tono *= 0.45 + 0.55 * smooth(0.02, 0.2, dx0 * dx0 + dy0 * dy0);\n          if (ptr.k > 0.01) {\n            const dx = x - ptr.x, dy = y - ptr.y, d2 = dx * dx + dy * dy;\n            const g = Math.exp(-d2 * inv2);\n            // El cursor infla los puntos cercanos, con un anillo que respira alrededor.\n            tono += ptr.k * (0.5 * g + 0.12 * g * Math.cos(Math.sqrt(d2) * 0.09 - t * 6));\n          }\n          if (tono < 0.03) continue;\n          const r = rMax * Math.sqrt(Math.min(0.95, tono));\n          ctx.moveTo(x + r, y);\n          ctx.arc(x, y, r, 0, 6.2832);\n        }\n      }\n      ctx.fillStyle = `rgba(${rgbT[0]},${rgbT[1]},${rgbT[2]},${alfa})`;\n      ctx.fill();\n    };\n\n    const pintar = () => {\n      const p = props.current;\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, canvas.width, canvas.height);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      const a = rgb(p.accent, [217, 87, 58]);\n      const b = rgb(p.ink, [233, 228, 216]);\n      if (p.accentAmount > 0.01) trama((75 * Math.PI) / 180, 1.32, 2.1, a, 0.9, 0.78 * Math.min(1, p.accentAmount));\n      trama((15 * Math.PI) / 180, 1, 0, b, 0.9, 0.85);\n    };\n\n    const paso = (ts: number) => {\n      raf = 0;\n      if (muerto || !visible) return;\n      const dt = Math.min(0.05, ult ? (ts - ult) / 1000 : 0.016);\n      ult = ts;\n      t += dt * 0.22 * props.current.speed;\n      ptr.x += (ptr.tx - ptr.x) * (1 - Math.exp(-dt * 10)); ptr.y += (ptr.ty - ptr.y) * (1 - Math.exp(-dt * 10));\n      ptr.k += (ptr.kObj - ptr.k) * (1 - Math.exp(-dt * 5));\n      pintar();\n      raf = requestAnimationFrame(paso);\n    };\n    const wake = () => { if (reducido) return; if (!raf && visible && !muerto) { ult = 0; raf = requestAnimationFrame(paso); } };\n    wakeRef.current = () => { if (reducido) pintar(); else wake(); };\n\n    const medir = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      const r = root.getBoundingClientRect();\n      w = Math.max(1, r.width); h = Math.max(1, r.height);\n      canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr);\n      pintar();\n      wake();\n    };\n    const ro = new ResizeObserver(medir);\n    ro.observe(root);\n    const io = new IntersectionObserver(([e]) => { visible = e.isIntersecting; if (visible) wake(); }, { rootMargin: \"80px\" });\n    io.observe(root);\n\n    const onMove = (e: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      ptr.tx = e.clientX - r.left; ptr.ty = e.clientY - r.top;\n      if (!ptr.hay) { ptr.x = ptr.tx; ptr.y = ptr.ty; ptr.hay = true; }\n      ptr.kObj = 1;\n    };\n    const onLeave = () => { ptr.kObj = 0; ptr.hay = false; };\n    if (!reducido) {\n      root.addEventListener(\"pointermove\", onMove, { passive: true });\n      root.addEventListener(\"pointerdown\", onMove, { passive: true });\n      root.addEventListener(\"pointerleave\", onLeave);\n    }\n    medir();\n\n    return () => {\n      muerto = true;\n      wakeRef.current = null;\n      cancelAnimationFrame(raf);\n      ro.disconnect(); io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      canvas.remove();\n    };\n  }, []);\n\n  useEffect(() => { wakeRef.current?.(); }, [ink, accent, spacing, speed, cursorRadius, accentAmount]);\n\n  const vars = { \"--furin-halftone-field-bg\": background } as CSSProperties;\n  return (\n    <div ref={rootRef} className={`furin-halftone-field ${className}`} style={{ ...vars, ...style }}>\n      {children != null ? <div className=\"furin-halftone-field__contenido\">{children}</div> : null}\n    </div>\n  );\n}\n\nexport default HalftoneField;\n",
      "type": "registry:component",
      "target": "components/furin/halftone-field/HalftoneField.tsx"
    },
    {
      "path": "registry/halftone-field/HalftoneField.css",
      "content": ".furin-halftone-field {\n  position: absolute;\n  inset: 0;\n  display: grid;\n  place-items: center;\n  overflow: hidden;\n  isolation: isolate;\n  background: var(--furin-halftone-field-bg, #0b0c0f);\n  touch-action: pan-y;\n}\n.furin-halftone-field__lienzo { position: absolute; inset: 0; width: 100%; height: 100%; display: block; z-index: 0; }\n.furin-halftone-field__contenido { position: relative; z-index: 1; max-width: 100%; pointer-events: none; }\n.furin-halftone-field__contenido > * { pointer-events: auto; }\n",
      "type": "registry:file",
      "target": "components/furin/halftone-field/HalftoneField.css"
    }
  ]
}
