Skip to content

Instantly share code, notes, and snippets.

@Go7hic
Last active August 5, 2018 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Go7hic/169685f6d66b3bc10f4e to your computer and use it in GitHub Desktop.
Save Go7hic/169685f6d66b3bc10f4e to your computer and use it in GitHub Desktop.
canvas做的背景
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>背景</title>
<style>
* {
margin: 0;
padding: 0;
}
body {background: black;}
canvas {display: block;}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
var c = document.getElementById("c");
var ctx = c.getContext("2d");
// 全屏展示
c.height = window.innerHeight;
c.width = window.innerWidth;
var chinese = "田由甲申甴电甶男甸甹町画甼甽甾甿畀畁畂畃畄畅畆畇畈畉畊畋界畍畎畏畐畑";
chinese = chinese.split("")
var fontSize = 10;
var columns = c.width/fontSize;
var drops = [];
for(var x = 0;x < columns;x++)
drops[x] = 1;
//下落背景
function draw() {
ctx.fillStyle = "rgba(0,0,0,0.5)";
ctx.fillRect(0,0,c.width,c.height);
ctx.fillStyle = "#0f0"; // 绿色的文字
ctx.font = fontSize + "px arial";
for(var i = 0;i < drops.length;i++) {
var text = chinese[Math.floor(Math.random()*chinese.length)];
ctx.fillText(text,i*fontSize,drops[i]*fontSize);
if(drops[i]*fontSize > c.height && Math.random() > 0.975)
drops[i] = 0;
drops[i]++;
}
}
setInterval(draw,33);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment