{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-bokeh",
  "type": "registry:component",
  "title": "Bokeh",
  "description": "Out-of-focus light discs with bright rims that drift upward while a slow focus pull sweeps through the depth, turning near discs into crisp points and far ones into soft glows.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "bokeh",
    "lights",
    "depth of field",
    "canvas",
    "warm"
  ],
  "docs": "https://hyperblur.dev/effects/bg-bokeh",
  "files": [
    {
      "path": "registry/hyperblur/bg-bokeh.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-bokeh.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgBokehProps extends React.HTMLAttributes<HTMLDivElement> {\n  colors?: string[];\n  background?: string;\n  count?: number;\n  shape?: \"circle\" | \"hexagon\";\n  focusPull?: boolean;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-vd10 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-vd10-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-vd10-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nconst defaultColors = [\"#ffb86b\", \"#ff7a8a\", \"#7fb3ff\", \"#ffe2a8\"];\n\nfunction hash(i: number) {\n  let x = Math.imul(i ^ 0x243f6a88, 0x85a308d3);\n  x ^= x >>> 16;\n  x = Math.imul(x, 0x13198a2e);\n  x ^= x >>> 13;\n  return (x >>> 0) / 4294967296;\n}\n\nfunction rgb(hex: string): [number, number, number] {\n  let s = hex.replace(\"#\", \"\");\n  if (s.length === 3) s = s.split(\"\").map((c) => c + c).join(\"\");\n  const n = parseInt(s.slice(0, 6), 16);\n  if (Number.isNaN(n)) return [255, 255, 255];\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nfunction sprite(c: [number, number, number], shape: \"circle\" | \"hexagon\", sharp: boolean) {\n  const S = 128;\n  const cv = document.createElement(\"canvas\");\n  cv.width = S;\n  cv.height = S;\n  const g = cv.getContext(\"2d\");\n  if (!g) return cv;\n  const m = S / 2;\n  const R = S / 2 - 2;\n  const col = (a: number) => `rgba(${c[0]},${c[1]},${c[2]},${a})`;\n  g.beginPath();\n  if (shape === \"hexagon\") {\n    for (let i = 0; i < 6; i++) {\n      const a = (i / 6) * Math.PI * 2 + Math.PI / 6;\n      const x = m + Math.cos(a) * R;\n      const y = m + Math.sin(a) * R;\n      if (i === 0) g.moveTo(x, y);\n      else g.lineTo(x, y);\n    }\n    g.closePath();\n  } else g.arc(m, m, R, 0, Math.PI * 2);\n  if (sharp) {\n    const grad = g.createRadialGradient(m - R * 0.15, m - R * 0.15, 0, m, m, R);\n    grad.addColorStop(0, col(0.42));\n    grad.addColorStop(0.7, col(0.32));\n    grad.addColorStop(0.9, col(0.55));\n    grad.addColorStop(1, col(0.95));\n    g.fillStyle = grad;\n    g.fill();\n    g.lineWidth = 2;\n    g.strokeStyle = col(0.9);\n    g.stroke();\n  } else {\n    g.save();\n    g.clip();\n    const grad = g.createRadialGradient(m, m, 0, m, m, R);\n    grad.addColorStop(0, col(0.55));\n    grad.addColorStop(0.6, col(0.35));\n    grad.addColorStop(1, col(0));\n    g.fillStyle = grad;\n    g.fillRect(0, 0, S, S);\n    g.restore();\n  }\n  return cv;\n}\n\nexport function BgBokeh({\n  colors = defaultColors,\n  background = \"#07060a\",\n  count = 38,\n  shape = \"circle\",\n  focusPull = true,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgBokehProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const colorKey = (colors.length ? colors : defaultColors).join(\"|\");\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 palette = colorKey.split(\"|\").map(rgb);\n    const sharpSprites = palette.map((c) => sprite(c, shape, true));\n    const softSprites = palette.map((c) => sprite(c, shape, false));\n    const N = Math.max(4, Math.min(160, Math.round(count)));\n    const discs = Array.from({ length: N }, (_, i) => ({\n      x: hash(i * 7 + 1),\n      y: hash(i * 7 + 2),\n      d: hash(i * 7 + 3),\n      size: 0.6 + 0.8 * hash(i * 7 + 4),\n      c: Math.floor(hash(i * 7 + 5) * palette.length),\n      vx: (hash(i * 7 + 6) - 0.5) * 0.012,\n      vy: -0.004 - 0.01 * hash(i * 7 + 7),\n      ph: hash(i * 7 + 8) * 6.28,\n    })).sort((a, b) => a.d - b.d);\n    const tone = rgb(background);\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 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      const warm = g.createRadialGradient(w * 0.3, h * 0.8, 0, w * 0.3, h * 0.8, Math.max(w, h) * 0.8);\n      const p0 = palette[0];\n      warm.addColorStop(0, `rgba(${p0[0]},${p0[1]},${p0[2]},0.16)`);\n      warm.addColorStop(1, `rgba(${tone[0]},${tone[1]},${tone[2]},0)`);\n      g.fillStyle = warm;\n      g.fillRect(0, 0, w, h);\n      g.globalCompositeOperation = \"lighter\";\n      const focus = focusPull ? 0.5 + 0.48 * Math.sin(t * 0.32) : 0.08;\n      const unit = Math.min(w, h);\n      for (const b of discs) {\n        const blur = Math.min(1, Math.abs(b.d - focus) * 1.6);\n        const r = unit * (0.012 + b.size * (0.018 + 0.11 * blur)) * (0.6 + b.d * 0.7);\n        const px = (((b.x + b.vx * t + 0.02 * Math.sin(t * 0.3 + b.ph)) % 1) + 1) % 1;\n        const py = (((b.y + b.vy * t) % 1) + 1) % 1;\n        const x = px * (w + r * 4) - r * 2;\n        const y = py * (h + r * 4) - r * 2;\n        const energy = Math.min(1, Math.pow((unit * 0.03) / r, 0.55));\n        const flicker = 0.85 + 0.15 * Math.sin(t * 1.3 + b.ph * 3);\n        const a = (0.25 + 0.75 * energy) * flicker;\n        if (blur < 0.98) {\n          g.globalAlpha = a * (1 - blur);\n          g.drawImage(sharpSprites[b.c], x - r, y - r, r * 2, r * 2);\n        }\n        g.globalAlpha = a * (0.25 + blur * 0.75);\n        g.drawImage(softSprites[b.c], x - r * 1.08, y - r * 1.08, r * 2.16, r * 2.16);\n      }\n      g.globalCompositeOperation = \"source-over\";\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 = 3;\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 = 3;\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  }, [colorKey, background, count, shape, focusPull, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-vd10 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-bokeh\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-vd10-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-vd10-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}