{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-contour-lines",
  "type": "registry:component",
  "title": "Contour Lines",
  "description": "A survey map of slowly reshaping hills: nested contour lines that merge where hills meet, bolder index lines with elevation labels, summit markers and edge ticks.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "topographic",
    "map",
    "contours",
    "canvas",
    "outdoor"
  ],
  "docs": "https://hyperblur.dev/effects/bg-contour-lines",
  "files": [
    {
      "path": "registry/hyperblur/bg-contour-lines.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-contour-lines.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgContourLinesProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  peaks?: number;\n  lines?: number;\n  labels?: boolean;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-vd3 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-vd3-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-vd3-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction hash(i: number) {\n  let x = Math.imul(i ^ 0x51ed270b, 0x2c1b3c6d);\n  x ^= x >>> 13;\n  x = Math.imul(x, 0x297a2d39);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\ntype Peak = { bx: number; by: number; r: number; p1: number; p2: number; p3: number; base: number; la: number };\n\nexport function BgContourLines({\n  color = \"#7fb3ff\",\n  accent = \"#2f7bff\",\n  background = \"#070a10\",\n  peaks = 3,\n  lines = 14,\n  labels = true,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgContourLinesProps) {\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 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 P = Math.max(1, Math.min(5, Math.round(peaks)));\n    const L = Math.max(4, Math.min(30, Math.round(lines)));\n    const anchors = [\n      [0.26, 0.4],\n      [0.74, 0.62],\n      [0.56, 0.18],\n      [0.12, 0.84],\n      [0.9, 0.16],\n    ];\n    const list: Peak[] = Array.from({ length: P }, (_, i) => ({\n      bx: anchors[i][0] + (hash(i * 9 + 1) - 0.5) * 0.06,\n      by: anchors[i][1] + (hash(i * 9 + 2) - 0.5) * 0.06,\n      r: 0.85 + 0.35 * hash(i * 9 + 3),\n      p1: hash(i * 9 + 4) * 6.28,\n      p2: hash(i * 9 + 5) * 6.28,\n      p3: hash(i * 9 + 6) * 6.28,\n      base: 1400 + Math.round(hash(i * 9 + 7) * 9) * 100,\n      la: hash(i * 9 + 8) * 6.28,\n    }));\n    const STEPS = 120;\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 center(pk: Peak) {\n      return {\n        x: w * (pk.bx + 0.025 * Math.sin(t * 0.05 + pk.p1)),\n        y: h * (pk.by + 0.03 * Math.cos(t * 0.04 + pk.p2)),\n        R: Math.min(w, h) * 0.62 * pk.r,\n      };\n    }\n\n    function shape(pk: Peak, a: number, s: number) {\n      return (\n        1 +\n        0.16 * Math.sin(3 * a + pk.p1 + t * 0.22 + s * 0.6) +\n        0.09 * Math.sin(5 * a + pk.p2 - t * 0.17 + s * 0.9) +\n        0.05 * Math.sin(8 * a + pk.p3 + t * 0.29)\n      );\n    }\n\n    function level(pk: Peak, c: { x: number; y: number; R: number }, x: number, y: number) {\n      const dx = x - c.x;\n      const dy = y - c.y;\n      const d = Math.sqrt(dx * dx + dy * dy);\n      const a = Math.atan2(dy, dx);\n      const s = d / c.R;\n      return 1 - d / (c.R * shape(pk, a, Math.min(1, s)));\n    }\n\n    function draw() {\n      if (!w || !h) return;\n      g.globalAlpha = 1;\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      g.strokeStyle = color;\n      g.globalAlpha = 0.14;\n      g.lineWidth = 1;\n      g.beginPath();\n      for (let x = 24; x < w; x += 24) {\n        const big = Math.round(x / 24) % 5 === 0;\n        g.moveTo(x + 0.5, 0);\n        g.lineTo(x + 0.5, big ? 8 : 4);\n        g.moveTo(x + 0.5, h);\n        g.lineTo(x + 0.5, h - (big ? 8 : 4));\n      }\n      for (let y = 24; y < h; y += 24) {\n        const big = Math.round(y / 24) % 5 === 0;\n        g.moveTo(0, y + 0.5);\n        g.lineTo(big ? 8 : 4, y + 0.5);\n        g.moveTo(w, y + 0.5);\n        g.lineTo(w - (big ? 8 : 4), y + 0.5);\n      }\n      g.stroke();\n      const cs = list.map(center);\n      const labelsToDraw: { x: number; y: number; a: number; text: string }[] = [];\n      for (let p = 0; p < P; p++) {\n        const pk = list[p];\n        const c = cs[p];\n        for (let k = 1; k <= L; k++) {\n          const s = k / L;\n          const lam = 1 - s;\n          const index = k % 5 === 0;\n          g.beginPath();\n          let pen = false;\n          for (let j = 0; j <= STEPS; j++) {\n            const a = (j / STEPS) * Math.PI * 2;\n            const r = c.R * s * shape(pk, a, s);\n            const x = c.x + Math.cos(a) * r;\n            const y = c.y + Math.sin(a) * r;\n            let hidden = false;\n            for (let q = 0; q < P; q++) {\n              if (q !== p && level(list[q], cs[q], x, y) > lam) {\n                hidden = true;\n                break;\n              }\n            }\n            if (hidden) {\n              pen = false;\n              continue;\n            }\n            if (pen) g.lineTo(x, y);\n            else g.moveTo(x, y);\n            pen = true;\n          }\n          g.globalAlpha = index ? 0.62 : 0.24 + 0.1 * (1 - s);\n          g.lineWidth = index ? 1.25 : 0.7;\n          g.strokeStyle = color;\n          g.stroke();\n          if (labels && index) {\n            const a = pk.la + t * 0.01;\n            const r = c.R * s * shape(pk, a, s);\n            const x = c.x + Math.cos(a) * r;\n            const y = c.y + Math.sin(a) * r;\n            let hidden = x < 16 || y < 12 || x > w - 16 || y > h - 12;\n            for (let q = 0; q < P && !hidden; q++) {\n              if (q !== p && level(list[q], cs[q], x, y) > lam) hidden = true;\n            }\n            if (!hidden) labelsToDraw.push({ x, y, a: a + Math.PI / 2, text: (pk.base + 20 - k * 20).toLocaleString(\"en-US\") });\n          }\n        }\n      }\n      g.font = \"500 9px ui-monospace, SFMono-Regular, Menlo, monospace\";\n      g.textAlign = \"center\";\n      g.textBaseline = \"middle\";\n      for (const lb of labelsToDraw) {\n        let ang = lb.a;\n        while (ang > Math.PI / 2) ang -= Math.PI;\n        while (ang < -Math.PI / 2) ang += Math.PI;\n        g.save();\n        g.translate(lb.x, lb.y);\n        g.rotate(ang);\n        const tw = g.measureText(lb.text).width + 6;\n        g.globalAlpha = 1;\n        g.fillStyle = background;\n        g.fillRect(-tw / 2, -6, tw, 12);\n        g.globalAlpha = 0.75;\n        g.fillStyle = color;\n        g.fillText(lb.text, 0, 0.5);\n        g.restore();\n      }\n      for (let p = 0; p < P; p++) {\n        const c = cs[p];\n        const pk = list[p];\n        const r = Math.max(2, (c.R / L) * 0.5);\n        g.globalAlpha = 0.22;\n        g.fillStyle = accent;\n        g.beginPath();\n        g.arc(c.x, c.y, r * 3.2, 0, Math.PI * 2);\n        g.fill();\n        g.globalAlpha = 1;\n        g.beginPath();\n        g.moveTo(c.x, c.y - 4.5);\n        g.lineTo(c.x + 4, c.y + 2.5);\n        g.lineTo(c.x - 4, c.y + 2.5);\n        g.closePath();\n        g.fill();\n        if (labels && c.x > 40 && c.x < w - 70 && c.y > 18) {\n          g.font = \"600 9px ui-monospace, SFMono-Regular, Menlo, monospace\";\n          g.textAlign = \"left\";\n          g.globalAlpha = 0.9;\n          g.fillStyle = color;\n          g.fillText(`${(pk.base + 20).toLocaleString(\"en-US\")} m`, c.x + 8, c.y - 4);\n        }\n      }\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 = 6;\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 = 6;\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  }, [color, accent, background, peaks, lines, labels, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-vd3 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-contour-lines\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-vd3-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-vd3-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}