类似黑客帝国的代码雨

<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8">
    <title>code rain</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      body {
        background: black;
      }

      canvas {
        display: block;
      }
    </style>
  </head>

  <body>
    <script>
      const canvas = document.createElement("canvas");
      canvas.setAttribute("width", window.innerWidth);
      canvas.setAttribute("height", window.innerHeight);
      document.body.append(canvas);

      const tmp = "abcdefghijklmnopqrstuvwxyz";
      const fontsize = 16;
      const columns = Math.ceil(canvas.width / fontsize);
      const drop = Array.from({ length: columns }).fill(0);
      const ctx = canvas.getContext("2d");
      function drap() {
        ctx.fillStyle = "rgba(0,0,0,0.07)";
        // a,b,c,d分别代表x方向偏移,y方向偏移,宽,高
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = "#0F0";
        ctx.font = fontsize + "px arial";
        for (let i = 0; i < drop.length; i++) {
          const text = tmp[Math.floor(Math.random() * tmp.length)];
          ctx.fillText(text, i * fontsize, drop[i] * fontsize);
          drop[i]++;
          if (drop[i] * fontsize > canvas.height && Math.random() > 0.9) {//90%的几率掉落
            drop[i] = 0;
          }
        }
      }
      setInterval(drap, 80);
      window.addEventListener("resize", () => {
        canvas.setAttribute("width", window.innerWidth);
        canvas.setAttribute("height", window.innerHeight);
      });
    </script>
  </body>

</html>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值