{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-mesh-lattice",
  "type": "registry:component",
  "title": "Mesh Lattice",
  "description": "A smooth mesh gradient whose color points drift on their own paths, drawn with the lattice and handles of a design tool. One handle at a time is selected and shows its color value.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "mesh gradient",
    "design tool",
    "gradient",
    "canvas",
    "handles"
  ],
  "docs": "https://hyperblur.dev/effects/bg-mesh-lattice",
  "files": [
    {
      "path": "registry/hyperblur/bg-mesh-lattice.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-mesh-lattice.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgMeshLatticeProps extends React.HTMLAttributes<HTMLDivElement> {\n  colors?: string[];\n  accent?: string;\n  background?: string;\n  handles?: boolean;\n  softness?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-vd5 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-vd5-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-vd5-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nconst defaultColors = [\"#2f7bff\", \"#0b1f4d\", \"#7fb3ff\", \"#5b3df5\", \"#0a0a12\", \"#1e4fd6\"];\n\nfunction hash(i: number) {\n  let x = Math.imul(i ^ 0x3c6ef372, 0xa54ff53a);\n  x ^= x >>> 16;\n  x = Math.imul(x, 0x510e527f);\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 [0, 0, 0];\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nexport function BgMeshLattice({\n  colors = defaultColors,\n  accent = \"#ffffff\",\n  background = \"#06070d\",\n  handles = true,\n  softness = 1,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgMeshLatticeProps) {\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    const off = document.createElement(\"canvas\");\n    const octx = off.getContext(\"2d\");\n    if (!wrap || !canvas || !context || !octx) return;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const g: CanvasRenderingContext2D = context;\n    const o: CanvasRenderingContext2D = octx;\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const palette = colorKey.split(\"|\");\n    const COLS = 4;\n    const ROWS = 3;\n    const pts = Array.from({ length: COLS * ROWS }, (_, i) => {\n      const cx = i % COLS;\n      const cy = Math.floor(i / COLS);\n      const hexColor = palette[(i * 5 + Math.floor(hash(i + 3) * palette.length)) % palette.length];\n      return {\n        bx: -0.08 + (cx / (COLS - 1)) * 1.16,\n        by: -0.1 + (cy / (ROWS - 1)) * 1.2,\n        ax: 0.05 + 0.07 * hash(i * 4 + 1),\n        ay: 0.05 + 0.08 * hash(i * 4 + 2),\n        f: 0.12 + 0.14 * hash(i * 4 + 3),\n        ph: hash(i * 4 + 4) * 6.28,\n        hex: hexColor,\n        c: rgb(hexColor),\n        x: 0,\n        y: 0,\n      };\n    });\n    const bg = rgb(background);\n    let w = 0;\n    let h = 0;\n    let bw = 0;\n    let bh = 0;\n    let img: ImageData | null = null;\n    let t = 0;\n    let raf = 0;\n    let prev = 0;\n    let visible = true;\n\n    function place() {\n      for (const p of pts) {\n        p.x = p.bx + p.ax * Math.sin(t * p.f + p.ph);\n        p.y = p.by + p.ay * Math.cos(t * p.f * 0.83 + p.ph * 1.3);\n      }\n    }\n\n    function paint() {\n      if (!img) return;\n      const data = img.data;\n      const aspect = w / Math.max(1, h);\n      const sig = 0.2 * Math.max(0.3, softness);\n      const inv = 1 / (2 * sig * sig);\n      for (let y = 0; y < bh; y++) {\n        const v = y / (bh - 1);\n        for (let x = 0; x < bw; x++) {\n          const u = x / (bw - 1);\n          let r = 0;\n          let gg = 0;\n          let b = 0;\n          let ws = 0.02;\n          for (const p of pts) {\n            const dx = (u - p.x) * aspect;\n            const dy = v - p.y;\n            const wt = Math.exp(-(dx * dx + dy * dy) * inv);\n            r += p.c[0] * wt;\n            gg += p.c[1] * wt;\n            b += p.c[2] * wt;\n            ws += wt;\n          }\n          r = (r + bg[0] * 0.02) / ws;\n          gg = (gg + bg[1] * 0.02) / ws;\n          b = (b + bg[2] * 0.02) / ws;\n          const n = (((x * 7 + y * 13) % 5) - 2) * 0.9;\n          const i4 = (y * bw + x) * 4;\n          data[i4] = r + n;\n          data[i4 + 1] = gg + n;\n          data[i4 + 2] = b + n;\n          data[i4 + 3] = 255;\n        }\n      }\n      o.putImageData(img, 0, 0);\n    }\n\n    function lattice() {\n      const P = (i: number) => ({ x: pts[i].x * w, y: pts[i].y * h });\n      g.strokeStyle = accent;\n      g.lineWidth = 1;\n      g.globalAlpha = 0.13;\n      g.beginPath();\n      for (let r = 0; r < ROWS; r++) {\n        for (let c = 0; c < COLS; c++) {\n          const a = P(r * COLS + c);\n          if (c === 0) g.moveTo(a.x, a.y);\n          if (c < COLS - 1) {\n            const b = P(r * COLS + c + 1);\n            const m = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };\n            g.quadraticCurveTo(a.x + (m.x - a.x) * 0.6, a.y, m.x, m.y);\n            g.quadraticCurveTo(b.x - (b.x - m.x) * 0.6, b.y, b.x, b.y);\n          }\n        }\n      }\n      for (let c = 0; c < COLS; c++) {\n        for (let r = 0; r < ROWS; r++) {\n          const a = P(r * COLS + c);\n          if (r === 0) g.moveTo(a.x, a.y);\n          if (r < ROWS - 1) {\n            const b = P((r + 1) * COLS + c);\n            const m = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };\n            g.quadraticCurveTo(a.x, a.y + (m.y - a.y) * 0.6, m.x, m.y);\n            g.quadraticCurveTo(b.x, b.y - (b.y - m.y) * 0.6, b.x, b.y);\n          }\n        }\n      }\n      g.stroke();\n      const order = [5, 6, 2, 9, 1, 10];\n      const sel = order[Math.floor(t / 3.2) % order.length];\n      const phase = (t / 3.2) % 1;\n      const appear = Math.min(1, phase * 6, (1 - phase) * 6);\n      for (let i = 0; i < pts.length; i++) {\n        const p = P(i);\n        if (p.x < -6 || p.y < -6 || p.x > w + 6 || p.y > h + 6) continue;\n        const active = i === sel;\n        if (active && appear > 0) {\n          const left = P(i % COLS === 0 ? i : i - 1);\n          const right = P(i % COLS === COLS - 1 ? i : i + 1);\n          const hx = (right.x - left.x) * 0.18;\n          const hy = (right.y - left.y) * 0.18;\n          g.globalAlpha = 0.7 * appear;\n          g.beginPath();\n          g.moveTo(p.x - hx, p.y - hy);\n          g.lineTo(p.x + hx, p.y + hy);\n          g.stroke();\n          g.fillStyle = accent;\n          g.beginPath();\n          g.arc(p.x - hx, p.y - hy, 2.4, 0, Math.PI * 2);\n          g.arc(p.x + hx, p.y + hy, 2.4, 0, Math.PI * 2);\n          g.fill();\n          g.globalAlpha = 0.9 * appear;\n          g.font = \"500 9px ui-monospace, SFMono-Regular, Menlo, monospace\";\n          g.textBaseline = \"middle\";\n          const label = pts[i].hex.toUpperCase();\n          const tx = p.x + 10 > w - 60 ? p.x - 62 : p.x + 10;\n          g.fillStyle = \"rgba(0,0,0,0.55)\";\n          g.fillRect(tx - 4, p.y + 8, 56, 14);\n          g.fillStyle = accent;\n          g.fillText(label, tx, p.y + 15.5);\n        }\n        g.globalAlpha = active ? 1 : 0.55;\n        g.fillStyle = active ? accent : \"rgba(10,10,14,0.85)\";\n        g.fillRect(p.x - 3, p.y - 3, 6, 6);\n        g.strokeStyle = accent;\n        g.lineWidth = 1;\n        g.strokeRect(p.x - 3.5, p.y - 3.5, 7, 7);\n      }\n      g.globalAlpha = 1;\n    }\n\n    function draw() {\n      if (!w || !h) return;\n      place();\n      paint();\n      g.globalAlpha = 1;\n      g.imageSmoothingEnabled = true;\n      g.imageSmoothingQuality = \"high\";\n      g.drawImage(off, 0, 0, bw, bh, 0, 0, w, h);\n      if (handles) lattice();\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      bw = 72;\n      bh = Math.max(8, Math.min(96, Math.round((72 * h) / w)));\n      off.width = bw;\n      off.height = bh;\n      img = o.createImageData(bw, bh);\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  }, [colorKey, accent, background, handles, softness, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-vd5 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-mesh-lattice\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-vd5-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-vd5-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}