不需要使用任何插件,html直接使用canvas实现的黑客帝国风格的代码雨特效。
代码十分简洁,如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>代码雨</title>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const width = document.getElementById("canvas").width = screen.availWidth;
const height = document.getElementById("canvas").height = screen.availHeight;
const ctx = document.getElementById("canvas").getContext("2d");
const arr = Array(Math.ceil(width / 10)).fill(0);
const str = "黑客帝国代码雨编程侠".split("");
function rain() {
ctx.fillStyle = "rgba(0,0,0,0.06)";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "#0f0";
ctx.font = "16px Arial";
arr.forEach(function (value, index) {
ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, value + 18);
arr[index] = value >= height || value > 66666 * Math.random() ? 0 : value + 18;
});
}
setInterval(rain, 50);
</script>
</body>
</html>