{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-topography",
  "type": "registry:component",
  "title": "Topography",
  "description": "Contour lines of a slowly shifting terrain, like a living survey map. The pointer raises a hill that pulls the lines into rings around it.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "contour",
    "map",
    "terrain",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-topography",
  "files": [
    {
      "path": "registry/hyperblur/bg-topography.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-topography.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgTopographyProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  levels?: number;\n  scale?: number;\n  cellSize?: number;\n  lift?: number;\n  radius?: number;\n  speed?: number;\n  seed?: number;\n}\n\nconst css = `\n.hb-qj6 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }\n.hb-qj6-c { display: block; width: 100%; height: 100%; }\n`;\n\nfunction h3(x: number, y: number, z: number, seed: number) {\n  let v = (Math.imul(x, 0x27d4eb2d) ^ Math.imul(y, 0x165667b1) ^ Math.imul(z, 0x9e3779b1) ^ Math.imul(seed + 1, 0x85ebca6b)) >>> 0;\n  v = Math.imul(v ^ (v >>> 15), 0x2c1b3c6d) >>> 0;\n  v = Math.imul(v ^ (v >>> 12), 0x297a2d39) >>> 0;\n  v = (v ^ (v >>> 15)) >>> 0;\n  return v / 4294967296;\n}\n\nfunction noise(x: number, y: number, z: number, seed: number) {\n  const xi = Math.floor(x);\n  const yi = Math.floor(y);\n  const zi = Math.floor(z);\n  const xf = x - xi;\n  const yf = y - yi;\n  const zf = z - zi;\n  const u = xf * xf * (3 - 2 * xf);\n  const v = yf * yf * (3 - 2 * yf);\n  const s = zf * zf * (3 - 2 * zf);\n  const a = h3(xi, yi, zi, seed);\n  const b = h3(xi + 1, yi, zi, seed);\n  const c = h3(xi, yi + 1, zi, seed);\n  const d = h3(xi + 1, yi + 1, zi, seed);\n  const e = h3(xi, yi, zi + 1, seed);\n  const f = h3(xi + 1, yi, zi + 1, seed);\n  const g = h3(xi, yi + 1, zi + 1, seed);\n  const k = h3(xi + 1, yi + 1, zi + 1, seed);\n  const l0 = a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v;\n  const l1 = e + (f - e) * u + (g - e) * v + (e - f - g + k) * u * v;\n  return l0 + (l1 - l0) * s;\n}\n\nexport function BgTopography({\n  color = \"rgba(255,255,255,0.16)\",\n  accent = \"#7fb3ff\",\n  levels = 12,\n  scale = 190,\n  cellSize = 10,\n  lift = 0.55,\n  radius = 150,\n  speed = 1,\n  seed = 11,\n  className = \"\",\n  style,\n  ...rest\n}: BgTopographyProps) {\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 host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx) return;\n\n    const R = Math.max(20, radius);\n    const inv = 1 / (R * R);\n    const sc = 1 / Math.max(30, scale);\n    const stepV = 1 / Math.max(3, Math.min(40, levels));\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let placed = false;\n    let cell = Math.max(5, cellSize);\n    let cols = 0;\n    let rows = 0;\n    let field = new Float32Array(0);\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.3 * Math.sin(tt * 0.33 + 1.1));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.28 * Math.sin(tt * 0.52 + 0.2));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed) {\n        px = gx;\n        py = gy;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 7 : 2));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n    }\n\n    function layout() {\n      cell = Math.max(5, cellSize);\n      while ((w / cell + 1) * (h / cell + 1) > 16000) cell *= 1.15;\n      cols = Math.ceil(w / cell) + 1;\n      rows = Math.ceil(h / cell) + 1;\n      field = new Float32Array(cols * rows);\n    }\n\n    function sample() {\n      const z = t * 0.09;\n      const bump = lift * (0.85 + 0.15 * Math.sin(t * 1.1));\n      for (let r = 0; r < rows; r++) {\n        const y = r * cell;\n        for (let c = 0; c < cols; c++) {\n          const x = c * cell;\n          const nx = x * sc;\n          const ny = y * sc;\n          let v = 0.68 * noise(nx, ny, z, seed) + 0.32 * noise(nx * 2.1 + 5.2, ny * 2.1 + 1.3, z * 1.6, seed + 7);\n          const dx = x - px;\n          const dy = y - py;\n          v += bump * Math.exp(-(dx * dx + dy * dy) * inv);\n          field[r * cols + c] = v;\n        }\n      }\n    }\n\n    function draw() {\n      if (!ctx || !w || !h) return;\n      sample();\n      ctx.clearRect(0, 0, w, h);\n      const minor = new Path2D();\n      const major = new Path2D();\n      const top = 1 + lift;\n      let index = 0;\n      for (let th = stepV; th < top; th += stepV) {\n        index++;\n        const path = index % 4 === 0 ? major : minor;\n        for (let r = 0; r < rows - 1; r++) {\n          const y0 = r * cell;\n          const y1 = y0 + cell;\n          for (let c = 0; c < cols - 1; c++) {\n            const i = r * cols + c;\n            const a = field[i];\n            const b = field[i + 1];\n            const d = field[i + cols];\n            const e = field[i + cols + 1];\n            const ia = a > th;\n            const ib = b > th;\n            const ie = e > th;\n            const id = d > th;\n            if (ia === ib && ib === ie && ie === id) continue;\n            const x0 = c * cell;\n            const x1 = x0 + cell;\n            const pts: number[] = [];\n            let tX = 0;\n            let rY = 0;\n            let bX = 0;\n            let lY = 0;\n            const hasT = ia !== ib;\n            const hasR = ib !== ie;\n            const hasB = id !== ie;\n            const hasL = ia !== id;\n            if (hasT) tX = x0 + ((th - a) / (b - a)) * cell;\n            if (hasR) rY = y0 + ((th - b) / (e - b)) * cell;\n            if (hasB) bX = x0 + ((th - d) / (e - d)) * cell;\n            if (hasL) lY = y0 + ((th - a) / (d - a)) * cell;\n            if (hasT && hasR && hasB && hasL) {\n              const center = (a + b + d + e) / 4 > th;\n              if (ia === center) {\n                path.moveTo(tX, y0);\n                path.lineTo(x1, rY);\n                path.moveTo(bX, y1);\n                path.lineTo(x0, lY);\n              } else {\n                path.moveTo(tX, y0);\n                path.lineTo(x0, lY);\n                path.moveTo(x1, rY);\n                path.lineTo(bX, y1);\n              }\n              continue;\n            }\n            if (hasT) pts.push(tX, y0);\n            if (hasR) pts.push(x1, rY);\n            if (hasB) pts.push(bX, y1);\n            if (hasL) pts.push(x0, lY);\n            if (pts.length === 4) {\n              path.moveTo(pts[0], pts[1]);\n              path.lineTo(pts[2], pts[3]);\n            }\n          }\n        }\n      }\n      const glow = ctx.createRadialGradient(px, py, 0, px, py, R * 1.6);\n      glow.addColorStop(0, accent);\n      glow.addColorStop(1, color);\n      ctx.lineJoin = \"round\";\n      ctx.lineWidth = 1;\n      ctx.strokeStyle = glow;\n      ctx.stroke(minor);\n      ctx.globalAlpha = 0.85;\n      ctx.lineWidth = 1.4;\n      ctx.strokeStyle = accent;\n      ctx.stroke(major);\n      ctx.globalAlpha = 1;\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !ctx) return;\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      layout();\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\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.6;\n      inside = false;\n      placed = false;\n      track(0);\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      track(dt);\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) still();\n    }\n\n    function move(e: PointerEvent) {\n      if (!host || motion.matches) return;\n      const rect = host.getBoundingClientRect();\n      const sx = rect.width ? host.clientWidth / rect.width : 1;\n      const sy = rect.height ? host.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * sx;\n      ty = (e.clientY - rect.top) * sy;\n      inside = true;\n    }\n\n    function leave(e: PointerEvent) {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\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(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\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      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [color, accent, levels, scale, cellSize, lift, radius, speed, seed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qj6 ${className}`} style={style} aria-hidden=\"true\" {...rest}>\n      <style href=\"hb-bg-topography\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qj6-c\" />\n    </div>\n  );\n}\n"
    }
  ]
}