5745
Sc
sacenox.github.io / files
sacenox.github.io / assets / js / galaxy.js
4.0 KB
1(function () { 2 var canvas = document.getElementById('galaxy'); 3 if (!canvas) return; 4 5 var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; 6 var ctx = canvas.getContext('2d'); 7 8 var palette = { 9 dark: { 10 bg: '#262427', 11 colors: ['#ff7272', '#bcdf59', '#ffca58', '#49cae4', '#a093e2', '#aee8f4'], 12 alphaMin: 0.18, 13 alphaRange: 0.30, 14 glowInner: 'rgba(160, 147, 226, 0.06)', 15 glowMid: 'rgba(73, 202, 228, 0.02)', 16 count: 500, 17 }, 18 light: { 19 bg: '#FAFAFA', 20 colors: ['#D1364E', '#6EA226', '#C08A19', '#2B8DB3', '#7C5EC1', '#2B9DA4'], 21 alphaMin: 0.12, 22 alphaRange: 0.22, 23 glowInner: 'rgba(43, 141, 179, 0.05)', 24 glowMid: 'rgba(124, 94, 193, 0.02)', 25 count: 400, 26 }, 27 }; 28 29 var width = 0; 30 var height = 0; 31 var centerX = 0; 32 var centerY = 0; 33 var particles = []; 34 35 function isDark() { 36 return document.documentElement.classList.contains('dark'); 37 } 38 function theme() { 39 return isDark() ? palette.dark : palette.light; 40 } 41 42 function resize() { 43 var ratio = window.devicePixelRatio || 1; 44 width = window.innerWidth; 45 height = window.innerHeight; 46 canvas.width = width * ratio; 47 canvas.height = height * ratio; 48 canvas.style.width = width + 'px'; 49 canvas.style.height = height + 'px'; 50 ctx.setTransform(ratio, 0, 0, ratio, 0, 0); 51 centerX = width * 0.5; 52 centerY = height * 0.5; 53 } 54 55 function makeParticles() { 56 var t = theme(); 57 var count = Math.min(t.count, Math.floor((width + height) / 3)); 58 particles = []; 59 for (var i = 0; i < count; i++) { 60 var bright = Math.random() < 0.06; 61 particles.push({ 62 radius: Math.random() * Math.min(width, height) * 0.5 + 10, 63 angle: Math.random() * Math.PI * 2, 64 speed: 0.0004 + Math.random() * 0.0013, 65 size: bright ? 1.6 + Math.random() * 1.8 : 0.7 + Math.random() * 1.5, 66 color: t.colors[Math.floor(Math.random() * t.colors.length)], 67 baseAlpha: bright 68 ? t.alphaMin + t.alphaRange * 0.9 69 : t.alphaMin + Math.random() * t.alphaRange, 70 drift: (Math.random() - 0.5) * 0.15, 71 phase: Math.random() * Math.PI * 2, 72 twinkle: 0.0006 + Math.random() * 0.0025, 73 bright: bright, 74 }); 75 } 76 } 77 78 function recolor() { 79 var t = theme(); 80 for (var i = 0; i < particles.length; i++) { 81 var p = particles[i]; 82 p.color = t.colors[Math.floor(Math.random() * t.colors.length)]; 83 p.baseAlpha = p.bright 84 ? t.alphaMin + t.alphaRange * 0.9 85 : t.alphaMin + Math.random() * t.alphaRange; 86 } 87 } 88 89 function frame(time) { 90 var t = theme(); 91 92 ctx.clearRect(0, 0, width, height); 93 94 ctx.fillStyle = t.bg; 95 ctx.fillRect(0, 0, width, height); 96 97 /* Nebula core glow */ 98 var r = Math.min(width, height) * 0.42; 99 var grad = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, r); 100 grad.addColorStop(0, t.glowInner); 101 grad.addColorStop(0.5, t.glowMid); 102 grad.addColorStop(1, 'transparent'); 103 ctx.fillStyle = grad; 104 ctx.fillRect(0, 0, width, height); 105 106 for (var i = 0; i < particles.length; i++) { 107 var p = particles[i]; 108 var spiral = p.radius / 85; 109 var x = centerX + Math.cos(p.angle + spiral) * p.radius; 110 var y = centerY + Math.sin(p.angle + spiral) * p.radius; 111 112 var flicker = Math.sin(time * p.twinkle + p.phase) * 0.06; 113 ctx.globalAlpha = Math.max(0.02, p.baseAlpha + flicker); 114 ctx.fillStyle = p.color; 115 ctx.beginPath(); 116 ctx.arc(x, y, p.size, 0, Math.PI * 2); 117 ctx.fill(); 118 119 if (!reduceMotion) { 120 p.angle += p.speed; 121 p.radius += p.drift * 0.1; 122 } 123 } 124 125 ctx.globalAlpha = 1; 126 127 if (!reduceMotion) { 128 requestAnimationFrame(frame); 129 } 130 } 131 132 resize(); 133 makeParticles(); 134 135 if (reduceMotion) { 136 frame(0); 137 } else { 138 requestAnimationFrame(frame); 139 } 140 141 window.addEventListener('resize', function () { 142 resize(); 143 makeParticles(); 144 if (reduceMotion) frame(0); 145 }); 146 147 window.addEventListener('themechange', recolor); 148})();