电影《黑客帝国》里电脑页面那种代码雨,感觉感觉很炫酷,我上网搜了一下源代码,发一下,以免丢失,觉得可以的请给个一键三连,
源代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code</title>
<style>
body{
margin: 0;
overflow: hidden;
font-size: 12px;
}
</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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01314521".split("");
function rain() {
//这里的重点就是每次调用这个方法的时候都会重新绘制一张透明度为0.05的黑色画布来覆盖前图
//所以就会形成人眼中的下雨效果
ctx.fillStyle = "rgba(0,0,0,0.06)";
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 >66666 * Math.random() ? 0 : value + 10;
});
}
setInterval(rain, 50);
</script>
</body>
</html>