首页 > 其他分享 >代码雨效果

代码雨效果

时间:2024-03-13 12:33:31浏览次数:22  
标签:nextChars const 效果 代码 ctx height width Math

<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>

 

标签:nextChars,const,效果,代码,ctx,height,width,Math
From: https://www.cnblogs.com/Jansens520/p/18070356

相关文章

  • Qt warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为
    Qtwarning:C4819:该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为Unicode格式以防止数据丢失Qt导入其他电脑上开发的项目后,出现这种警告:Qtwarning:C4819:该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为Unicode格式以防止数据丢失......
  • 实训5_“长征•人员损失”模块实现【附完整代码】
    实训:“长征•人员损失”模块实现在本实训中,我们继续修改首页的相关内容,添加一个呈现长征中各支红军队伍的人员损失统计信息的“长征•人员损失”模块,并利用echarts框架实现数据的图形化展示,具体步骤如下:修改global_data.py(位于项目根目录下的models/子目录内),在文件中原......
  • MyBatisPlus代码生成器(新)
    MyBatisPlus代码生成器(新)注意:适用版本:mybatis-plus-generator3.5.1以上版本参考:官网本次配置:JDK17+SpringBoot3.1.5+MyBatisPlus3.5.3.1注意:mybatis-plus-generator版本需与mybatis-plus版本一致最新依赖参考:https://mvnrepository.com/artifact/com.baomid......
  • 圆形放大的hover效果
    <template><divclass="container"><divclass="avatar"></div></div></template><scriptlang="ts"setup>import{ref}from'vue';</script><stylelang=......
  • 蜂巢布局效果
    <template><divclass="container"><divclass="line"v-for="(items,index)inlist":key="index"><divclass="item"v-for="(item,index)initems":key="inde......
  • 解决方案 | 一个VBA代码里面非常隐蔽的错误:运行时错误“5”:无效的过程调用或参数
    1代码部分代码功能:实现使用sumatra打开指定pdf指定页码代码:SubOpenPDFatPage()DimPDFFileAsStringDimPageNumberAsLongDimSumatraPathAsString'PDF文件路径PDFFile="C:\Users\Administrator\Desktop\22.pdf"'要打开的页......
  • 在Java中如何通过优化代码来节省内存
    Java程序的一个常见问题是高内存使用率,这会导致性能问题甚至崩溃。因此,需要使用内存节省技术来优化Java代码并减少内存使用非常重要。选择正确的数据类型:使用适当大小的数据类型可以避免不必要的内存浪费。例如,如果你知道一个整数的取值范围在-128到127之间,那么使用byte类......
  • 【嵌入式】从混乱到秩序:Code-Review代码审查助力代码质量飞跃(提供完整审查项checklist
    ......
  • 20个Python random模块的代码示例
    本文分享自华为云社区《Python随机数探秘:深入解析random模块的神奇之处》,作者:柠檬味拥抱。标准库random函数大全:探索Python中的随机数生成随机数在计算机科学和数据科学领域中扮演着重要角色,Python的标准库中提供了random模块,用于生成各种随机数。本篇博客将深入探讨random模块......
  • css 闪烁 左右摆动效果
    代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title></title><style></style></head><body><divstyle="display:......