欢迎与我联系   

念两句诗

楼上望春归去,芳草迷归路。
【宋代】苏轼《桃源忆故人·暮春》

代码雨效果

复制代码
<template>
  <div class="containers">
    <canvas id="bg"></canvas>
  </div>
</template>

<script lang="ts" setup>
import { nextTick, ref } from 'vue';
nextTick(() => {
  const cvs: any = document.getElementById('bg');
  const width = window.innerWidth * devicePixelRatio, height = window.innerHeight * devicePixelRatio;
  cvs.width = width;
  cvs.height = height;
  const ctx = cvs.getContext('2d');
  const fontSize = 20 * devicePixelRatio;
  const columnWidth = fontSize;
  const columnCount = Math.floor(width / columnWidth);
  const nextChars = new Array(columnCount).fill(0);
  const draw = () => {
    ctx.fillStyle = 'rgba(0,0,0,0.1)';
    ctx.fillRect(0, 0, width, height);
    for (let i = 0; i < columnCount; i++) {
      const char = getRandomChar();
      const color = getRandomColor();
      ctx.fillStyle = color;
      ctx.font = `${fontSize}px "Roboto Mono"`;
      const x = columnWidth * i;
      const index = nextChars[i];
      const y = (index + 1) * fontSize;
      ctx.fillText(char, x, y);
      if (y > height && Math.random() > 0.99) {
        nextChars[i] = 0;
      } else {
        nextChars[i]++;
      }
    }
  }
  const getRandomColor = () => {
    const fontColors = [
      "#33b5e5",
      "#0099cc",
      "#aa66cc",
      "#9933cc",
      "#A0522D",
      "#A52A2A",
      "#ADFF2F",
      "#C71585",
      "#DAA520",
      "#FF00FF"
    ];
    return fontColors[Math.floor(Math.random() * fontColors.length)];
  }
  const getRandomChar = () => {
    const str = 'I LOVE YOU &=& i love you';
    return str[Math.floor(Math.random() * str.length)]
  }
  draw();
  setInterval(() => {
    draw();
  }, 40);
});
</script>
<style lang="scss" scoped>
.containers {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

#bg {
  background: #000;
  height: 100%;
  width: 100%;
}
</style>
复制代码

 

posted @   小珍珠在河里敲代码  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示