{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"bg-falling-stars","type":"registry:ui","title":"FallingStarsBg","description":"Canvas-based 3D starfield with perspective projection, motion trails, and glow","author":"fancy-ui <https://fancy-ui.rama.app>","categories":["backgrounds"],"registryDependencies":["https://fancy-ui.rama.app/r/utils.json"],"files":[{"path":"react/src/components/bg-falling-stars/FallingStarsBg.tsx","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\nimport { cn } from \"../../utils\";\n\n/**\n * FallingStarsBg - Canvas-based 3D starfield background\n *\n * Stars travel toward the camera through a perspective projection and trail a\n * layered stroke that fakes motion blur. Everything is drawn on a 2D canvas from\n * a `requestAnimationFrame` loop - no CSS or SVG animation - and the canvas is\n * sized from `devicePixelRatio` and kept in sync by a `ResizeObserver`.\n */\nexport interface FallingStarsBgProps {\n\t/** Star color: a hex string, or `currentColor` (default) to follow the text colour */\n\tcolor?: string;\n\t/** Number of stars */\n\tcount?: number;\n\t/** Additional CSS classes */\n\tclassName?: string;\n\t/**\n\t * Seed for the starfield. The same seed always lays out and flies the same\n\t * sky, which is what keeps the component free of `Math.random()`. Change it\n\t * to give two starfields on one page different skies - the default is\n\t * shared, so two `<FallingStarsBg />` with no seed show the same sky.\n\t */\n\tseed?: number;\n}\n\ninterface Star {\n\tx: number;\n\ty: number;\n\tz: number;\n\tspeed: number;\n}\n\ninterface Rgb {\n\tr: number;\n\tg: number;\n\tb: number;\n}\n\nconst CURRENT_COLOR = /^\\s*currentcolor\\s*$/i;\n\nfunction parseHex(hex: string): Rgb {\n\tlet h = (hex || \"#000\").trim().replace(/^#/, \"\");\n\tif (h.length === 3) {\n\t\th = h\n\t\t\t.split(\"\")\n\t\t\t.map((c) => c + c)\n\t\t\t.join(\"\");\n\t}\n\tconst bigint = parseInt(h, 16) || 0;\n\treturn { r: (bigint >> 16) & 255, g: (bigint >> 8) & 255, b: bigint & 255 };\n}\n\n/**\n * Turn the browser's computed `color` into 0-255 channels. The common\n * `rgb()`/`rgba()` serialisation is parsed directly; newer spaces (oklch,\n * color-mix, ...) are rasterised through a 1x1 canvas, which converts them to\n * sRGB for free. Falls back to black when neither works (no canvas backend).\n */\nfunction computedToRgb(computed: string): Rgb {\n\tconst m = /^rgba?\\(\\s*([\\d.]+)[,\\s]+([\\d.]+)[,\\s]+([\\d.]+)/i.exec(computed);\n\tif (m) return { r: Math.round(+m[1]!), g: Math.round(+m[2]!), b: Math.round(+m[3]!) };\n\tif (!computed) return { r: 0, g: 0, b: 0 };\n\ttry {\n\t\tconst probe = document.createElement(\"canvas\");\n\t\tprobe.width = probe.height = 1;\n\t\tconst pctx = probe.getContext(\"2d\", { willReadFrequently: true });\n\t\tif (pctx) {\n\t\t\tpctx.fillStyle = computed;\n\t\t\tpctx.fillRect(0, 0, 1, 1);\n\t\t\tconst [r = 0, g = 0, b = 0] = pctx.getImageData(0, 0, 1, 1).data;\n\t\t\treturn { r, g, b };\n\t\t}\n\t} catch {\n\t\t// fall through to the black fallback\n\t}\n\treturn { r: 0, g: 0, b: 0 };\n}\n\n/**\n * Colour resolver: a hex string is parsed once per value, `currentColor` is\n * re-read from `el`'s computed style on every call (so a light/dark switch\n * lands on the next frame) and re-parsed only when that string changes.\n */\nfunction createColorResolver(el: Element) {\n\tlet lastComputed = \"\";\n\tlet lastRgb: Rgb = { r: 0, g: 0, b: 0 };\n\tlet lastHex = \"\";\n\tlet lastHexRgb: Rgb = { r: 0, g: 0, b: 0 };\n\treturn (color: string): Rgb => {\n\t\tif (CURRENT_COLOR.test(color)) {\n\t\t\tconst computed = getComputedStyle(el).color;\n\t\t\tif (computed !== lastComputed) {\n\t\t\t\tlastComputed = computed;\n\t\t\t\tlastRgb = computedToRgb(computed);\n\t\t\t}\n\t\t\treturn lastRgb;\n\t\t}\n\t\tif (color !== lastHex) {\n\t\t\tlastHex = color;\n\t\t\tlastHexRgb = parseHex(color);\n\t\t}\n\t\treturn lastHexRgb;\n\t};\n}\n\n/**\n * mulberry32 - a tiny deterministic PRNG, standing in for the `Math.random()`\n * the starfield is seeded and respawned with. The sky is generated inside the\n * mount effect, so it never reaches a server render, but a seeded stream still\n * buys two things a raw `Math.random()` cannot: a sky that comes back identical\n * after a remount, and a draw path a test can assert on frame by frame.\n */\nfunction mulberry32(seed: number): () => number {\n\tlet state = seed >>> 0;\n\treturn () => {\n\t\tstate = (state + 0x6d2b79f5) >>> 0;\n\t\tlet t = state;\n\t\tt = Math.imul(t ^ (t >>> 15), t | 1);\n\t\tt ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n\t\treturn ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n\t};\n}\n\nexport function FallingStarsBg({\n\tcolor = \"currentColor\",\n\tcount = 200,\n\tclassName = \"\",\n\tseed = 1,\n}: FallingStarsBgProps) {\n\tconst canvasRef = useRef<HTMLCanvasElement>(null);\n\n\t// The Svelte source caches the parsed colour in a `$derived`. Here the\n\t// running loop reads it through a ref, so a colour change repaints on the\n\t// next frame instead of tearing the starfield down and re-seeding it.\n\tconst colorRef = useRef(color);\n\tuseEffect(() => {\n\t\tcolorRef.current = color;\n\t}, [color]);\n\n\tuseEffect(() => {\n\t\tconst canvas = canvasRef.current;\n\t\tif (!canvas) return;\n\n\t\tconst random = mulberry32(seed);\n\t\tconst resolveColor = createColorResolver(canvas);\n\t\tlet rgb: Rgb = resolveColor(colorRef.current);\n\t\tlet perspective = 0;\n\t\tlet stars: Star[] = [];\n\t\tlet ctx: CanvasRenderingContext2D | null = null;\n\t\tlet dpr = 1;\n\t\tlet rafId = 0;\n\n\t\tfunction resizeCanvas() {\n\t\t\tif (!canvas) return;\n\n\t\t\tdpr = window.devicePixelRatio || 1;\n\t\t\tconst width = Math.max(1, Math.floor(canvas.clientWidth));\n\t\t\tconst height = Math.max(1, Math.floor(canvas.clientHeight));\n\n\t\t\tcanvas.width = Math.floor(width * dpr);\n\t\t\tcanvas.height = Math.floor(height * dpr);\n\t\t\tcanvas.style.width = `${width}px`;\n\t\t\tcanvas.style.height = `${height}px`;\n\n\t\t\tctx = canvas.getContext(\"2d\");\n\t\t\tif (ctx) {\n\t\t\t\tctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n\t\t\t}\n\n\t\t\tperspective = width / 2;\n\t\t}\n\n\t\tfunction drawStar(star: Star, width: number, height: number) {\n\t\t\tif (!ctx) return;\n\n\t\t\tconst scale = perspective / (perspective + star.z);\n\t\t\tconst x2d = width / 2 + star.x * scale;\n\t\t\tconst y2d = height / 2 + star.y * scale;\n\t\t\tconst size = Math.max(scale * 3, 0.5);\n\n\t\t\tconst prevScale = perspective / (perspective + star.z + star.speed * 15);\n\t\t\tconst xPrev = width / 2 + star.x * prevScale;\n\t\t\tconst yPrev = height / 2 + star.y * prevScale;\n\n\t\t\t// Layered strokes from wide+faint to narrow+brighter to fake blur\n\t\t\tconst layerAlphas = [0.08, 0.14, 0.22];\n\t\t\tfor (let i = 0; i < layerAlphas.length; i++) {\n\t\t\t\tctx.beginPath();\n\t\t\t\tctx.strokeStyle = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${layerAlphas[i]})`;\n\t\t\t\tctx.lineWidth = size * (1.4 + i * 1.2);\n\t\t\t\tctx.moveTo(x2d, y2d);\n\t\t\t\tctx.lineTo(xPrev, yPrev);\n\t\t\t\tctx.stroke();\n\t\t\t}\n\n\t\t\t// Sharp center line\n\t\t\tctx.beginPath();\n\t\t\tctx.strokeStyle = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 0.6)`;\n\t\t\tctx.lineWidth = Math.max(1, size);\n\t\t\tctx.moveTo(x2d, y2d);\n\t\t\tctx.lineTo(xPrev, yPrev);\n\t\t\tctx.stroke();\n\n\t\t\t// Dot\n\t\t\tctx.beginPath();\n\t\t\tctx.fillStyle = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1)`;\n\t\t\tctx.arc(x2d, y2d, Math.max(0.5, size / 4), 0, Math.PI * 2);\n\t\t\tctx.fill();\n\t\t}\n\n\t\tfunction loop() {\n\t\t\tif (!canvas) return;\n\t\t\tif (!ctx) ctx = canvas.getContext(\"2d\");\n\t\t\tif (!ctx) return;\n\n\t\t\tconst width = canvas.clientWidth;\n\t\t\tconst height = canvas.clientHeight;\n\n\t\t\tctx.clearRect(0, 0, width, height);\n\t\t\trgb = resolveColor(colorRef.current);\n\n\t\t\tfor (const star of stars) {\n\t\t\t\tdrawStar(star, width, height);\n\n\t\t\t\tstar.z -= star.speed;\n\n\t\t\t\tif (star.z <= 0) {\n\t\t\t\t\tstar.z = width || 1;\n\t\t\t\t\tstar.x = (random() - 0.5) * 2 * width;\n\t\t\t\t\tstar.y = (random() - 0.5) * 2 * height;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\trafId = requestAnimationFrame(loop);\n\t\t}\n\n\t\tresizeCanvas();\n\n\t\tconst cssWidth = canvas.clientWidth;\n\t\tconst cssHeight = canvas.clientHeight;\n\t\tstars = [];\n\t\tfor (let i = 0; i < count; i++) {\n\t\t\tstars.push({\n\t\t\t\tx: (random() - 0.5) * 2 * cssWidth,\n\t\t\t\ty: (random() - 0.5) * 2 * cssHeight,\n\t\t\t\tz: random() * (cssWidth || 1),\n\t\t\t\tspeed: random() * 5 + 2,\n\t\t\t});\n\t\t}\n\n\t\trafId = requestAnimationFrame(loop);\n\n\t\tconst resizeObserver = new ResizeObserver(() => resizeCanvas());\n\t\tresizeObserver.observe(canvas);\n\n\t\treturn () => {\n\t\t\tif (rafId) cancelAnimationFrame(rafId);\n\t\t\tresizeObserver.disconnect();\n\t\t};\n\t}, [count, seed]);\n\n\treturn <canvas ref={canvasRef} className={cn(\"absolute inset-0 h-full w-full\", className)} />;\n}\n","type":"registry:ui","target":"@ui/fancy/components/bg-falling-stars/FallingStarsBg.tsx"},{"path":"react/src/components/bg-falling-stars/index.ts","content":"\"use client\";\n\nexport { FallingStarsBg } from \"./FallingStarsBg\";\nexport type { FallingStarsBgProps } from \"./FallingStarsBg\";\n","type":"registry:ui","target":"@ui/fancy/components/bg-falling-stars/index.ts"}],"meta":{"license":"MIT","package":"fancy-ui-react","version":"0.7.0","source":"https://fancy-ui.rama.app/docs/components/bg-falling-stars?framework=react"}}