炫酷代码雨

<!DOCTYPE html>
 
<html lang="en">
 
<head>
 
<meta charset="UTF-8">
 
<title>Code</title>
 
<style>
 
body{
 
margin: 0;
 
overflow: hidden;
 
}
 
</style>
 
</head>
 
<body>
 
<canvas id="myCanvas"></canvas>
 
<script>
    // 获取canvas元素的宽度和高度,并将宽度和高度设置为屏幕的可用宽度和高度 
const width = document.getElementById("myCanvas").width = screen.availWidth;
const height = document.getElementById("myCanvas").height = screen.availHeight;
    //  获取canvas的绘图上下文
const ctx = document.getElementById("myCanvas").getContext("2d");
    // 创建一个大小为width/10的数组并填充为0
const arr = Array(Math.ceil(width / 10)).fill(0);
    // 创建一个字符串用于存储字符
const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
 
function rain() {
    //  设置颜色,并绘制一个全屏的矩形
ctx.fillStyle = "rgba(0,0,0,0.05)";
ctx.fillRect(0, 0, width, height);
    //  设置文字的颜色
ctx.fillStyle = "#0f0";
arr.forEach(function (value, index) {
    //  根据数组的索引值来绘制文字,x坐标为索引值*10,y坐标为value+10
ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, value + 10);
    //从上一次绘制的位置开始,将数组设置为下一次绘制位置
arr[index] = value >= height || value > 8888 * Math.random() ? 0 : value + 10;
});
}
    // 每30毫秒执行一次rain函数
setInterval(rain, 30);
 
</script>
 
</body>
 
</html>

效果展示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值