模仿黑客帝国结果2(canvas试验)

之前看到一个css和js运用的结果练了一下,演示:
此次用canvas玩一下,觉得也还不错,
Github传送门:https://github.com/EchoLsx/hacker
演示地点:http://codepen.io/echo_lsx/pen/ZOaOyZ

<canvas id="c"></canvas>
<!-- 引荐开源CDN来拔取需援用的外部JS //-->
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
 /*Javascript代码片断*/
    var c = document.getElementById("c");
    var ctx = c.getContext("2d");
    
    //全屏
    c.height = window.innerHeight;
c.width = window.innerWidth;

//笔墨
var txts = "101010110101";
//转为数组
txts = txts.split("");

var font_size = 16;
var columns = c.width/font_size; 
//用于盘算输出笔墨时坐标,所以长度即为列数
var drops = [];
//初始值
for(var x = 0; x < columns; x++)
    drops[x] = 1; 

//输出笔墨
function draw()
{
    //让背景逐步由通明到不通明
    ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
    ctx.fillRect(0, 0, c.width, c.height);
    
    ctx.fillStyle = "#0F0"; //笔墨色彩
    ctx.font = font_size + "px arial";
    //逐行输出笔墨
    for(var i = 0; i < drops.length; i++)
    {
        //随机取要输出的笔墨
        var text = txts[Math.floor(Math.random()*txts.length)];
        //输出笔墨,注重坐标的盘算
        ctx.fillText(text, i*font_size, drops[i]*font_size);
        
        //假如绘满一屏或随机数大于0.95(此数可自行调解,结果会差别)
        if(drops[i]*font_size > c.height || Math.random() >0.95)
            drops[i] = 0;
        
        //用于Y轴坐标增添
        drops[i]++;
    }
}

setInterval(draw, 100);
    原文作者:Echo_lsx
    原文地址: https://segmentfault.com/a/1190000005945271
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞