HTML 纯js代码,彩色数据瀑布流

HTML js 仿制黑客帝国瀑布流数据,献上

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>黑客帝国瀑布流数据</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--s css-->
<style lang="css">
* {
margin: 0;
padding: 0;
}

html,
body {
height: 100vh;
width: 100%;
}

body {
position: relative;
}

canvas {
display: block;
z-index: -1;
position: absolute;
top: 0;
left: 0;
}
</style>
<!--e css-->
</head>

<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var string1 = "01010101010110100110101010101001110";
string1.split("");
var fontsize = 19;
columns = canvas.width / fontsize;
var drop = [];
for (var x = 0; x < columns; x++) {
drop[x] = 0;
}

function drap() {
ctx.fillStyle = "rgba(0,0,0,0.07)";
ctx.fillRect(0, 0, canvas.width, canvas.height); //fillRect(x,y,width,height),x坐标、y坐标、width宽、height高
ctx.fillStyle =getColor(); //"#0F0";
ctx.font = fontsize + "px arial";
for (var i = 0; i < drop.length; i++) {
var text1 = string1[Math.floor(Math.random() * string1.length)];
ctx.fillText(text1, i * fontsize, drop[i] * fontsize);
//fillText(text,x,y,maxWidth),text画布文本、开始绘制文本的 x 坐标位置、开始绘制文本的 y 坐标位置、可选。允许的最大文本宽度,以像素计
drop[i]++;
if (drop[i] * fontsize > canvas.height && Math.random() > 0.9) { //90%的几率掉落
drop[i] = 0;
}
}
}
setInterval(drap, 30);
function getColor(){
return "#"+(function(color){
return new Array(7 - color.length).join(0) + color;
})((Math.random() * 0x1000000 | 0).toString(16));
}
</script>
</body>

</html>

 


我们的征途是星辰大海