1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| var cvs = document.getElementById("cvs"); var ctx = cvs.getContext("2d"); var cw = cvs.width = document.body.clientWidth; var ch = cvs.height = document.body.clientHeight; var requestAnimationFrame = window.requestAnimationFrame window.mozRequestAnimationFrame window.webkitRequestAnimationFrame window.msRequestAnimationFrame; var codeRainArr = []; var cols = parseInt(cw/14); var step = 16 ; ctx.font = " 14px microsoft yahei"; function createCodeRain() { for (var n = 0; n < cols; n++) { var col = []; var basePos = parseInt(Math.random()*300); var speed = parseInt(Math.random()*10)+3; var colx = parseInt(Math.random()*cw) for (var i = 0; i < parseInt(ch/step)/2; i++) { var code = { x : colx, y : -(step*i)-basePos, speed : speed, text : parseInt(Math.random()*10)%2 == 0 ? 0 : 1 } col.push(code); } codeRainArr.push(col); } } function codeRaining(){ ctx.clearRect(0,0,cw,ch); for (var n = 0; n < codeRainArr.length; n++) { col = codeRainArr[n]; for (var i = 0; i < col.length; i++) { var code = col[i]; if(code.y > ch){ code.y = 0; }else{ code.y += code.speed; } ctx.fillStyle = "hsl("+(parseInt(Math.random()*359)+1)+",30%,"+(50-i*2)+"%)"; ctx.fillText(code.text,code.x,code.y); } } requestAnimationFrame(codeRaining); } createCodeRain(); requestAnimationFrame(codeRaining);
|