{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"constellation","type":"registry:ui","title":"Constellation","description":"Drifting points that connect with lines when they come close, and move gently out of the way of the pointer.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/constellation.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useReducedMotion } from \"motion/react\";\nimport { useEffect, useRef } from \"react\";\n\nexport type ConstellationProps = {\n  /** Point and line color as a hex value. */\n  color?: string;\n  /** Number of points in a 1100 by 600 area. Other sizes keep the same density. */\n  count?: number;\n  /** How close two points must be to connect, in pixels. */\n  distance?: number;\n  /** Drift speed multiplier. */\n  speed?: number;\n  /** Radius of the area the pointer pushes away, in pixels. 0 turns it off. */\n  pushRadius?: number;\n  className?: string;\n};\n\ntype Point = { x: number; y: number; vx: number; vy: number; driftX: number; driftY: number; size: number; phase: number };\n\nconst MAX_POINTS = 500;\nconst LINE_LEVELS = 6; // lines are batched by brightness, so a frame makes only a few draw calls\nconst PUSH_FORCE = 520; // px per second squared at the center of the pushed area\nconst WHITE = [255, 255, 255];\n\nfunction hexToRgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, (c) => c + c);\n  const n = parseInt(h, 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\n/** Points that connect with lines when they come close, and move gently out of the pointer's way. */\nexport function Constellation({\n  color = \"#ff784f\",\n  count = 120,\n  distance = 140,\n  speed = 1,\n  pushRadius = 140,\n  className = \"\",\n}: ConstellationProps) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  // Kept across prop changes, so dragging a slider does not reshuffle the sky.\n  const pointsRef = useRef<Point[]>([]);\n  const fadeRef = useRef(0);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!canvas || !ctx) return;\n\n    const rgb = hexToRgb(color);\n    const hot = rgb.map((c, i) => Math.round(c + (WHITE[i] - c) * 0.55));\n    const reach = Math.max(10, distance);\n    const margin = reach;\n    const points = pointsRef.current;\n    const pointer = { x: 0, y: 0, on: false, glow: 0 };\n    const buckets: number[][] = Array.from({ length: LINE_LEVELS }, () => []);\n    const lineColors = buckets.map((_, i) => `rgba(${rgb[0]},${rgb[1]},${rgb[2]},${((i + 1) / LINE_LEVELS) ** 1.25 * 0.9})`);\n\n    // One soft dot, drawn once and stamped for every point.\n    const sprite = document.createElement(\"canvas\");\n    sprite.width = sprite.height = 48;\n    const spriteCtx = sprite.getContext(\"2d\");\n    if (spriteCtx) {\n      const g = spriteCtx.createRadialGradient(24, 24, 0, 24, 24, 24);\n      g.addColorStop(0, `rgb(${hot.join(\",\")})`);\n      g.addColorStop(0.16, `rgb(${hot.join(\",\")})`);\n      g.addColorStop(0.24, `rgba(${rgb.join(\",\")},0.42)`);\n      g.addColorStop(1, `rgba(${rgb.join(\",\")},0)`);\n      spriteCtx.fillStyle = g;\n      spriteCtx.fillRect(0, 0, 48, 48);\n    }\n\n    let width = 0;\n    let height = 0;\n    let frame = 0;\n    let visible = true;\n    let last = 0;\n    let time = 0;\n\n    const makePoint = (x: number, y: number): Point => {\n      const angle = Math.random() * Math.PI * 2;\n      const drift = 6 + Math.random() * 12;\n      return {\n        x,\n        y,\n        vx: 0,\n        vy: 0,\n        driftX: Math.cos(angle) * drift,\n        driftY: Math.sin(angle) * drift,\n        // Mostly small points with a few bright ones, like a real night sky.\n        size: 0.9 + Math.random() ** 3 * 1.9,\n        phase: Math.random() * Math.PI * 2,\n      };\n    };\n\n    const fit = () => {\n      // Same density at any size, so a phone is not a solid web of lines and a wide hero is not empty.\n      const fullW = width + margin * 2;\n      const fullH = height + margin * 2;\n      const wanted = Math.min(MAX_POINTS, Math.round(Math.max(0, count) * ((fullW * fullH) / (1100 * 600))));\n      if (points.length < wanted) {\n        // Fill a jittered grid in shuffled order: an even sky without visible rows or big empty holes.\n        const missing = wanted - points.length;\n        const cols = Math.max(1, Math.ceil(Math.sqrt((missing * fullW) / fullH)));\n        const rows = Math.max(1, Math.ceil(missing / cols));\n        const slots = Array.from({ length: cols * rows }, (_, i) => i);\n        for (let i = slots.length - 1; i > 0; i--) {\n          const j = Math.floor(Math.random() * (i + 1));\n          [slots[i], slots[j]] = [slots[j], slots[i]];\n        }\n        for (let i = 0; i < missing; i++) {\n          const col = slots[i] % cols;\n          const row = Math.floor(slots[i] / cols);\n          points.push(makePoint(-margin + ((col + Math.random()) / cols) * fullW, -margin + ((row + Math.random()) / rows) * fullH));\n        }\n      }\n      points.length = wanted;\n    };\n\n    const step = (dt: number) => {\n      const lap = Math.min(1, dt * 1.4);\n      const fullW = width + margin * 2;\n      const fullH = height + margin * 2;\n      for (const p of points) {\n        p.vx += (p.driftX * speed - p.vx) * lap;\n        p.vy += (p.driftY * speed - p.vy) * lap;\n        if (pointer.on && pushRadius > 0) {\n          const dx = p.x - pointer.x;\n          const dy = p.y - pointer.y;\n          const d = Math.hypot(dx, dy);\n          if (d < pushRadius && d > 0.01) {\n            const force = (1 - d / pushRadius) ** 2 * PUSH_FORCE * dt;\n            p.vx += (dx / d) * force;\n            p.vy += (dy / d) * force;\n          }\n        }\n        p.x += p.vx * dt;\n        p.y += p.vy * dt;\n        // Points wrap around well outside the canvas, where their lines have already faded out.\n        if (p.x < -margin) p.x += fullW;\n        else if (p.x > width + margin) p.x -= fullW;\n        if (p.y < -margin) p.y += fullH;\n        else if (p.y > height + margin) p.y -= fullH;\n      }\n    };\n\n    const paint = () => {\n      ctx.clearRect(0, 0, width, height);\n\n      if (pointer.glow > 0.01 && pushRadius > 0) {\n        const halo = ctx.createRadialGradient(pointer.x, pointer.y, 0, pointer.x, pointer.y, pushRadius * 1.3);\n        halo.addColorStop(0, `rgba(${rgb.join(\",\")},${0.09 * pointer.glow})`);\n        halo.addColorStop(1, `rgba(${rgb.join(\",\")},0)`);\n        ctx.fillStyle = halo;\n        ctx.fillRect(0, 0, width, height);\n      }\n\n      for (const bucket of buckets) bucket.length = 0;\n      const limit = reach * reach;\n      for (let i = 0; i < points.length; i++) {\n        const a = points[i];\n        for (let j = i + 1; j < points.length; j++) {\n          const b = points[j];\n          const dx = a.x - b.x;\n          if (dx > reach || dx < -reach) continue;\n          const dy = a.y - b.y;\n          const d2 = dx * dx + dy * dy;\n          if (d2 > limit) continue;\n          let k = 1 - Math.sqrt(d2) / reach;\n          // Lines near the pointer read a little brighter.\n          if (pointer.glow > 0.01 && pushRadius > 0) {\n            const near = 1 - Math.hypot((a.x + b.x) / 2 - pointer.x, (a.y + b.y) / 2 - pointer.y) / (pushRadius * 1.5);\n            if (near > 0) k += near * 0.5 * pointer.glow;\n          }\n          buckets[Math.min(LINE_LEVELS - 1, Math.floor(k * LINE_LEVELS))].push(a.x, a.y, b.x, b.y);\n        }\n      }\n      ctx.globalAlpha = fadeRef.current;\n      ctx.lineWidth = 1;\n      ctx.lineCap = \"round\";\n      buckets.forEach((bucket, level) => {\n        if (!bucket.length) return;\n        ctx.strokeStyle = lineColors[level];\n        ctx.beginPath();\n        for (let i = 0; i < bucket.length; i += 4) {\n          ctx.moveTo(bucket[i], bucket[i + 1]);\n          ctx.lineTo(bucket[i + 2], bucket[i + 3]);\n        }\n        ctx.stroke();\n      });\n\n      for (const p of points) {\n        const twinkle = 0.62 + 0.38 * Math.sin(time * 1.3 + p.phase);\n        const r = p.size * 5.5;\n        ctx.globalAlpha = fadeRef.current * twinkle;\n        ctx.drawImage(sprite, p.x - r, p.y - r, r * 2, r * 2);\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const draw = (t: number) => {\n      const dt = last ? Math.min(50, t - last) / 1000 : 0.016;\n      last = t;\n      time += dt;\n      fadeRef.current = Math.min(1, fadeRef.current + dt * 1.4);\n      pointer.glow += ((pointer.on ? 1 : 0) - pointer.glow) * Math.min(1, dt * 5);\n      step(dt);\n      paint();\n      if (visible) frame = requestAnimationFrame(draw);\n      else frame = 0;\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      const oldW = width;\n      const oldH = height;\n      width = canvas.clientWidth;\n      height = canvas.clientHeight;\n      canvas.width = Math.max(1, Math.round(width * dpr));\n      canvas.height = Math.max(1, Math.round(height * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (oldW && oldH) {\n        for (const p of points) {\n          p.x = ((p.x + margin) / (oldW + margin * 2)) * (width + margin * 2) - margin;\n          p.y = ((p.y + margin) / (oldH + margin * 2)) * (height + margin * 2) - margin;\n        }\n      }\n      fit();\n      if (reduce) {\n        fadeRef.current = 1;\n        paint();\n      }\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const rect = canvas.getBoundingClientRect();\n      const x = e.clientX - rect.left;\n      const y = e.clientY - rect.top;\n      pointer.on = x >= 0 && y >= 0 && x <= rect.width && y <= rect.height;\n      if (pointer.on) {\n        pointer.x = x;\n        pointer.y = y;\n      }\n    };\n    const onLeave = (e: PointerEvent) => {\n      if (!e.relatedTarget || e.pointerType === \"touch\") pointer.on = false;\n    };\n\n    resize();\n    const observer = new ResizeObserver(resize);\n    observer.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !reduce && !frame) {\n        last = 0;\n        frame = requestAnimationFrame(draw);\n      }\n    });\n    io.observe(canvas);\n    if (!reduce) {\n      window.addEventListener(\"pointermove\", onMove, { passive: true });\n      document.addEventListener(\"pointerout\", onLeave);\n      frame = requestAnimationFrame(draw);\n    }\n\n    return () => {\n      cancelAnimationFrame(frame);\n      observer.disconnect();\n      io.disconnect();\n      window.removeEventListener(\"pointermove\", onMove);\n      document.removeEventListener(\"pointerout\", onLeave);\n    };\n  }, [color, count, distance, speed, pushRadius, reduce]);\n\n  return (\n    <canvas\n      ref={canvasRef}\n      className={`pointer-events-none absolute inset-0 block size-full ${className}`}\n      aria-hidden=\"true\"\n    />\n  );\n}\n"}]}