{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "starfield",
  "type": "registry:ui",
  "title": "Starfield",
  "description": "A night sky behind a hero: still stars, a share of them breathing, an occasional shooting star and a few slow meteors. Everything is painted on ONE `<canvas>` driven by ONE animation frame loop, so the whole sky costs a single layer and a single callback…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "decorative",
    "effect"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/effects/starfield.tsx",
      "target": "components/ui/effects/starfield.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/starfield v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/starfield\n// What changed since: https://ds.interlace.tools/c/starfield#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — Starfield\n *\n * A night sky behind a hero: still stars, a share of them breathing, an\n * occasional shooting star and a few slow meteors. Everything is painted on\n * ONE `<canvas>` driven by ONE animation frame loop, so the whole sky costs a\n * single layer and a single callback, whatever the star count.\n *\n * ## Why one canvas\n *\n * Stars, shooting stars and meteors are the same thing at different speeds:\n * points of light that move or do not. Drawing them on one surface means one\n * resize path, one device-pixel-ratio fix, one place that pauses, and no DOM\n * node per particle. The loop sleeps when the sky scrolls out of view or the\n * tab is hidden, so an idle page burns no frames.\n *\n * ## Colour from the cascade\n *\n * Canvas cannot read `var()`. Each colour prop is applied to the canvas as\n * its CSS `color` and read back through `getComputedStyle`, so a caller\n * passes `var(--hero-star)`, `oklch(…)` or a hex and all three work, and a\n * theme switch is picked up on the next resize.\n *\n * ## Motion\n *\n * Under `prefers-reduced-motion: reduce` the sky paints once and stops: the\n * stars stay, nothing moves, and no frame is requested. The canvas is\n * `aria-hidden` and `pointer-events-none`; it never carries meaning.\n *\n * | Rule | Concept                 | Where in this file                                   |\n * | ---- | ----------------------- | ---------------------------------------------------- |\n * | R4   | Extends native el       | `canvas` props, minus children                        |\n * | R6   | data-slot               | `starfield`                                           |\n * | R7   | cn + ...rest            | `cn(…, className)` + `{...props}`                     |\n * | R19  | Tokens only             | colours default to `--hero-*` tokens                  |\n * | R23  | CLS=0                   | `absolute inset-0`, never in flow                     |\n * | R25  | Client component        | canvas + animation frames need the DOM                |\n */\n\nimport { cn } from '@/lib/utils';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\n\nexport interface StarfieldProps\n  extends Omit<React.ComponentPropsWithoutRef<'canvas'>, 'children' | 'aria-hidden'> {\n  /** Stars per square CSS pixel. @default 0.0002 */\n  density?: number;\n  /** Share of stars that breathe, 0 to 1. @default 0.7 */\n  twinkle?: number;\n  /** Slowest and fastest breath, in seconds per cycle. @default [2, 5] */\n  twinkleDuration?: readonly [number, number];\n  /** Seconds between shooting stars (shortest, longest). `false` turns them off. @default [3, 8] */\n  shootingStars?: readonly [number, number] | false;\n  /** Meteors in the sky at once. `0` turns them off. @default 2 */\n  meteors?: number;\n  /** Seconds a meteor takes to cross (shortest, longest). @default [6, 14] */\n  meteorDuration?: readonly [number, number];\n  /** Any CSS colour. @default 'var(--hero-star)' */\n  starColor?: string;\n  /** Colour of shooting-star and meteor heads. @default 'var(--hero-meteor)' */\n  streakColor?: string;\n}\n\ninterface Star {\n  x: number;\n  y: number;\n  r: number;\n  alpha: number;\n  period: number;\n  phase: number;\n}\n\ninterface Streak {\n  kind: 'meteor' | 'shot';\n  x: number;\n  y: number;\n  vx: number;\n  vy: number;\n  length: number;\n  born: number;\n  life: number;\n}\n\nconst between = (min: number, max: number) => min + Math.random() * (max - min);\n\n/**\n * Any CSS colour (a variable, `oklch()`, `color-mix()`…) as `rgb(r, g, b)`.\n * Computed styles keep modern colour spaces as written, so the colour is\n * painted on a one-pixel canvas and read back, which always yields sRGB.\n */\nfunction resolveColor(canvas: HTMLCanvasElement, value: string): string {\n  canvas.style.color = value;\n  const computed = getComputedStyle(canvas).color || value;\n  canvas.style.color = '';\n  const probe = document.createElement('canvas');\n  probe.width = 1;\n  probe.height = 1;\n  const ctx = probe.getContext('2d', { willReadFrequently: true });\n  if (typeof ctx?.fillRect !== 'function' || typeof ctx.getImageData !== 'function') return computed;\n  ctx.fillStyle = computed;\n  ctx.fillRect(0, 0, 1, 1);\n  const pixel = ctx.getImageData(0, 0, 1, 1).data;\n  return `rgb(${pixel[0] ?? 0}, ${pixel[1] ?? 0}, ${pixel[2] ?? 0})`;\n}\n\n/** `rgb(1, 2, 3)` (what `resolveColor` returns) → the same colour at `alpha`. */\nfunction withAlpha(color: string, alpha: number): string {\n  const [r, g, b] = color.match(/[\\d.]+/g) ?? [];\n  if (r === undefined || g === undefined || b === undefined) return color;\n  return `rgba(${r}, ${g}, ${b}, ${alpha})`;\n}\n\nexport function Starfield({\n  density = 0.0002,\n  twinkle = 0.7,\n  twinkleDuration = [2, 5],\n  shootingStars = [3, 8],\n  meteors = 2,\n  meteorDuration = [6, 14],\n  starColor = 'var(--hero-star)',\n  streakColor = 'var(--hero-meteor)',\n  className,\n  ...props\n}: StarfieldProps) {\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const reduced = useReducedMotion();\n\n  const [twMin, twMax] = twinkleDuration;\n  const shooting = shootingStars !== false;\n  const shootMin = shootingStars ? shootingStars[0] : 0;\n  const shootMax = shootingStars ? shootingStars[1] : 0;\n  const [meteorMin, meteorMax] = meteorDuration;\n\n  React.useEffect(() => {\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext('2d');\n    if (!canvas || !ctx) return;\n\n    let width = 0;\n    let height = 0;\n    let stars: Star[] = [];\n    let streaks: Streak[] = [];\n    let star = '';\n    let head = '';\n    let frame = 0;\n    let visible = true;\n    let nextShot = 0;\n\n    const spawnMeteor = (now: number): Streak => {\n      const life = between(meteorMin, meteorMax) * 1000;\n      const distance = Math.hypot(width, height) * 1.2;\n      // Meteors fall from the upper edge toward the lower left, at a fixed\n      // angle so several read as one shower rather than noise.\n      return {\n        kind: 'meteor',\n        x: between(width * 0.2, width * 1.2),\n        y: between(-height * 0.3, 0),\n        vx: (-distance * 0.6) / life,\n        vy: (distance * 0.8) / life,\n        length: between(60, 120),\n        born: now + between(0, life),\n        life,\n      };\n    };\n\n    const spawnShot = (now: number): Streak => {\n      const life = between(700, 1200);\n      const distance = between(width * 0.3, width * 0.6);\n      return {\n        kind: 'shot',\n        x: between(0, width * 0.7),\n        y: between(0, height * 0.5),\n        vx: (distance * 0.9) / life,\n        vy: (distance * 0.35) / life,\n        length: between(80, 140),\n        born: now,\n        life,\n      };\n    };\n\n    const layout = () => {\n      const rect = canvas.getBoundingClientRect();\n      width = rect.width;\n      height = rect.height;\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.round(width * dpr);\n      canvas.height = Math.round(height * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      star = resolveColor(canvas, starColor);\n      head = resolveColor(canvas, streakColor);\n      const count = Math.round(width * height * density);\n      stars = Array.from({ length: count }, () => ({\n        x: Math.random() * width,\n        y: Math.random() * height,\n        r: between(0.4, 1.3),\n        alpha: between(0.35, 1),\n        period: Math.random() < twinkle ? between(twMin, twMax) * 1000 : 0,\n        phase: Math.random() * Math.PI * 2,\n      }));\n      const now = performance.now();\n      streaks = reduced ? [] : Array.from({ length: Math.max(0, meteors) }, () => spawnMeteor(now));\n    };\n\n    const drawStreak = (s: Streak, t: number) => {\n      const age = t - s.born;\n      if (age < 0) return;\n      const x = s.x + s.vx * age;\n      const y = s.y + s.vy * age;\n      const speed = Math.hypot(s.vx, s.vy) || 1;\n      const tx = x - (s.vx / speed) * s.length;\n      const ty = y - (s.vy / speed) * s.length;\n      // Fade in over the first fifth of the life and out over the last.\n      const p = age / s.life;\n      const fade = Math.min(1, p * 5, (1 - p) * 5);\n      const gradient = ctx.createLinearGradient(x, y, tx, ty);\n      gradient.addColorStop(0, withAlpha(head, fade));\n      gradient.addColorStop(1, withAlpha(head, 0));\n      ctx.strokeStyle = gradient;\n      ctx.lineWidth = 1.2;\n      ctx.beginPath();\n      ctx.moveTo(x, y);\n      ctx.lineTo(tx, ty);\n      ctx.stroke();\n    };\n\n    const paint = (t: number) => {\n      ctx.clearRect(0, 0, width, height);\n      for (const s of stars) {\n        const breath = s.period && !reduced ? 0.55 + 0.45 * Math.sin((t / s.period) * Math.PI * 2 + s.phase) : 1;\n        ctx.fillStyle = withAlpha(star, s.alpha * breath);\n        ctx.beginPath();\n        ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);\n        ctx.fill();\n      }\n      if (reduced) return;\n      streaks = streaks.map((s) => {\n        if (t - s.born <= s.life) return s;\n        // A finished meteor is replaced; a finished shooting star is dropped.\n        return s.kind === 'meteor' ? spawnMeteor(t) : null;\n      }).filter((s): s is Streak => s !== null);\n      if (shooting && t >= nextShot) {\n        streaks.push(spawnShot(t));\n        nextShot = t + between(shootMin, shootMax) * 1000;\n      }\n      for (const s of streaks) drawStreak(s, t);\n    };\n\n    const loop = (t: number) => {\n      paint(t);\n      frame = visible && !document.hidden ? requestAnimationFrame(loop) : 0;\n    };\n\n    const start = () => {\n      if (reduced || frame) return;\n      frame = requestAnimationFrame(loop);\n    };\n\n    layout();\n    nextShot = performance.now() + between(shootMin, shootMax) * 1000;\n    paint(performance.now());\n    start();\n\n    const resize = new ResizeObserver(() => {\n      layout();\n      paint(performance.now());\n    });\n    resize.observe(canvas);\n\n    const io =\n      typeof IntersectionObserver === 'undefined'\n        ? null\n        : new IntersectionObserver(([entry]) => {\n            visible = entry?.isIntersecting ?? true;\n            if (visible) start();\n          });\n    io?.observe(canvas);\n\n    const onVisibility = () => {\n      if (!document.hidden) start();\n    };\n    document.addEventListener('visibilitychange', onVisibility);\n\n    return () => {\n      cancelAnimationFrame(frame);\n      frame = 0;\n      resize.disconnect();\n      io?.disconnect();\n      document.removeEventListener('visibilitychange', onVisibility);\n    };\n  }, [\n    density,\n    twinkle,\n    twMin,\n    twMax,\n    shooting,\n    shootMin,\n    shootMax,\n    meteors,\n    meteorMin,\n    meteorMax,\n    starColor,\n    streakColor,\n    reduced,\n  ]);\n\n  return (\n    <canvas\n      ref={canvasRef}\n      data-slot=\"starfield\"\n      className={cn('pointer-events-none absolute inset-0 size-full', className)}\n      {...props}\n      aria-hidden\n    />\n  );\n}\n"
    }
  ],
  "meta": {
    "tier": "effect",
    "client": true,
    "minViewport": null,
    "loading": false,
    "version": "1.0.0",
    "since": null
  },
  "docs": "## @interlace/starfield\n\nInstalled to `components/ui/effects/starfield.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/effects/starfield';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/starfield\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
