DigitalRain

功能描述

依赖模块

快速使用

  1. <div style="text-align: center; margin-top:0;">
  2. <canvas id="canvas"></canvas>
  3. </div>
  4. window.onload = function() {
  5. var canv = document.getElementById('canvas');
  6. var cw = document.documentElement.clientWidth;
  7. var ch = document.documentElement.clientHeight;
  8. canv.width = cw;
  9. canv.height = "667";
  10. var bc = 120;
  11. var bcIndex = 5;
  12. var shadowBlur = 2;
  13. var sbIndex = 0.4;
  14. var fh = 210;
  15. var ctx = canv.getContext('2d');
  16. var fontSize = 20;
  17. var columns = Math.ceil(cw / fontSize); //计算适合的列数
  18. var arr = [];
  19. var texts = '';
  20. for (var i = 0; i < columns; i++) {
  21. arr[i] = 1;
  22. }
  23. //数字转ascii 字母
  24. function time() {
  25. var d = new Date();
  26. console.log(d.getTime());
  27. return d.getTime();
  28. }
  29. return "#" + Math.round(Math.random() * 255).toString(16) + Math.round(Math.random() * 255).toString(16) + Math.round(Math.random() * 255).toString(16);
  30. }
  31. texts = time().toString().split(""); //字符源
  32. ctx.fillStyle = "rgba(0,0,0,0.08)";
  33. ctx.fillRect(0, 0, canv.width, canv.height);
  34. // ctx.fillStyle = "#ff0000";
  35. ctx.fillStyle = color();
  36. ctx.font = fontSize + "px Arial";
  37. ctx.textAlign = 'center';
  38. ctx.textBaseline = 'middle';
  39. for (var i = 0; i < arr.length; i++) {
  40. var text = texts[Math.floor(Math.random() * texts.length)];
  41. ctx.fillText(text, i * fontSize, arr[i] * fontSize);
  42. if (arr[i] * fontSize > ch || Math.random() > 0.95) {
  43. arr[i] = 0;
  44. }
  45. arr[i]++;
  46. }
  47. drawText();
  48. window.onresize = function() {
  49. cw = document.documentElement.clientWidth;
  50. ch = document.documentElement.clientHeight;
  51. canv.width = cw;
  52. canv.height = ch;
  53. columns = Math.ceil(cw / fontSize);
  54. arr = [];
  55. for (var i = 0; i < columns; i++) {
  56. arr[i] = 1;
  57. }
  58. }
  59. ctx.save();
  60. var title = "";
  61. var title2 = "";
  62. ctx.fillStyle = "rgba(0,24,0,1)";
  63. ctx.font = fh + "px Arial";
  64. ctx.textAlign = 'center';
  65. ctx.textBaseline = 'middle';
  66. ctx.fontWeight = 900;
  67. ctx.shadowOffsetY = 0;
  68. //字体阴影宽度变化
  69. shadowBlur += sbIndex;
  70. ctx.shadowBlur = shadowBlur;
  71. if (shadowBlur >= 14) {
  72. sbIndex *= -1
  73. } else if (shadowBlur <= 0) {
  74. sbIndex *= -1;
  75. }
  76. //字体阴影颜色变化
  77. bc += bcIndex;
  78. ctx.shadowColor = 'rgba(0,' + bc + ',0,1)';
  79. if (bc >= 255) {
  80. bcIndex *= -1;
  81. } else if (bc <= 115) {
  82. bcIndex *= -1;
  83. }
  84. ctx.fillText(title, cw / 2, ch / 2 - 55);
  85. ctx.font = 80 + "px Arial";
  86. ctx.fillText(title2, cw / 2, ch / 2 + 45);
  87. ctx.restore();
  88. }
  89. setInterval(run, 40);
  90. }

特别说明