data-stream.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. (function(){
  2. window.HPX = window.HPX || {};
  3. window.HPX['data-stream'] = function(el){
  4. const U = window.HPX._u;
  5. const k = U.canvas(el), ctx = k.ctx;
  6. const ac = U.accent(el,'#22d3ee'), ac2 = U.accent2(el,'#7c5cff');
  7. const rows = [];
  8. const rh = 22;
  9. const genRow = (y) => ({
  10. y, dir: Math.random()<0.5?-1:1,
  11. speed: U.rand(30, 90),
  12. offset: Math.random()*2000,
  13. text: Array.from({length:120}, () => {
  14. const r = Math.random();
  15. if (r<0.3) return Math.random()<0.5?'0':'1';
  16. if (r<0.6) return '0x' + Math.floor(Math.random()*256).toString(16).padStart(2,'0');
  17. return Math.random().toString(16).slice(2,6);
  18. }).join(' ')
  19. });
  20. const init = () => {
  21. rows.length = 0;
  22. const n = Math.ceil(k.h/rh);
  23. for (let i=0;i<n;i++) rows.push(genRow(i*rh + rh*0.7));
  24. };
  25. init();
  26. let lh = k.h;
  27. const stop = U.loop((t) => {
  28. if (k.h!==lh){ init(); lh=k.h; }
  29. ctx.fillStyle = 'rgba(5,8,14,0.35)';
  30. ctx.fillRect(0,0,k.w,k.h);
  31. ctx.font = '13px ui-monospace,Menlo,monospace';
  32. for (let i=0;i<rows.length;i++){
  33. const r = rows[i];
  34. const x = r.dir>0
  35. ? ((t*r.speed + r.offset) % (k.w+400)) - 400
  36. : k.w - (((t*r.speed + r.offset) % (k.w+400)) - 400);
  37. ctx.fillStyle = (i%3===0)?ac:ac2;
  38. ctx.globalAlpha = 0.65 + (i%2)*0.3;
  39. ctx.fillText(r.text, x, r.y);
  40. }
  41. ctx.globalAlpha = 1;
  42. });
  43. return { stop(){ stop(); k.destroy(); } };
  44. };
  45. })();