{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-crystal-facets",
  "type": "registry:component",
  "title": "Crystal Facets",
  "description": "A low-poly crystal surface whose facets breathe in depth while a light orbits above them. Facets that face the light glint, and a diagonal shimmer passes over the whole surface.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "low poly",
    "crystal",
    "facets",
    "canvas",
    "light"
  ],
  "docs": "https://hyperblur.dev/effects/bg-crystal-facets",
  "files": [
    {
      "path": "registry/hyperblur/bg-crystal-facets.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-crystal-facets.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgCrystalFacetsProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  facet?: number;\n  relief?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve9 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve9-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve9-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction rgbOf(value: string, fallback: [number, number, number]): [number, number, number] {\n  const raw = value.trim().replace(\"#\", \"\");\n  const hex = raw.length === 3 ? raw.split(\"\").map((c) => c + c).join(\"\") : raw;\n  const n = parseInt(hex.slice(0, 6), 16);\n  if (hex.length < 6 || Number.isNaN(n)) return fallback;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nfunction rnd(i: number) {\n  let x = Math.imul(i ^ 0x9e3779b9, 0x85ebca6b);\n  x ^= x >>> 13;\n  x = Math.imul(x, 0xc2b2ae35);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\nexport function BgCrystalFacets({\n  color = \"#28334d\",\n  accent = \"#9fd0ff\",\n  background = \"#05060a\",\n  facet = 84,\n  relief = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgCrystalFacetsProps) {\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 ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !ctx) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n\n    const base = rgbOf(color, [40, 51, 77]);\n    const hi = rgbOf(accent, [159, 208, 255]);\n    const bg = rgbOf(background, [5, 6, 10]);\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let cols = 0;\n    let rows = 0;\n    let cell = 0;\n    let px = new Float32Array(0);\n    let py = new Float32Array(0);\n    let pz = new Float32Array(0);\n    let ph = new Float32Array(0);\n    let flip = new Uint8Array(0);\n    let zbuf = new Float32Array(0);\n\n    function setup() {\n      cell = Math.max(36, facet);\n      cols = Math.ceil(w / cell) + 3;\n      rows = Math.ceil(h / cell) + 3;\n      const n = cols * rows;\n      px = new Float32Array(n);\n      py = new Float32Array(n);\n      pz = new Float32Array(n);\n      ph = new Float32Array(n);\n      flip = new Uint8Array(n);\n      zbuf = new Float32Array(n);\n      const ox = (w - (cols - 1) * cell) / 2;\n      const oy = (h - (rows - 1) * cell) / 2;\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          const i = r * cols + c;\n          const edge = r === 0 || c === 0 || r === rows - 1 || c === cols - 1;\n          px[i] = ox + c * cell + (edge ? 0 : (rnd(i * 3 + 1) - 0.5) * cell * 0.72);\n          py[i] = oy + r * cell + (edge ? 0 : (rnd(i * 3 + 2) - 0.5) * cell * 0.72);\n          pz[i] = rnd(i * 3 + 3);\n          ph[i] = rnd(i * 7 + 5) * 6.283;\n          flip[i] = rnd(i * 11 + 9) > 0.5 ? 1 : 0;\n        }\n      }\n    }\n\n    function tri(a: number, b: number, c: number, lx: number, ly: number, lz: number, sweep: number, zs: Float32Array) {\n      const ax = px[b] - px[a];\n      const ay = py[b] - py[a];\n      const az = (zs[b] - zs[a]) * cell * 0.55 * relief;\n      const bx = px[c] - px[a];\n      const by = py[c] - py[a];\n      const bz = (zs[c] - zs[a]) * cell * 0.55 * relief;\n      let nx = ay * bz - az * by;\n      let ny = az * bx - ax * bz;\n      let nz = ax * by - ay * bx;\n      if (nz < 0) {\n        nx = -nx;\n        ny = -ny;\n        nz = -nz;\n      }\n      const nl = Math.hypot(nx, ny, nz) || 1;\n      nx /= nl;\n      ny /= nl;\n      nz /= nl;\n      const lam = Math.max(0, nx * lx + ny * ly + nz * lz);\n      const hx = lx;\n      const hy = ly;\n      const hz = lz + 1;\n      const hl = Math.hypot(hx, hy, hz);\n      const spec = Math.pow(Math.max(0, (nx * hx + ny * hy + nz * hz) / hl), 36);\n      const cx = (px[a] + px[b] + px[c]) / 3;\n      const cy = (py[a] + py[b] + py[c]) / 3;\n      const dd = (cx + cy) / (w + h) - sweep;\n      const band = Math.exp(-(dd * dd) / 0.004) * 0.5;\n      const m = 0.22 + lam * 0.9;\n      const q = Math.min(1, spec + band * (0.4 + lam));\n      const r = Math.min(255, bg[0] + base[0] * m + (hi[0] - base[0] * m) * q);\n      const gg = Math.min(255, bg[1] + base[1] * m + (hi[1] - base[1] * m) * q);\n      const bb = Math.min(255, bg[2] + base[2] * m + (hi[2] - base[2] * m) * q);\n      g.fillStyle = `rgb(${r | 0},${gg | 0},${bb | 0})`;\n      g.beginPath();\n      g.moveTo(px[a], py[a]);\n      g.lineTo(px[b], py[b]);\n      g.lineTo(px[c], py[c]);\n      g.closePath();\n      g.fill();\n      g.strokeStyle = `rgba(${hi[0]},${hi[1]},${hi[2]},${(0.05 + q * 0.4).toFixed(3)})`;\n      g.stroke();\n    }\n\n    function render() {\n      if (!w || !h || !cols) return;\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      const zs = zbuf;\n      for (let i = 0; i < pz.length; i++) zs[i] = pz[i] + 0.22 * Math.sin(t * 0.7 + ph[i]);\n      const ang = t * 0.22;\n      const lx0 = Math.cos(ang) * 0.62;\n      const ly0 = Math.sin(ang) * 0.42 - 0.3;\n      const lz0 = 0.62;\n      const ll = Math.hypot(lx0, ly0, lz0);\n      const lx = lx0 / ll;\n      const ly = ly0 / ll;\n      const lz = lz0 / ll;\n      const sweep = ((t * 0.08) % 1.8) - 0.4;\n      g.lineWidth = 0.7;\n      g.lineJoin = \"round\";\n      for (let r = 0; r < rows - 1; r++) {\n        for (let c = 0; c < cols - 1; c++) {\n          const a = r * cols + c;\n          const b = a + 1;\n          const d = a + cols;\n          const e = d + 1;\n          if (flip[a]) {\n            tri(a, b, e, lx, ly, lz, sweep, zs);\n            tri(a, e, d, lx, ly, lz, sweep, zs);\n          } else {\n            tri(a, b, d, lx, ly, lz, sweep, zs);\n            tri(b, e, d, lx, ly, lz, sweep, zs);\n          }\n        }\n      }\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      t = 3.4;\n      render();\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      setup();\n      if (motion.matches) still();\n      else render();\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      render();\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) still();\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, facet, relief, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve9 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-crystal-facets\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve9-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve9-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}