{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-halftone",
  "type": "registry:component",
  "title": "Halftone",
  "description": "A moving gradient printed as a two-ink halftone: each ink has its own screen angle and a slight misregistration, and the dots swell and shrink as the tones drift.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "halftone",
    "print",
    "dots",
    "canvas",
    "editorial"
  ],
  "docs": "https://hyperblur.dev/effects/bg-halftone",
  "files": [
    {
      "path": "registry/hyperblur/bg-halftone.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-halftone.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgHalftoneProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  spacing?: number;\n  angle?: number;\n  misregister?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-vd15 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-vd15-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-vd15-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nexport function BgHalftone({\n  color = \"#ededed\",\n  accent = \"#2f7bff\",\n  background = \"#08080a\",\n  spacing = 9,\n  angle = 18,\n  misregister = 1.5,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgHalftoneProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const canvas = canvasRef.current;\n    const context = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !context) return;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const g: CanvasRenderingContext2D = context;\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const S = Math.max(5, Math.min(40, spacing));\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let raf = 0;\n    let prev = 0;\n    let visible = true;\n\n    function field(x: number, y: number, ch: number) {\n      const u = x / w;\n      const v = y / h;\n      const ox = ch ? 0.6 : 0;\n      const b1x = 0.3 + 0.22 * Math.sin(t * 0.23 + ox);\n      const b1y = 0.4 + 0.2 * Math.cos(t * 0.19 + ox * 1.5);\n      const b2x = 0.72 + 0.2 * Math.cos(t * 0.17 + 1.7 + ox);\n      const b2y = 0.62 + 0.22 * Math.sin(t * 0.21 + 0.4 - ox);\n      const a = w / Math.max(1, h);\n      const d1 = ((u - b1x) * a) ** 2 + (v - b1y) ** 2;\n      const d2 = ((u - b2x) * a) ** 2 + (v - b2y) ** 2;\n      const sweep = 0.5 + 0.5 * Math.sin(u * 2.4 - v * 1.6 + t * 0.35 + ox * 2);\n      const f = (ch ? 0.75 : 0.95) * Math.exp(-d1 / 0.09) + (ch ? 0.9 : 0.55) * Math.exp(-d2 / 0.12) + sweep * 0.18;\n      return Math.min(1, f);\n    }\n\n    function screen(deg: number, ch: number, fill: string, dx: number, dy: number) {\n      const a = (deg * Math.PI) / 180;\n      const ca = Math.cos(a);\n      const sa = Math.sin(a);\n      const cx = w / 2;\n      const cy = h / 2;\n      const half = Math.ceil(Math.hypot(w, h) / 2 / S) + 1;\n      const maxR = S * 0.56;\n      g.beginPath();\n      for (let j = -half; j <= half; j++) {\n        for (let i = -half; i <= half; i++) {\n          const lx = i * S;\n          const ly = j * S;\n          const x = cx + lx * ca - ly * sa + dx;\n          const y = cy + lx * sa + ly * ca + dy;\n          if (x < -S || y < -S || x > w + S || y > h + S) continue;\n          const f = field(x, y, ch);\n          if (f < 0.04) continue;\n          const r = maxR * Math.sqrt(f);\n          g.moveTo(x + r, y);\n          g.arc(x, y, r, 0, Math.PI * 2);\n        }\n      }\n      g.fillStyle = fill;\n      g.fill();\n    }\n\n    function draw() {\n      if (!w || !h) return;\n      g.globalCompositeOperation = \"source-over\";\n      g.globalAlpha = 1;\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      g.globalCompositeOperation = \"lighter\";\n      g.globalAlpha = 0.95;\n      screen(angle + 45, 1, accent, misregister, -misregister * 0.6);\n      g.globalAlpha = 0.85;\n      screen(angle, 0, color, 0, 0);\n      g.globalCompositeOperation = \"source-over\";\n      const vig = g.createRadialGradient(w / 2, h / 2, Math.min(w, h) * 0.25, w / 2, h / 2, Math.max(w, h) * 0.75);\n      vig.addColorStop(0, \"rgba(0,0,0,0)\");\n      vig.addColorStop(1, background);\n      g.globalAlpha = 0.85;\n      g.fillStyle = vig;\n      g.fillRect(0, 0, w, h);\n      g.globalAlpha = 1;\n    }\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      cv.width = Math.max(1, Math.round(w * dpr));\n      cv.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (motion.matches) t = 2;\n      draw();\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      draw();\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) {\n        t = 2;\n        draw();\n      }\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(el);\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n    };\n  }, [color, accent, background, spacing, angle, misregister, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-vd15 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-halftone\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-vd15-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-vd15-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}