使用canvas实现代码雨高级升阶版【附带源码和使用方法】

简介: 使用canvas实现代码雨高级升阶版【附带源码和使用方法】

前言

hello world欢迎来到前端的新世界


😜当前文章系列专栏:前端面试

🐱‍👓博主在前端领域还有很多知识和技术需要掌握,正在不断努力填补技术短板。(如果出现错误,感谢大家指出)🌹

💖感谢大家支持!您的观看就是作者创作的动力

基本绿色的


<!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>
        * {
            padding: 0;
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script src="index.js"></script>
</body>
</html>


let canvas = document.querySelector("canvas")
let ctx = canvas.getContext("2d");
canvas.width = screen.availWidth;
canvas.height = screen.availHeight;
let str = "鋜 斗 z s y y d s 加 油 干".split(" ");
let arr = Array(Math.ceil(canvas.width / 10)).fill(0);
const rain = () => {
    ctx.fillStyle = "rgba(0,0,0,0.05)";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = "#0f0";
    arr.forEach((item, index) => {
        ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, item + 10)
        arr[index] = item > canvas.height || item > Math.random() * 10000 ? 0 : item + 10;
    })
}
setInterval(rain, 40)


彩色版本



html


<!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>
        * {
            padding: 0;
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script src="index.js"></script>
</body>
</html>


js


let canvas = document.querySelector("canvas");
let ctx = canvas.getContext("2d");
canvas.width = screen.availWidth;
canvas.height = screen.availHeight;
let str = "鋜 斗 z s y y d s 加 油 干".split(" ");
let arr = Array(Math.ceil(canvas.width / 10)).fill(0);
const colors = ["#0f0", "#f00", "#00f", "#ff0", "#0ff"]; // 添加颜色数组
const rain = () => {
    ctx.fillStyle = "rgba(0,0,0,0.05)";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    arr.forEach((item, index) => {
        const randomColor = colors[Math.floor(Math.random() * colors.length)]; // 随机选取颜色
        ctx.fillStyle = randomColor; // 使用随机颜色
        ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, item + 10);
        arr[index] = item > canvas.height || item > Math.random() * 10000 ? 0 : item + 10;
    });
};
setInterval(rain, 40);


飘散雪花状



<!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>
        * {
            padding: 0;
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script src="index.js"></script>
</body>
</html>


let canvas = document.querySelector("canvas");
let ctx = canvas.getContext("2d");
canvas.width = screen.availWidth;
canvas.height = screen.availHeight;
let str = "鋜 斗 加 油 猛 猛 干 ".split(" ");
let strIndex = 0;
let arr = Array(Math.ceil(canvas.width / 10)).fill(0);
class Drop {
  constructor() {
    this.x = Math.random() * canvas.width;
    this.y = Math.random() * -canvas.height;
    this.speed = Math.random() * 2 + 2;
    this.color = "#" + Math.floor(Math.random() * 16777215).toString(16);
    this.height = Math.random() * 20 + 10;
    this.text = str[strIndex];
    strIndex = (strIndex + 1) % str.length;
  }
  update() {
    this.y += this.speed;
    if (this.y > canvas.height) {
      this.y = Math.random() * -canvas.height;
      this.x = Math.random() * canvas.width;
      this.color = "#" + Math.floor(Math.random() * 16777215).toString(16);
      this.height = Math.random() * 20 + 10;
      this.text = str[strIndex];
      strIndex = (strIndex + 1) % str.length;
    }
  }
  draw() {
    ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = "white";
    ctx.font = this.height + "px Arial";
    ctx.fillText(this.text, this.x, this.y + this.height);
  }
}
let drops = [];
for (let i = 0; i < 100; i++) {
  drops.push(new Drop());
}
const animate = () => {
  drops.forEach((drop) => {
    drop.update();
    drop.draw();
  });
  requestAnimationFrame(animate);
};
animate();


后言


创作不易,要是本文章对广大读者有那么一点点帮助 不妨三连支持一下,您的鼓励就是博主创作的动力


目录
相关文章
|
开发工具 git
如何在vscode编辑器中实时查看代码git记录(被谁修改、自己什么时候修改)
如何在vscode编辑器中实时查看代码git记录(被谁修改、自己什么时候修改)
7009 0
如何在vscode编辑器中实时查看代码git记录(被谁修改、自己什么时候修改)
|
文字识别 自然语言处理 达摩院
一文上手文档智能Document Mind
简要讲述文档智能Document Mind以及文档智能的功能测试
一文上手文档智能Document Mind
|
Kubernetes API 数据库
Crossplane - 比 Terraform 更先进的云基础架构管理平台?
Crossplane - 比 Terraform 更先进的云基础架构管理平台?
|
数据采集 运维 搜索推荐
手把手教你如何申请网站空间
网站空间就像房屋的地基,对网站运营的影响非常大。在网站还没有发布之前,首先需要考虑存放的空间。
1872 0
手把手教你如何申请网站空间
|
6月前
|
存储 JavaScript 前端开发
前端实现动态路由vue
该代码实现了Vue动态路由功能,包含基础路由配置、路由守卫、动态添加路由及登录认证。用户登录成功后,将动态加载仪表盘路由组件,实现按需加载。
171 0
|
11月前
|
开发者 索引
HarmonyOS使用系统图标
HarmonyOS图标符号是系统内置的图标资源库,开发者可通过SymbolGlyph和SymbolSpan组件高效引用图标资源,简化开发流程并确保应用与系统设计风格一致。通过`$r(&#39;sys.symbol.resource_name&#39;)`访问系统图标资源,支持调整大小、颜色、粗细、渲染策略及动效。更多示例和学习资料详见官方文档和教程。
715 2
HarmonyOS使用系统图标
|
12月前
|
人工智能
RAG - 拒识模块
在RAG(Retrieval-Augmented Generation)模型中,拒识模块(或称为拒绝模块,Reject Module)是一个重要的组成部分,旨在提高生成模型在面对不相关查询或信息时的鲁棒性。RAG模型结合了检索和生成两种能力,通过在生成过程中引入外部文档来增强生成的质量和准确性。
735 2
|
运维 监控 安全
系统日志规范问题之日志打印等级的DEBUG的定义如何解决
系统日志规范问题之日志打印等级的DEBUG的定义如何解决
|
资源调度 前端开发
vue3.2最新语法如何使用socket.io实现即时通讯
vue3.2最新语法如何使用socket.io实现即时通讯
327 3
|
Shell Linux 网络安全
shell中 << EOF 和 EOF 使用
shell中 << EOF 和 EOF 使用
1016 0