代码雨动画

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>
相关推荐
智者知已应修善业8 小时前
【输入两个数字,判断两数相乘是否等于各自逆序数相乘】2023-10-24
c语言·c++·经验分享·笔记·算法·1024程序员节
CoderYanger18 小时前
动态规划算法-子数组、子串系列(数组中连续的一段):21.乘积最大子数组
开发语言·算法·leetcode·职场和发展·动态规划·1024程序员节
CoderYanger18 小时前
A.每日一题——3432. 统计元素和差值为偶数的分区方案
java·数据结构·算法·leetcode·1024程序员节
CoderYanger19 小时前
动态规划算法-子数组、子串系列(数组中连续的一段):26.环绕字符串中唯一的子字符串
java·算法·leetcode·动态规划·1024程序员节
韩家阿杰2 天前
RabbitMQ技术的使用
1024程序员节
CoderYanger3 天前
动态规划算法-简单多状态dp问题:15.买卖股票的最佳时机含冷冻期
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger3 天前
递归、搜索与回溯-FloodFill:33.太平洋大西洋水流问题
java·算法·leetcode·1024程序员节
CoderYanger3 天前
动态规划算法-斐波那契数列模型:2.三步问题
开发语言·算法·leetcode·面试·职场和发展·动态规划·1024程序员节
CoderYanger3 天前
动态规划算法-简单多状态dp问题:16.买卖股票的最佳时机含手续费
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger3 天前
C.滑动窗口-求子数组个数-越短越合法——3258. 统计满足 K 约束的子字符串数量 I
java·开发语言·算法·leetcode·1024程序员节