{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"constellation-network-particle-background-with-depth-fade","type":"registry:block","title":"Constellation Network with Depth Fade","description":"Free constellation particle background in plain canvas: thin links between nearby points, depth fade and a pause off screen. React and vanilla versions.","dependencies":[],"files":[{"path":"components/we-inc/constellation-network-particle-background-with-depth-fade.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Constellation Network with Depth Fade: a canvas 2D particle background behind a small hero.\n// Sprites are drawn once to small offscreen canvases, motion is scaled by frame\n// time, the loop stops off screen, and reduced motion gets one settled frame.\n// Values worth changing:\nconst NODES_PER_MPX = 165; // how many points per million pixels of area\nconst INK = \"51,65,85\"; // colour of the points and links, as r,g,b\nconst ACCENT = \"15,118,110\"; // colour of the few highlighted hub points\nconst LINK_DISTANCE = 165; // pixels: points closer than this get joined\nconst SPEED = 1; // 0.5 for a slower drift, 2 for faster\nconst WARMUP = 30; // frames simulated before the first paint\n\ntype Ptr = { x: number; y: number; on: boolean };\ntype Scene = { update: (dt: number, t: number) => void; draw: (c: CanvasRenderingContext2D, t: number) => void };\n\nfunction glow(size: number, rgb: string, stops: number[][]): HTMLCanvasElement {\n  const c = document.createElement(\"canvas\");\n  c.width = c.height = size;\n  const g = c.getContext(\"2d\")!;\n  const r = size / 2;\n  const grd = g.createRadialGradient(r, r, 0, r, r, r);\n  for (const [o, a] of stops) grd.addColorStop(o, `rgba(${rgb},${a})`);\n  g.fillStyle = grd;\n  g.fillRect(0, 0, size, size);\n  return c;\n}\n\nfunction mount(canvas: HTMLCanvasElement, root: HTMLElement): () => void {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  const still = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const ptr: Ptr = { x: -1e4, y: -1e4, on: false };\n  let w = 0, h = 0, t = 0, last = 0, raf = 0, seen = true;\n  let scene: Scene | null = null;\n  const fit = () => {\n    const r = canvas.getBoundingClientRect();\n    if (scene && Math.abs(r.width - w) < 1 && Math.abs(r.height - h) < 1) return;\n    const dpr = Math.min(window.devicePixelRatio || 1, 1.5);\n    w = Math.max(1, r.width);\n    h = Math.max(1, r.height);\n    canvas.width = Math.round(w * dpr);\n    canvas.height = Math.round(h * dpr);\n    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    scene = createScene(w, h, ptr);\n    for (let i = 0; i < WARMUP; i++) { t += 1 / 30; scene.update(1 / 30, t); }\n    scene.draw(ctx, t);\n  };\n  const frame = (now: number) => {\n    const dt = Math.min(0.05, Math.max(0, (now - last) / 1000));\n    last = now;\n    t += dt;\n    if (scene) { scene.update(dt, t); scene.draw(ctx, t); }\n    raf = requestAnimationFrame(frame);\n  };\n  const run = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    if (!still && seen && !document.hidden) { last = performance.now(); raf = requestAnimationFrame(frame); }\n  };\n  const ro = new ResizeObserver(fit);\n  ro.observe(canvas);\n  const io = new IntersectionObserver((es) => { seen = es[es.length - 1].isIntersecting; run(); });\n  io.observe(canvas);\n  document.addEventListener(\"visibilitychange\", run);\n  fit();\n  run();\n  return () => {\n    cancelAnimationFrame(raf);\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", run);\n  };\n}\n\nfunction createScene(w: number, h: number, _ptr: Ptr): Scene {\n  const n = Math.max(28, Math.min(200, Math.round(((w * h) / 1e6) * NODES_PER_MPX)));\n  const dot = glow(32, INK, [[0, 1], [0.3, 1], [0.42, 0]]);\n  const hubDot = glow(32, ACCENT, [[0, 1], [0.3, 1], [0.42, 0]]);\n  const halo = glow(96, ACCENT, [[0, 0.28], [0.25, 0.12], [1, 0]]);\n  const ps = Array.from({ length: n }, () => {\n    const z = Math.random();\n    const a = Math.random() * Math.PI * 2;\n    const v = (3 + Math.random() * 7) * (0.35 + z);\n    return { x: Math.random() * w, y: Math.random() * h, z, vx: Math.cos(a) * v, vy: Math.sin(a) * v, hub: Math.random() < 0.07, s: Math.random() * 9 };\n  });\n  return {\n    update(dt) {\n      for (const p of ps) {\n        p.x += p.vx * dt * SPEED;\n        p.y += p.vy * dt * SPEED;\n        if (p.x < -60) p.x += w + 120; else if (p.x > w + 60) p.x -= w + 120;\n        if (p.y < -60) p.y += h + 120; else if (p.y > h + 60) p.y -= h + 120;\n      }\n    },\n    draw(c, t) {\n      c.clearRect(0, 0, w, h);\n      c.strokeStyle = `rgb(${INK})`;\n      for (let i = 0; i < n; i++) {\n        const a = ps[i];\n        for (let j = i + 1; j < n; j++) {\n          const b = ps[j];\n          const zz = (a.z + b.z) / 2;\n          const L = LINK_DISTANCE * (0.55 + 0.65 * zz);\n          const dx = a.x - b.x, dy = a.y - b.y;\n          if (dx > L || dx < -L || dy > L || dy < -L) continue;\n          const d2 = dx * dx + dy * dy;\n          if (d2 > L * L) continue;\n          const f = 1 - Math.sqrt(d2) / L;\n          c.globalAlpha = f * (0.06 + 0.4 * zz);\n          c.lineWidth = 0.5 + zz * 0.7;\n          c.beginPath();\n          c.moveTo(a.x, a.y);\n          c.lineTo(b.x, b.y);\n          c.stroke();\n        }\n      }\n      for (const p of ps) {\n        const s = 3 + p.z * 5;\n        if (p.hub) {\n          const hs = 44 + 14 * Math.sin(t * 0.9 + p.s);\n          c.globalAlpha = 1;\n          c.drawImage(halo, p.x - hs / 2, p.y - hs / 2, hs, hs);\n          c.drawImage(hubDot, p.x - s * 0.7, p.y - s * 0.7, s * 1.4, s * 1.4);\n        } else {\n          c.globalAlpha = 0.25 + p.z * 0.7;\n          c.drawImage(dot, p.x - s / 2, p.y - s / 2, s, s);\n        }\n      }\n      c.globalAlpha = 1;\n    },\n  };\n}\n\nexport default function ConstellationNetworkDepthFade() {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const rootRef = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    if (!canvasRef.current || !rootRef.current) return;\n    return mount(canvasRef.current, rootRef.current);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"cnl-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.cnl-bg{background:linear-gradient(160deg, #f5f7f9 0%, #e8edf2 100%)}.cnl-scrim{background:radial-gradient(55% 75% at 22% 50%, rgba(245,247,249,0.92) 0%, rgba(245,247,249,0) 75%)}`}</style>\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      <div aria-hidden=\"true\" className=\"cnl-scrim pointer-events-none absolute inset-0 -z-10\" />\n      <div className=\"mx-auto w-full max-w-6xl\">\n        <div className=\"max-w-xl\">\n          <p className=\"text-sm font-semibold uppercase tracking-[0.14em] text-[#0f766e]\">Meridian Survey Labs</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#0f172a] sm:text-5xl\">See how every site in your region connects.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#334155]\">We map utilities, transit and land records into one shared model your planning team can search, filter and export.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-md bg-[#0f766e] px-6 text-sm font-semibold text-[#ffffff] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#0f766e] focus-visible:ring-offset-2 focus-visible:ring-offset-[#f5f7f9]\"\n          >\n            Request a sample map\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
