- html代码
<canvas id="c"></canvas>
- js 代码
function onClick(id){
var c,
ctx,
max,
intertime,
width,
height,
chinese,
chineseLen,
drops,
font_size,
columns,
draw;
max = 0.975;intertime = 50;
c = document.getElementById(id);
ctx = c.getContext('2d');
width = window.innerWidth;
height = window.innerHeight;
c.width = width; c.height = height;
chinese = 'qwertyuiopasdfghjklzxcvbnm'.split('');
chineseLen = chinese.length;
drops = []; font_size = 10;
columns = parseInt(width / font_size);
draw = function(){
ctx.fillStyle = "rgba( 0, 0, 0, 0.05)";
ctx.fillRect( 0, 0, width, height);
ctx.fillStyle = '#0F0';
ctx.font = (Math.random() * font_size) + 'px arial';
for(var i=0;i < columns;i++){
drops[i] = drops[i] || 1;
var text = chinese[Math.floor(Math.random() * chineseLen)];
ctx.fillText(text, i * font_size, drops[i] * font_size);
if(drops[i] * font_size > c.height && Math.random() > max){
drops[i] = 1;
}
drops[i] ++;
}
}
setInterval(draw, intertime);
};
setTimeout(function(){onClick('c');}, 100);