Guest User

Matrix with time

a guest
Apr 18th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Japanese Matrix Background with Time</title>
  5. <style>
  6. body { background-color: black; overflow: hidden; }
  7. canvas { display: block; position: absolute; top: 0; left: 0; }
  8. </style>
  9. </head>
  10. <body>
  11. <canvas id="matrix"></canvas>
  12. <canvas id="time"></canvas>
  13.  
  14. <script>
  15. const mc = document.getElementById('matrix'), mx = mc.getContext('2d'), tc = document.getElementById('time'), tx = tc.getContext('2d');
  16. let mw = mc.width = tc.width = window.innerWidth, mh = mc.height = tc.height = window.innerHeight;
  17. const h = 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん', fs = 16, c = mw / fs, d = [];
  18. let s = 33, timeX = mw / 2, timeY = mh / 2, tvx = 1, tvy = 1, ts = 1, tfs = 0, tst = 0; // tst: 0-bouncing, 1-middle, 2-hidden
  19. for (let i = 0; i < c; i++) d[i] = 1;
  20.  
  21. function draw() {
  22. mx.fillStyle = 'rgba(0,0,0,0.05)';
  23. mx.fillRect(0, 0, mw, mh);
  24. mx.fillStyle = '#0F0';
  25. mx.font = `${fs}px monospace`;
  26. for (let i = 0; i < d.length; i++) {
  27. mx.fillText(h[Math.floor(Math.random() * h.length)], i * fs, d[i] * fs);
  28. if (d[i] * fs > mh && Math.random() > 0.975) d[i] = 0;
  29. d[i]++;
  30. }
  31.  
  32. tx.clearRect(0, 0, mw, mh);
  33. if (tst !== 2) {
  34. const now = new Date(), hrs = now.getHours().toString().padStart(2, '0'), min = now.getMinutes().toString().padStart(2, '0'), sec = now.getSeconds().toString().padStart(2, '0'), timeString = `${hrs}:${min}:${sec}`;
  35. tx.fillStyle = 'rgba(0,255,0,0.25)';
  36. tx.font = `${tfs}px monospace`;
  37. tx.textAlign = 'center';
  38. tx.textBaseline = 'middle';
  39. const tw = tx.measureText(timeString).width, th = tfs;
  40. if (tst === 0) {
  41. timeX += tvx * ts;
  42. timeY += tvy * ts;
  43. if (timeX - tw / 2 < 0 || timeX + tw / 2 > mw) tvx *= -1;
  44. if (timeY - th / 2 < 0 || timeY + th / 2 > mh) tvy *= -1;
  45. } else if (tst === 1) {
  46. timeX = mw / 2;
  47. timeY = mh / 2;
  48. }
  49. tx.fillText(timeString, timeX, timeY);
  50. }
  51. }
  52.  
  53. function resizeCanvas() {
  54. mw = mc.width = tc.width = window.innerWidth;
  55. mh = mc.height = tc.height = window.innerHeight;
  56. tfs = Math.min(mw, mh) * 0.2;
  57. }
  58.  
  59. resizeCanvas();
  60. let interval = setInterval(draw, s);
  61. window.addEventListener('resize', resizeCanvas);
  62. document.addEventListener('keydown', e => {
  63. if (e.key === 'ArrowUp' && s > 10) { s -= 5; clearInterval(interval); interval = setInterval(draw, s); }
  64. else if (e.key === 'ArrowDown') { s += 5; clearInterval(interval); interval = setInterval(draw, s); }
  65. else if (e.key === 'ArrowLeft' && ts > 1) ts--;
  66. else if (e.key === 'ArrowRight') ts++;
  67. else if (e.code === 'Space') tst = (tst + 1) % 3;
  68. });
  69. </script>
  70. </body>
  71. </html>
Tags: Matrix
Advertisement
Add Comment
Please, Sign In to add comment