{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "map-heat-dots",
  "type": "registry:component",
  "title": "Map Heat Dots",
  "description": "A dot-density heat map over a coastal city silhouette with a slow shimmer. Hovering or moving with the arrow keys reads the value and district of a dot.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "maps",
    "map",
    "heat map",
    "dot density",
    "data viz",
    "tooltip",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/map-heat-dots",
  "files": [
    {
      "path": "registry/hyperblur/map-heat-dots.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/map-heat-dots.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type HeatMetric = \"footfall\" | \"temperature\" | \"noise\" | \"signal\";\n\nexport interface MapHeatDotsProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  metric?: HeatMetric;\n  accent?: string;\n  coldColor?: string;\n  seed?: number;\n  density?: number;\n  title?: string;\n  subtitle?: string;\n}\n\ntype C3 = [number, number, number];\ntype Spot = { u: number; v: number; amp: number; r: number; name: string };\ntype Dot = { x: number; y: number; val: number; ph: number; spot: number };\n\nconst METRICS: Record<HeatMetric, { label: string; unit: string; min: number; max: number; digits: number; sub: string }> = {\n  footfall: { label: \"Footfall\", unit: \"people/h\", min: 0, max: 2400, digits: 0, sub: \"Saturday, 14:00\" },\n  temperature: { label: \"Surface temperature\", unit: \"°C\", min: 16, max: 39, digits: 1, sub: \"July average, 15:00\" },\n  noise: { label: \"Noise level\", unit: \"dB\", min: 38, max: 84, digits: 0, sub: \"Weekday peak\" },\n  signal: { label: \"5G throughput\", unit: \"Mbps\", min: 40, max: 960, digits: 0, sub: \"Median, last 7 days\" },\n};\n\nconst NAMES = [\"Harbor Front\", \"Old Town\", \"Market Hall\", \"Central Station\", \"Northbank\", \"Mill Quarter\", \"Lighthouse Point\"];\nconst RM_QUERY = \"(prefers-reduced-motion: reduce)\";\n\nfunction mix32(n: number) {\n  let x = n | 0;\n  x = Math.imul(x ^ (x >>> 16), 0x7feb352d);\n  x = Math.imul(x ^ (x >>> 15), 0x846ca68b);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\nfunction rnd(seed: number, a: number, b = 0) {\n  return mix32(seed * 374761393 + a * 668265263 + b * 2246822519 + 1013904223);\n}\n\nfunction coast(u: number, seed: number) {\n  return 0.3 + 0.09 * Math.sin(u * 5.1 + seed) + 0.045 * Math.sin(u * 13.7 + seed * 2.3) + 0.02 * Math.sin(u * 31 + seed);\n}\n\nfunction isLand(u: number, v: number, seed: number) {\n  if (((u - 0.17) / 0.075) ** 2 + ((v - 0.17) / 0.05) ** 2 < 1) return true;\n  if (((u - 0.86) / 0.05) ** 2 + ((v - 0.12) / 0.035) ** 2 < 1) return true;\n  if (v < coast(u, seed)) return false;\n  if (((u - 0.62) / 0.13) ** 2 + ((v - coast(0.62, seed) - 0.02) / 0.15) ** 2 < 1) return false;\n  const rx = 0.36 + 0.05 * Math.sin(v * 10 + seed);\n  if (Math.abs(u - rx) < 0.011 && v > 0.2) return false;\n  return true;\n}\n\nfunction buildSpots(seed: number): Spot[] {\n  const out: Spot[] = [];\n  for (let i = 0; i < 6; i++) {\n    let u = 0.08 + rnd(seed, i, 1) * 0.84;\n    let v = 0.32 + rnd(seed, i, 2) * 0.6;\n    for (let tries = 0; tries < 12 && !isLand(u, v, seed); tries++) {\n      u = 0.08 + rnd(seed, i, 10 + tries) * 0.84;\n      v = 0.4 + rnd(seed, i, 30 + tries) * 0.52;\n    }\n    out.push({ u, v, amp: 0.45 + rnd(seed, i, 3) * 0.55, r: 0.07 + rnd(seed, i, 4) * 0.09, name: NAMES[(i + seed) % NAMES.length] });\n  }\n  out.push({ u: 0.17, v: 0.17, amp: 0.55, r: 0.05, name: \"Lighthouse Point\" });\n  return out;\n}\n\nfunction mixc(a: C3, b: C3, t: number): C3 {\n  return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];\n}\n\nfunction ramp(cold: C3, hot: C3, t: number): C3 {\n  if (t < 0.7) return mixc(cold, hot, t / 0.7);\n  return mixc(hot, [255, 240, 225], (t - 0.7) / 0.3);\n}\n\nfunction parseColor(ctx: CanvasRenderingContext2D, value: string, fb: C3): C3 {\n  ctx.fillStyle = \"#000000\";\n  ctx.fillStyle = value;\n  const v = String(ctx.fillStyle);\n  if (v[0] === \"#\" && v.length === 7) {\n    return [parseInt(v.slice(1, 3), 16), parseInt(v.slice(3, 5), 16), parseInt(v.slice(5, 7), 16)];\n  }\n  const m = v.match(/[\\d.]+/g);\n  return m && m.length >= 3 ? [Number(m[0]), Number(m[1]), Number(m[2])] : fb;\n}\n\nfunction format(metric: HeatMetric, t: number) {\n  const m = METRICS[metric];\n  const n = m.min + (m.max - m.min) * t;\n  const s = m.digits ? n.toFixed(m.digits) : Math.round(n).toLocaleString(\"en-US\");\n  return `${s} ${m.unit}`;\n}\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(RM_QUERY);\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\nconst css = `\n.hb-ub5 {\n  --hb-ub5-a: #ff4d4d;\n  --hb-ub5-c: #2c4270;\n  position: relative;\n  box-sizing: border-box;\n  width: 100%;\n  aspect-ratio: 16 / 10;\n  min-height: 170px;\n  overflow: hidden;\n  isolation: isolate;\n  border-radius: 14px;\n  border: 1px solid rgba(255, 255, 255, 0.09);\n  background: radial-gradient(120% 90% at 30% 0%, #0d1220, #08090c 70%);\n  color: #ededed;\n  font-family: inherit;\n}\n.hb-ub5 *, .hb-ub5 *::before, .hb-ub5 *::after { box-sizing: border-box; }\n.hb-ub5-stage { position: absolute; inset: 0; outline: none; cursor: crosshair; }\n.hb-ub5-stage:focus-visible { box-shadow: inset 0 0 0 2px var(--hb-ub5-a); border-radius: 13px; }\n.hb-ub5-cv { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ub5-head { position: absolute; left: 12px; top: 10px; display: flex; flex-direction: column; gap: 3px; pointer-events: none; }\n.hb-ub5-t { font-size: 13px; font-weight: 600; line-height: 1.2; white-space: nowrap; }\n.hb-ub5-s { font: 600 8.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.14em; text-transform: uppercase; color: #8f8f8f; white-space: nowrap; }\n.hb-ub5-lg {\n  position: absolute;\n  right: 8px;\n  bottom: 8px;\n  display: grid;\n  grid-template-columns: 1fr auto;\n  gap: 4px 8px;\n  width: 152px;\n  padding: 7px 8px;\n  border-radius: 9px;\n  background: rgba(8, 9, 12, 0.72);\n  border: 1px solid rgba(255, 255, 255, 0.06);\n  pointer-events: none;\n}\n.hb-ub5-bar {\n  grid-column: 1 / span 2;\n  height: 5px;\n  border-radius: 3px;\n  background: linear-gradient(90deg, var(--hb-ub5-c), var(--hb-ub5-a) 70%, #fff0e1);\n}\n.hb-ub5-lg span { font: 500 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace; color: #8f8f8f; font-variant-numeric: tabular-nums; }\n.hb-ub5-lg span:last-child { text-align: right; }\n.hb-ub5-tip {\n  position: absolute;\n  left: 0;\n  top: 0;\n  z-index: 2;\n  min-width: 118px;\n  padding: 7px 9px 8px;\n  border-radius: 10px;\n  background: rgba(14, 14, 16, 0.94);\n  border: 1px solid rgba(255, 255, 255, 0.1);\n  box-shadow: 0 12px 28px -10px rgba(0, 0, 0, 0.8);\n  pointer-events: none;\n  opacity: 0;\n  transition: opacity 0.15s ease;\n  white-space: nowrap;\n}\n.hb-ub5-tip[data-on=\"true\"] { opacity: 1; }\n.hb-ub5-tn { display: block; font: 600 8.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.14em; text-transform: uppercase; color: #8f8f8f; }\n.hb-ub5-tv { display: flex; align-items: center; gap: 6px; margin-top: 5px; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }\n.hb-ub5-tv i { width: 8px; height: 8px; border-radius: 50%; background: var(--hb-ub5-dot, var(--hb-ub5-a)); box-shadow: 0 0 8px var(--hb-ub5-dot, var(--hb-ub5-a)); }\n.hb-ub5-sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-ub5-tip { transition: none; }\n}\n`;\n\nexport function MapHeatDots({\n  metric = \"footfall\",\n  accent = \"#ff4d4d\",\n  coldColor = \"#2c4270\",\n  seed = 3,\n  density = 58,\n  title,\n  subtitle,\n  className = \"\",\n  style,\n  ...rest\n}: MapHeatDotsProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const stageRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const tipRef = React.useRef<HTMLDivElement>(null);\n  const tipName = React.useRef<HTMLSpanElement>(null);\n  const tipVal = React.useRef<HTMLSpanElement>(null);\n  const liveRef = React.useRef<HTMLSpanElement>(null);\n  const reduced = React.useSyncExternalStore(subscribeMotion, () => window.matchMedia(RM_QUERY).matches, () => false);\n  const info = METRICS[metric] ?? METRICS.footfall;\n  const st = React.useRef({ metric, accent, cold: coldColor, reduced: false, redraw: () => {} });\n\n  React.useEffect(() => {\n    const s = st.current;\n    s.metric = metric;\n    s.accent = accent;\n    s.cold = coldColor;\n    s.reduced = reduced;\n    s.redraw();\n  }, [metric, accent, coldColor, reduced]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const stage = stageRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!root || !stage || !canvas || !ctx) return;\n    const s = st.current;\n    const spots = buildSpots(seed);\n    let dots: Dot[] = [];\n    let sea: number[] = [];\n    let cols = 0;\n    let rows = 0;\n    let grid: number[] = [];\n    let sp = 8;\n    let w = 0;\n    let h = 0;\n    let dpr = 1;\n    let raf = 0;\n    let visible = true;\n    let hover = -1;\n    let hot: C3 = [255, 77, 77];\n    let cold: C3 = [44, 66, 112];\n    let colorsFor = \"\";\n\n    const layout = () => {\n      const r = root.getBoundingClientRect();\n      if (r.width < 2 || r.height < 2) return false;\n      w = r.width;\n      h = r.height;\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      canvas.width = Math.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      sp = Math.max(5.5, w / Math.max(20, density));\n      cols = Math.floor(w / sp);\n      rows = Math.floor(h / sp);\n      const ox = (w - (cols - 1) * sp) / 2;\n      const oy = (h - (rows - 1) * sp) / 2;\n      dots = [];\n      sea = [];\n      grid = new Array(cols * rows).fill(-1);\n      for (let j = 0; j < rows; j++) {\n        for (let i = 0; i < cols; i++) {\n          const x = ox + i * sp;\n          const y = oy + j * sp;\n          const u = x / w;\n          const v = y / h;\n          if (!isLand(u, v, seed)) {\n            if ((i + j) % 2 === 0) sea.push(x, y);\n            continue;\n          }\n          let val = 0;\n          let best = 0;\n          let bestV = -1;\n          spots.forEach((p, k) => {\n            const d2 = ((u - p.u) * 1.6) ** 2 + (v - p.v) ** 2;\n            const c = p.amp * Math.exp(-d2 / (2 * p.r * p.r));\n            val += c;\n            if (c > bestV) {\n              bestV = c;\n              best = k;\n            }\n          });\n          val = Math.min(1, val * 0.8 + (rnd(seed, i, j) - 0.5) * 0.08 + 0.04);\n          val = Math.max(0, val);\n          grid[j * cols + i] = dots.length;\n          dots.push({ x, y, val, ph: rnd(seed, i + 500, j) * Math.PI * 2, spot: best });\n        }\n      }\n      return true;\n    };\n\n    const draw = (t: number) => {\n      if (colorsFor !== s.accent + s.cold) {\n        hot = parseColor(ctx, s.accent, [255, 77, 77]);\n        cold = parseColor(ctx, s.cold, [44, 66, 112]);\n        colorsFor = s.accent + s.cold;\n      }\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, w, h);\n      ctx.beginPath();\n      for (let i = 0; i < sea.length; i += 2) ctx.rect(sea[i] - 0.5, sea[i + 1] - 0.5, 1, 1);\n      ctx.fillStyle = \"rgba(127,179,255,0.09)\";\n      ctx.fill();\n      const base = sp * 0.42;\n      const time = s.reduced ? 0 : t / 1000;\n      const buckets: Path2D[] = [];\n      const B = 10;\n      const S = 3;\n      for (let i = 0; i < B * S; i++) buckets.push(new Path2D());\n      const glow = new Path2D();\n      for (const d of dots) {\n        const wave = s.reduced ? 0.5 : 0.5 + 0.5 * Math.sin(time * 1.7 - d.x * 0.018 - d.y * 0.012 + d.ph * 0.35);\n        const vb = Math.min(B - 1, Math.floor(d.val * B));\n        const sb = Math.min(S - 1, Math.floor(wave * S));\n        const r = base * (0.42 + 0.58 * d.val) * (0.9 + 0.12 * wave * d.val);\n        const p = buckets[vb * S + sb];\n        p.moveTo(d.x + r, d.y);\n        p.arc(d.x, d.y, r, 0, Math.PI * 2);\n        if (d.val > 0.72) {\n          glow.moveTo(d.x + r * 2.4, d.y);\n          glow.arc(d.x, d.y, r * 2.4, 0, Math.PI * 2);\n        }\n      }\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.fillStyle = `rgba(${hot[0]},${hot[1]},${hot[2]},0.05)`;\n      ctx.fill(glow);\n      ctx.globalCompositeOperation = \"source-over\";\n      for (let vb = 0; vb < B; vb++) {\n        const c = ramp(cold, hot, (vb + 0.5) / B);\n        for (let sb = 0; sb < S; sb++) {\n          const a = 0.6 + ((vb + 0.5) / B) * 0.3 + (sb / (S - 1)) * 0.12;\n          ctx.fillStyle = `rgba(${Math.round(c[0])},${Math.round(c[1])},${Math.round(c[2])},${Math.min(1, a).toFixed(3)})`;\n          ctx.fill(buckets[vb * S + sb]);\n        }\n      }\n      if (hover >= 0 && dots[hover]) {\n        const d = dots[hover];\n        ctx.beginPath();\n        ctx.arc(d.x, d.y, sp * 0.95, 0, Math.PI * 2);\n        ctx.strokeStyle = \"rgba(255,255,255,0.9)\";\n        ctx.lineWidth = 1.3;\n        ctx.stroke();\n      }\n    };\n\n    const frame = (t: number) => {\n      raf = 0;\n      draw(t);\n      if (visible && !s.reduced) raf = requestAnimationFrame(frame);\n    };\n\n    const kick = () => {\n      if (!visible) return;\n      if (s.reduced) {\n        draw(0);\n        return;\n      }\n      if (!raf) raf = requestAnimationFrame(frame);\n    };\n\n    const showTip = (idx: number) => {\n      hover = idx;\n      const tip = tipRef.current;\n      if (!tip) return;\n      if (idx < 0) {\n        tip.dataset.on = \"false\";\n        if (s.reduced) draw(0);\n        return;\n      }\n      const d = dots[idx];\n      const name = spots[d.spot].name;\n      const value = format(s.metric as HeatMetric, d.val);\n      if (tipName.current) tipName.current.textContent = name;\n      if (tipVal.current) tipVal.current.textContent = value;\n      const c = ramp(cold, hot, d.val);\n      tip.style.setProperty(\"--hb-ub5-dot\", `rgb(${Math.round(c[0])},${Math.round(c[1])},${Math.round(c[2])})`);\n      const tw = tip.offsetWidth || 130;\n      const th = tip.offsetHeight || 46;\n      let x = d.x + 12;\n      let y = d.y - th - 10;\n      if (x + tw > w - 6) x = d.x - tw - 12;\n      if (y < 6) y = d.y + 12;\n      tip.style.transform = `translate(${Math.round(Math.max(6, x))}px, ${Math.round(Math.min(h - th - 6, y))}px)`;\n      tip.dataset.on = \"true\";\n      if (s.reduced) draw(0);\n    };\n\n    const nearest = (x: number, y: number) => {\n      const ox = (w - (cols - 1) * sp) / 2;\n      const oy = (h - (rows - 1) * sp) / 2;\n      const ci = Math.round((x - ox) / sp);\n      const cj = Math.round((y - oy) / sp);\n      let best = -1;\n      let bd = Infinity;\n      for (let j = cj - 1; j <= cj + 1; j++) {\n        for (let i = ci - 1; i <= ci + 1; i++) {\n          if (i < 0 || j < 0 || i >= cols || j >= rows) continue;\n          const k = grid[j * cols + i];\n          if (k < 0) continue;\n          const dd = (dots[k].x - x) ** 2 + (dots[k].y - y) ** 2;\n          if (dd < bd) {\n            bd = dd;\n            best = k;\n          }\n        }\n      }\n      return best;\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = stage.getBoundingClientRect();\n      showTip(nearest(e.clientX - r.left, e.clientY - r.top));\n    };\n    const onLeave = () => {\n      if (document.activeElement !== stage) showTip(-1);\n    };\n    const onFocus = () => {\n      if (hover >= 0) return;\n      let best = 0;\n      dots.forEach((d, k) => {\n        if (d.val > dots[best].val) best = k;\n      });\n      showTip(best);\n    };\n    const onBlur = () => showTip(-1);\n    const onKey = (e: KeyboardEvent) => {\n      const dirs: Record<string, [number, number]> = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] };\n      if (e.key === \"Escape\") {\n        showTip(-1);\n        return;\n      }\n      const dir = dirs[e.key];\n      if (!dir || !dots.length) return;\n      e.preventDefault();\n      const cur = dots[hover >= 0 ? hover : 0];\n      const ox = (w - (cols - 1) * sp) / 2;\n      const oy = (h - (rows - 1) * sp) / 2;\n      let i = Math.round((cur.x - ox) / sp);\n      let j = Math.round((cur.y - oy) / sp);\n      const step = e.shiftKey ? 4 : 2;\n      for (let n = 0; n < cols + rows; n++) {\n        i += dir[0] * step;\n        j += dir[1] * step;\n        if (i < 0 || j < 0 || i >= cols || j >= rows) return;\n        const k = grid[j * cols + i];\n        if (k >= 0) {\n          showTip(k);\n          const d = dots[k];\n          if (liveRef.current) liveRef.current.textContent = `${spots[d.spot].name}: ${format(s.metric as HeatMetric, d.val)}`;\n          return;\n        }\n      }\n    };\n\n    s.redraw = () => {\n      if (hover >= 0) showTip(hover);\n      if (s.reduced) draw(0);\n      else kick();\n    };\n\n    s.reduced = window.matchMedia(RM_QUERY).matches;\n    const resize = () => {\n      if (!layout()) return;\n      hover = -1;\n      if (tipRef.current) tipRef.current.dataset.on = \"false\";\n      draw(performance.now());\n    };\n    resize();\n    stage.addEventListener(\"pointermove\", onMove);\n    stage.addEventListener(\"pointerleave\", onLeave);\n    stage.addEventListener(\"focus\", onFocus);\n    stage.addEventListener(\"blur\", onBlur);\n    stage.addEventListener(\"keydown\", onKey);\n    const ro = new ResizeObserver(resize);\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) kick();\n    });\n    io.observe(root);\n    kick();\n    return () => {\n      stage.removeEventListener(\"pointermove\", onMove);\n      stage.removeEventListener(\"pointerleave\", onLeave);\n      stage.removeEventListener(\"focus\", onFocus);\n      stage.removeEventListener(\"blur\", onBlur);\n      stage.removeEventListener(\"keydown\", onKey);\n      ro.disconnect();\n      io.disconnect();\n      cancelAnimationFrame(raf);\n      s.redraw = () => {};\n    };\n  }, [seed, density]);\n\n  return (\n    <div\n      ref={rootRef}\n      className={`hb-ub5 ${className}`.trim()}\n      style={{ \"--hb-ub5-a\": accent, \"--hb-ub5-c\": coldColor, ...style } as React.CSSProperties}\n      {...rest}\n    >\n      <style href=\"hb-map-heat-dots\" precedence=\"default\">\n        {css}\n      </style>\n      <div\n        ref={stageRef}\n        className=\"hb-ub5-stage\"\n        tabIndex={0}\n        role=\"group\"\n        aria-roledescription=\"heat map\"\n        aria-label={`${title ?? info.label} heat map. Use arrow keys to read values.`}\n      >\n        <canvas ref={canvasRef} className=\"hb-ub5-cv\" aria-hidden=\"true\" />\n      </div>\n      <div className=\"hb-ub5-head\">\n        <span className=\"hb-ub5-t\">{title ?? info.label}</span>\n        <span className=\"hb-ub5-s\">{subtitle ?? info.sub}</span>\n      </div>\n      <div className=\"hb-ub5-lg\" aria-hidden=\"true\">\n        <i className=\"hb-ub5-bar\" />\n        <span>{format(metric, 0).split(\" \")[0]}</span>\n        <span>{format(metric, 1)}</span>\n      </div>\n      <div ref={tipRef} className=\"hb-ub5-tip\" data-on=\"false\" aria-hidden=\"true\">\n        <span ref={tipName} className=\"hb-ub5-tn\" />\n        <span className=\"hb-ub5-tv\">\n          <i />\n          <span ref={tipVal} />\n        </span>\n      </div>\n      <span ref={liveRef} className=\"hb-ub5-sr\" aria-live=\"polite\" />\n    </div>\n  );\n}\n"
    }
  ]
}