返回 html-ppt-skill
starfield.js
根目录 / assets / animations / fx / starfield.js
1 (function(){
2 window.HPX = window.HPX || {};
3 window.HPX['starfield'] = function(el){
4 const U = window.HPX._u;
5 const k = U.canvas(el), ctx = k.ctx;
6 const tx = U.text(el, '#ffffff');
7 const N = 260;
8 const stars = Array.from({length:N}, () => ({
9 x: U.rand(-1,1), y: U.rand(-1,1), z: Math.random()
10 }));
11 const stop = U.loop(() => {
12 ctx.fillStyle = 'rgba(0,0,0,0.25)';
13 ctx.fillRect(0,0,k.w,k.h);
14 const cx = k.w/2, cy = k.h/2;
15 for (const s of stars){
16 s.z -= 0.006;
17 if (s.z <= 0.02) { s.x = U.rand(-1,1); s.y = U.rand(-1,1); s.z = 1; }
18 const px = cx + (s.x/s.z)*cx;
19 const py = cy + (s.y/s.z)*cy;
20 if (px<0||py<0||px>k.w||py>k.h) continue;
21 const r = (1-s.z)*2.4;
22 ctx.globalAlpha = 1-s.z;
23 ctx.fillStyle = tx;
24 ctx.beginPath(); ctx.arc(px,py,r,0,Math.PI*2); ctx.fill();
25 }
26 ctx.globalAlpha = 1;
27 });
28 return { stop(){ stop(); k.destroy(); } };
29 };
30 })();
31
31 lines JAVASCRIPT