电子雨html代码

本文介绍了一段使用HTML5Canvas和JavaScript编写的代码,通过创建一个动态的随机文字动画,显示在画布上,用户可以复制代码并参与互动。作者鼓励读者点赞和支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说下面是代码:

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

const width = document.getElementById("myCanvas").width = screen.availWidth;

const height = document.getElementById("myCanvas").height = screen.availHeight;

const ctx = document.getElementById("myCanvas").getContext("2d");

const arr = Array(Math.ceil(width / 10)).fill(0);

const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".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) {

ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, value + 10);

arr[index] = value >= height || value > 8888 * Math.random() ? 0 : value + 10;

});

}

setInterval(rain, 30);

</script>

</body>

</html>

下面是代码效果

代码可以直接复制

如果有啥问题可以问我看到一定会回复大家,如果大家喜欢可以作者点赞和关注

大家的支持是我创作下去的最大动力!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值