代码雨动画

js 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta
    name="viewport"
    content="width=device-width, initial-scale=1.0"
  >
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    canvas {
      width: 100%;
      height: 100vh;
      display: block;
      background-color: #000;
    }
  </style>
</head>

<body>
  <canvas
    width="1920"
    height="920"
  ></canvas>

  <script>
    const cvs = document.querySelector('canvas');
    const ctx = cvs.getContext('2d');
    const w = cvs.width;
    const h = cvs.height;
    const str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    const randomStr = () => str[Math.floor(Math.random() * str.length)]
    const randomColor = () => `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
    const fontW = 28;
    const fontH = 28;
    const columns = Math.ceil(w / fontW);
    const rows = Math.ceil(h / fontH);
    const startH = Array.from({ length: columns }, () => parseInt(Math.random() * 30) - 15);
    ctx.textAlign = 'left'
    ctx.textBaseline = 'top'

    function draw() {
      ctx.fillStyle = 'rgba(0,0,0,0.04)';
      ctx.fillRect(0, 0, w, h);
      for (let i = 0; i < columns; i++) {
        const c = randomColor()
        const str = randomStr();
        const color = c;
        const x = i * fontW;
        const y = startH[i] * fontH;
        ctx.fillStyle = color;
        ctx.font = `${fontH - 4}px Arial`;
        ctx.fillText(str, x, y);
        if (startH[i] > rows) {
          startH[i] = 0 - parseInt(Math.random() * 15);
        } else {
          startH[i] += 1;
        }
      }
    }

    function startDraw() {
      draw()
      setTimeout(startDraw, 50);
    }

    startDraw()
  </script>
</body>

</html>
相关推荐
彩云回5 小时前
多维尺度分析法(MDS)
人工智能·机器学习·1024程序员节
j_xxx404_6 小时前
Linux:权限复盘扩展|粘滞位|软件包管理器|Linux软件生态
linux·1024程序员节
pp-周子晗(努力赶上课程进度版)8 小时前
WebRTC项目架构详解
1024程序员节
Yupureki11 小时前
从零开始的C++学习生活 20:数据结构与STL复习课(4.4w字全解析)
c语言·数据结构·c++·学习·visual studio·1024程序员节
Predestination王瀞潞14 小时前
Java EE开发技术(第五章:JSP技术)
1024程序员节
AORO202515 小时前
三防平板三防是指哪三防?适合应用在什么场景?
服务器·网络·智能手机·电脑·1024程序员节
极客数模16 小时前
【浅析赛题,一等奖水平】思路模型数据相关资料!2025 年“大湾区杯”粤港澳金融数学建模竞赛B 题 稳定币的综合评价与发展分析~
大数据·算法·数学建模·金融·数据挖掘·图论·1024程序员节
再睡一夏就好17 小时前
【C++闯关笔记】使用红黑树简单模拟实现map与set
java·c语言·数据结构·c++·笔记·语法·1024程序员节
TDengine (老段)17 小时前
益和热力性能优化实践:从 SQL Server 到 TDengine 时序数据库,写入快 20 秒、查询提速 5 倍
大数据·数据库·物联网·性能优化·时序数据库·tdengine·1024程序员节
B站计算机毕业设计之家19 小时前
python图像识别系统 AI多功能图像识别检测系统(11种识别功能)银行卡、植物、动物、通用票据、营业执照、身份证、车牌号、驾驶证、行驶证、车型、Logo✅
大数据·开发语言·人工智能·python·图像识别·1024程序员节·识别