Files
sorsfonalak/services/frontend/src/components/NightSky.tsx
T
fecoandClaude Opus 5.5 116e4747c2
CI / test (push) Successful in 1m42s
CI / build (push) Successful in 5m18s
CI / deploy (push) Skipped
Night sky background with shooting stars on every page
Faint twinkling stars and, on average every 6 seconds, a gold shooting
star, on a fixed canvas behind all content: anything with its own
background (bands, cards, the header) covers it, so the meteor passes
under them. With reduced motion only still stars are drawn. Replaces the
home page hero's own starfield.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-21 21:23:52 +00:00

144 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useEffect, useRef } from "react";
/** The meteor's colour (warm gold, like the site's accent) and its head. */
const TINT = [236, 205, 140];
/** On average one meteor this often (seconds); each gap is 50–150% of it. */
const EVERY = 6;
/**
* The site's background: faint twinkling stars and, every few seconds, a
* shooting star. A fixed canvas behind all content, so anything with its own
* background (bands, cards, the header) covers it and the meteor passes
* under them. With "reduce motion" set, only still stars are drawn.
*/
export function NightSky() {
const ref = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const canvas = ref.current;
const ctx = canvas?.getContext("2d");
if (!canvas || !ctx) return;
const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
type Star = { x: number; y: number; r: number; base: number; phase: number; rate: number };
type Meteor = { x: number; y: number; dx: number; dy: number; len: number; life: number; span: number; width: number };
let w = 0;
let h = 0;
let stars: Star[] = [];
const meteors: Meteor[] = [];
function resize() {
const dpr = Math.min(window.devicePixelRatio || 1, 2);
w = canvas!.clientWidth;
h = canvas!.clientHeight;
canvas!.width = Math.round(w * dpr);
canvas!.height = Math.round(h * dpr);
ctx!.setTransform(dpr, 0, 0, dpr, 0, 0);
// About one star per 9000 px².
stars = Array.from({ length: Math.round((w * h) / 9000) }, () => ({
x: Math.random() * w,
y: Math.random() * h,
r: Math.random() < 0.85 ? 0.5 + Math.random() * 0.7 : 1.1 + Math.random() * 0.6,
base: 0.12 + Math.random() * 0.3,
phase: Math.random() * Math.PI * 2,
rate: 0.4 + Math.random() * 0.9,
}));
if (reduced) drawStars(0);
}
/** Enters in the upper part of the screen and falls down and to the left at a shallow angle. */
function spawn() {
const angle = ((200 + Math.random() * 18) * Math.PI) / 180;
const speed = (w > 700 ? 900 : 620) * (0.85 + Math.random() * 0.3);
meteors.push({
x: w * (0.35 + Math.random() * 0.75),
y: -20 + Math.random() * h * 0.35,
dx: Math.cos(angle) * speed,
dy: -Math.sin(angle) * speed,
len: (w > 700 ? 190 : 130) * (0.8 + Math.random() * 0.5),
life: 0,
span: 0.75 + Math.random() * 0.45,
width: 1.2 + Math.random() * 0.8,
});
}
function drawStars(t: number) {
ctx!.clearRect(0, 0, w, h);
ctx!.fillStyle = "#ece6d8";
for (const s of stars) {
ctx!.globalAlpha = Math.max(0.04, reduced ? s.base : s.base + Math.sin(t * s.rate + s.phase) * 0.12);
ctx!.beginPath();
ctx!.arc(s.x, s.y, s.r, 0, Math.PI * 2);
ctx!.fill();
}
ctx!.globalAlpha = 1;
}
function drawMeteor(m: Meteor) {
const p = m.life / m.span;
// Fades in fast, burns, then fades out.
const fade = p < 0.15 ? p / 0.15 : p > 0.7 ? Math.max(0, (1 - p) / 0.3) : 1;
const [r, g, b] = TINT;
const norm = Math.hypot(m.dx, m.dy);
const tx = m.x - (m.dx / norm) * m.len;
const ty = m.y - (m.dy / norm) * m.len;
const tail = ctx!.createLinearGradient(m.x, m.y, tx, ty);
tail.addColorStop(0, `rgba(255, 250, 235, ${0.95 * fade})`);
tail.addColorStop(0.12, `rgba(${r}, ${g}, ${b}, ${0.75 * fade})`);
tail.addColorStop(1, `rgba(${r}, ${g}, ${b}, 0)`);
ctx!.strokeStyle = tail;
ctx!.lineWidth = m.width;
ctx!.lineCap = "round";
ctx!.beginPath();
ctx!.moveTo(m.x, m.y);
ctx!.lineTo(tx, ty);
ctx!.stroke();
const glow = ctx!.createRadialGradient(m.x, m.y, 0, m.x, m.y, 9);
glow.addColorStop(0, `rgba(255, 252, 240, ${0.9 * fade})`);
glow.addColorStop(0.4, `rgba(${r}, ${g}, ${b}, ${0.35 * fade})`);
glow.addColorStop(1, `rgba(${r}, ${g}, ${b}, 0)`);
ctx!.fillStyle = glow;
ctx!.beginPath();
ctx!.arc(m.x, m.y, 9, 0, Math.PI * 2);
ctx!.fill();
}
window.addEventListener("resize", resize);
resize();
if (reduced) return () => window.removeEventListener("resize", resize);
let next = 1.5;
let last = performance.now();
let raf = 0;
const frame = (now: number) => {
// A hidden tab gets no frames; a long gap is not a burst of meteors.
const dt = Math.min((now - last) / 1000, 0.05);
last = now;
drawStars(now / 1000);
next -= dt;
if (next <= 0) {
spawn();
next = EVERY * (0.5 + Math.random());
}
for (let i = meteors.length - 1; i >= 0; i--) {
const m = meteors[i];
m.life += dt;
m.x += m.dx * dt;
m.y += m.dy * dt;
if (m.life >= m.span) meteors.splice(i, 1);
else drawMeteor(m);
}
raf = requestAnimationFrame(frame);
};
raf = requestAnimationFrame(frame);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", resize);
};
}, []);
return <canvas ref={ref} className="night-sky" aria-hidden="true" />;
}