代码雨动画

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>
相关推荐
wei_shuo6 天前
从数据中台到数据飞轮:实现数据驱动的升级之路
1024程序员节·数据飞轮
玖剹20 天前
矩阵区域和 --- 前缀和
数据结构·c++·算法·leetcode·矩阵·动态规划·1024程序员节
jamison_12 个月前
文心一言与 DeepSeek 的竞争分析:技术先发优势为何未能转化为市场主导地位?
人工智能·ai·chatgpt·gpt-3·1024程序员节
NaZiMeKiY2 个月前
HTML5前端第六章节
前端·html·html5·1024程序员节
jamison_12 个月前
颠覆未来:解锁ChatGPT衍生应用的无限可能(具体应用、功能、付费模式与使用情况)
ai·chatgpt·1024程序员节
NaZiMeKiY2 个月前
HTML5前端第七章节
1024程序员节
earthzhang20212 个月前
《Python深度学习》第四讲:计算机视觉中的深度学习
人工智能·python·深度学习·算法·计算机视觉·numpy·1024程序员节
明明真系叻3 个月前
2025.3.2机器学习笔记:PINN文献阅读
人工智能·笔记·深度学习·机器学习·1024程序员节·pinn
bitenum3 个月前
【C++/数据结构】队列
c语言·开发语言·数据结构·c++·青少年编程·visualstudio·1024程序员节
IT学长编程3 个月前
计算机毕业设计 基于SpringBoot的智慧社区管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·后端·毕业设计·课程设计·论文笔记·1024程序员节