Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : ORIX App Universe8 * File : src/components/hero/Starfield.tsx9 * Module : components10 * Repository : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website : https://www.orixlab.dev12 * Created : 2026-11-2513 * Last Modified : 2026-11-2514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243"use client";44import { useEffect, useRef } from "react";4546type Star = { x: number; y: number; z: number; px: number; py: number; tint: number };47type Meteor = { x: number; y: number; vx: number; vy: number; life: number };4849/**50 * A field of stars with real depth, drawn on one canvas. It arrives at warp speed, settles into a slow51 * drift, leans with the pointer, and accelerates while the page scrolls. Now and then a meteor crosses.52 * It stops drawing when it is off screen or the tab is hidden, and draws a single still frame when the53 * visitor asks for reduced motion.54 */55export function Starfield({ density = 1, className = "" }: { density?: number; className?: string }) {56 const ref = useRef<HTMLCanvasElement>(null);5758 useEffect(() => {59 const canvas = ref.current;60 const ctx = canvas?.getContext("2d");61 if (!canvas || !ctx) return;62 const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches;63 const dpr = Math.min(window.devicePixelRatio || 1, 2);64 let w = 0, h = 0, raf = 0, visible = true, last = 0;65 let ink = "244,245,247", tints = ["76,141,255", "143,107,255", "63,216,238"];66 const CRUISE = 0.012;67 let speed = reduce ? 0 : 1.6, boost = 0, pxT = 0, pyT = 0, px = 0, py = 0, lastScroll = window.scrollY, nextMeteor = 2600;68 const stars: Star[] = [];69 const meteors: Meteor[] = [];7071 const rgb = (v: string) => {72 const m = v.trim().match(/^#([0-9a-f]{6})$/i);73 if (!m) return null;74 const n = parseInt(m[1], 16);75 return `${n >> 16},${(n >> 8) & 255},${n & 255}`;76 };77 const palette = () => {78 const cs = getComputedStyle(document.documentElement);79 ink = rgb(cs.getPropertyValue("--ink")) ?? ink;80 tints = ["--blue", "--violet", "--cyan"].map((k, i) => rgb(cs.getPropertyValue(k)) ?? tints[i]);81 };82 const spawn = (s: Star, far: boolean) => {83 s.x = (Math.random() * 2 - 1) * 1.25; s.y = (Math.random() * 2 - 1) * 1.25;84 s.z = far ? 1 : 0.08 + Math.random() * 0.92; s.px = NaN; s.py = NaN;85 s.tint = Math.random() < 0.16 ? Math.floor(Math.random() * 3) : -1;86 };87 const resize = () => {88 const r = canvas.getBoundingClientRect();89 w = r.width; h = r.height;90 canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr);91 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);92 const want = Math.round(Math.min(900, (w * h) / 2600) * density);93 while (stars.length < want) { const s = {} as Star; spawn(s, false); stars.push(s); }94 stars.length = want;95 if (reduce) frame(0);96 };9798 const frame = (now: number) => {99 const dt = Math.min(50, last ? now - last : 16) / 1000;100 last = now;101 speed += (CRUISE - speed) * Math.min(1, dt * 1.9);102 boost *= Math.pow(0.04, dt);103 px += (pxT - px) * Math.min(1, dt * 3); py += (pyT - py) * Math.min(1, dt * 3);104 const v = speed + boost, cx = w / 2, cy = h * 0.46, f = Math.max(w, h) * 0.5;105 ctx.clearRect(0, 0, w, h);106 ctx.lineCap = "round";107 for (const s of stars) {108 s.z -= v * dt;109 if (s.z <= 0.03) spawn(s, true);110 const k = 1 / s.z, near = 1 - s.z;111 const x = cx + s.x * k * f * 0.42 + px * near * 46, y = cy + s.y * k * f * 0.42 + py * near * 34;112 if (x < -40 || x > w + 40 || y < -40 || y > h + 40) { spawn(s, true); continue; }113 const alpha = Math.min(1, near * near * 1.5 + 0.08), size = 0.35 + near * 1.5;114 const colour = s.tint < 0 ? ink : tints[s.tint];115 const moved = Number.isNaN(s.px) ? 0 : Math.hypot(x - s.px, y - s.py);116 if (moved > 1.6) {117 ctx.strokeStyle = `rgba(${colour},${alpha})`; ctx.lineWidth = size;118 ctx.beginPath(); ctx.moveTo(s.px, s.py); ctx.lineTo(x, y); ctx.stroke();119 } else {120 ctx.fillStyle = `rgba(${colour},${alpha})`;121 ctx.beginPath(); ctx.arc(x, y, size * 0.62, 0, 6.2832); ctx.fill();122 }123 s.px = x; s.py = y;124 }125 if (!reduce) {126 nextMeteor -= dt * 1000;127 if (nextMeteor <= 0) {128 nextMeteor = 3800 + Math.random() * 5200;129 const left = Math.random() < 0.5;130 meteors.push({ x: left ? Math.random() * w * 0.5 : w * (0.5 + Math.random() * 0.5), y: Math.random() * h * 0.35, vx: (left ? 1 : -1) * (520 + Math.random() * 360), vy: 190 + Math.random() * 170, life: 1 });131 }132 for (let i = meteors.length - 1; i >= 0; i--) {133 const m = meteors[i];134 m.x += m.vx * dt; m.y += m.vy * dt; m.life -= dt * 1.15;135 if (m.life <= 0) { meteors.splice(i, 1); continue; }136 const tx = m.x - m.vx * 0.2, ty = m.y - m.vy * 0.2;137 const g = ctx.createLinearGradient(m.x, m.y, tx, ty);138 g.addColorStop(0, `rgba(${ink},${0.95 * m.life})`); g.addColorStop(0.3, `rgba(${tints[2]},${0.5 * m.life})`); g.addColorStop(1, `rgba(${tints[1]},0)`);139 ctx.strokeStyle = g; ctx.lineWidth = 1.7;140 ctx.beginPath(); ctx.moveTo(m.x, m.y); ctx.lineTo(tx, ty); ctx.stroke();141 }142 if (visible) raf = requestAnimationFrame(frame);143 }144 };145146 const start = () => { if (!reduce && !raf && visible) { last = 0; raf = requestAnimationFrame(frame); } };147 const stop = () => { cancelAnimationFrame(raf); raf = 0; };148 const onPointer = (e: PointerEvent) => { pxT = (e.clientX / window.innerWidth - 0.5) * -2; pyT = (e.clientY / window.innerHeight - 0.5) * -2; };149 const onScroll = () => { const y = window.scrollY; boost = Math.min(1.1, boost + Math.abs(y - lastScroll) * 0.0035); lastScroll = y; };150 const onVisibility = () => { if (document.hidden) stop(); else start(); };151152 palette();153 const ro = new ResizeObserver(resize); ro.observe(canvas);154 const io = new IntersectionObserver(([e]) => { visible = e.isIntersecting; if (visible) start(); else stop(); });155 io.observe(canvas);156 const mo = new MutationObserver(() => { palette(); if (reduce) frame(0); });157 mo.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });158 const scheme = matchMedia("(prefers-color-scheme: light)");159 scheme.addEventListener("change", palette);160 if (!reduce) {161 if (matchMedia("(hover: hover) and (pointer: fine)").matches) window.addEventListener("pointermove", onPointer, { passive: true });162 window.addEventListener("scroll", onScroll, { passive: true });163 document.addEventListener("visibilitychange", onVisibility);164 }165 return () => {166 stop(); ro.disconnect(); io.disconnect(); mo.disconnect(); scheme.removeEventListener("change", palette);167 window.removeEventListener("pointermove", onPointer); window.removeEventListener("scroll", onScroll); document.removeEventListener("visibilitychange", onVisibility);168 };169 }, [density]);170171 return <canvas ref={ref} className={`starfield ${className}`} aria-hidden />;172}173