代码雨动画

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>
相关推荐
小坏讲微服务6 小时前
MaxWell中基本使用原理 完整使用 (第一章)
大数据·数据库·hadoop·sqoop·1024程序员节·maxwell
liu****20 小时前
18.HTTP协议(一)
linux·网络·网络协议·http·udp·1024程序员节
洛_尘20 小时前
JAVA EE初阶 6: 网络编程套接字
网络·1024程序员节
2301_800256111 天前
关系数据库小测练习笔记(1)
1024程序员节
金融小师妹2 天前
基于多源政策信号解析与量化因子的“12月降息预期降温”重构及黄金敏感性分析
人工智能·深度学习·1024程序员节
GIS数据转换器2 天前
基于GIS的智慧旅游调度指挥平台
运维·人工智能·物联网·无人机·旅游·1024程序员节
南方的狮子先生2 天前
【C++】C++文件读写
java·开发语言·数据结构·c++·算法·1024程序员节
Neil今天也要学习3 天前
永磁同步电机无速度算法--基于三阶LESO的反电动势观测器
算法·1024程序员节
开开心心_Every3 天前
专业视频修复软件,简单操作效果好
学习·elasticsearch·pdf·excel·音视频·memcache·1024程序员节
liu****4 天前
16.udp_socket(三)
linux·开发语言·数据结构·c++·1024程序员节