{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-cloth",
  "type": "registry:component",
  "title": "Satin Cloth",
  "description": "A sheet of satin rippling in a slow breeze, lit per facet with soft diffuse light and a narrow sheen that slides along the folds.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "cloth",
    "satin",
    "fabric",
    "canvas",
    "waves"
  ],
  "docs": "https://hyperblur.dev/effects/bg-cloth",
  "files": [
    {
      "path": "registry/hyperblur/bg-cloth.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-cloth.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgClothProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  folds?: number;\n  sheen?: number;\n  detail?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve8 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve8-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve8-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\nexport function BgCloth({\n  color = \"#1a2b52\",\n  accent = \"#9cc2ff\",\n  background = \"#05060a\",\n  folds = 1,\n  sheen = 1,\n  detail = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgClothProps) {\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, [26, 43, 82]);\n    const hi = rgbOf(accent, [156, 194, 255]);\n    const lx0 = -0.45;\n    const ly0 = -0.62;\n    const lz0 = 0.64;\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 hx0 = lx;\n    const hy0 = ly;\n    const hz0 = lz + 1;\n    const hl = Math.hypot(hx0, hy0, hz0);\n    const hx = hx0 / hl;\n    const hy = hy0 / hl;\n    const hz = hz0 / hl;\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let cols = 0;\n    let rows = 0;\n    let vx = new Float32Array(0);\n    let vy = new Float32Array(0);\n    let sh = new Float32Array(0);\n    let sp = new Float32Array(0);\n\n    function height(x: number, y: number) {\n      const f = Math.max(0.2, folds);\n      return (\n        Math.sin(x * 0.55 * f + y * 0.22 - t * 0.9) * 1.0 +\n        Math.sin(x * 0.23 * f - y * 0.5 - t * 0.55 + 1.7) * 0.75 +\n        Math.sin((x * 0.9 + y * 1.1) * f - t * 1.6) * 0.16\n      );\n    }\n\n    function setup() {\n      const cell = Math.max(10, 18 / Math.max(0.4, detail));\n      cols = Math.min(110, Math.ceil(w / cell) + 3);\n      rows = Math.min(80, Math.ceil(h / cell) + 3);\n      const n = cols * rows;\n      vx = new Float32Array(n);\n      vy = new Float32Array(n);\n      sh = new Float32Array(n);\n      sp = new Float32Array(n);\n    }\n\n    function render() {\n      if (!w || !h || !cols) return;\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      const cw = (w + 40) / (cols - 1);\n      const ch = (h + 40) / (rows - 1);\n      const unit = Math.max(w, h) / 16;\n      const amp = Math.min(w, h) * 0.035;\n      const e = 0.35;\n      const gl = Math.max(0, sheen);\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          const i = r * cols + c;\n          const gx = (c * cw) / unit;\n          const gy = (r * ch) / unit;\n          const z = height(gx, gy);\n          const dx = (height(gx + e, gy) - z) / e;\n          const dy = (height(gx, gy + e) - z) / e;\n          const nl = Math.hypot(dx, dy, 1.6);\n          const nx = -dx / nl;\n          const ny = -dy / nl;\n          const nz = 1.6 / nl;\n          const lam = Math.max(0, nx * lx + ny * ly + nz * lz);\n          const ndh = Math.max(0, nx * hx + ny * hy + nz * hz);\n          sh[i] = lam;\n          sp[i] = Math.pow(ndh, 28) * gl + Math.pow(ndh, 6) * 0.12 * gl;\n          vx[i] = -20 + c * cw + dx * amp * 0.25;\n          vy[i] = -20 + r * ch - z * amp;\n        }\n      }\n      g.lineJoin = \"round\";\n      g.lineWidth = 0.8;\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 b2 = a + 1;\n          const d = a + cols;\n          const e2 = d + 1;\n          const s = (sh[a] + sh[b2] + sh[d] + sh[e2]) * 0.25;\n          const q = (sp[a] + sp[b2] + sp[d] + sp[e2]) * 0.25;\n          const m = 0.18 + s * 0.95;\n          const rr = Math.min(255, base[0] * m + hi[0] * q);\n          const gg = Math.min(255, base[1] * m + hi[1] * q);\n          const bb = Math.min(255, base[2] * m + hi[2] * q);\n          const col = `rgb(${rr | 0},${gg | 0},${bb | 0})`;\n          g.fillStyle = col;\n          g.strokeStyle = col;\n          g.beginPath();\n          g.moveTo(vx[a], vy[a]);\n          g.lineTo(vx[b2], vy[b2]);\n          g.lineTo(vx[e2], vy[e2]);\n          g.lineTo(vx[d], vy[d]);\n          g.closePath();\n          g.fill();\n          g.stroke();\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 = 2.2;\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, folds, sheen, detail, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve8 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-cloth\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve8-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve8-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}