canvas實現黑客帝國矩形陣

在博客園看到了車大棒的寫了一篇關於實現黑客帝國矩形陣,以爲canvas仍是有一些奇妙的地方所在,故作個筆記記錄一下。javascript

實現的效果以下:css

真的是一兩行關鍵的代碼添加就能實現意想不到的效果。html

因爲是canvas實現的,全部首先第一步就是在頁面添加canvas標籤,以下:java

<canvas id="canvas">請使用高版本瀏覽器,IE8以及一下不支持canvas</canvas>

css代碼:canvas

html,body{height:100%;overflow:hidden}

因爲實現的效果是canvas全屏顯示在瀏覽器中,全部下一步就是把屏幕的寬高賦值給canvas,以下:數組

var width,height,
  canvas = document.getElementById("canvas");
  canvas.width = width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  canvas.height = height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

建立canvas的2D繪圖環境,以下:瀏覽器

var ctx = canvas.getContext('2d');

下面建立一個數組,用一個數組來幹嗎呢,主要是用數組來存放canvas中文字的Y值。不懂的話看完就全明白了。dom

 var num = Math.ceil(width / 10);
 var y = Array(num).join(0).split('');

num的值就是把屏幕的寬度分割成num份,也便是num列,每列的寬度是10px。post

canvas的繪圖代碼:字體

var draw = function() {
        ctx.fillStyle = 'rgba(0,0,0,.05)'; //核心代碼,建立黑色背景,透明度爲0.05的填充色。
        ctx.fillRect(0, 0, width, height);
        ctx.fillStyle = '#0f0'; //設置了字體顏色爲綠色
        ctx.font = '10px Microsoft YaHei';//設置字體大小與family
        for(i = 0; i < num; i++) {
            var x = (i * 10) + 10;
            text = String.fromCharCode(65 + Math.random() * 62);
            var y1 = y[i];
            ctx.fillText(text, x, y1);
            if(y1 > Math.random() * 10 * height) {
                y[i] = 0;
            } else {
                y[i] = parseInt(y[i]) + 10;
            }
        }
    }

代碼中的ctx.fillStyle = 'rgba(0,0,0,.05)',因爲頁面在反覆調用這個draw方法,透明度也在疊加,使裏面的文字顏色也有變化,全部看起來感受有3D的感受,有層次感。

代碼中的for循環的主要做用是設置文本,用到了String.fromCharCode()方法,傳遞的是Unicode值,返回的是字符串,詳細瞭解能夠查看JavaScript系列方法與實例總結(一)

而後就是設置字體的位置,如,第一行的字體位置[10,10],[20,10],[30,10].....;第二行的字體位置[20,20],[30,20],[40,20]....依次推導。

因此代碼中var y = Array(num).join(0).split('');就是保存字體位置的y值,而後經過for循環依次+10,如今估計知道了該數組的做用了 就是更新位置。

最後就是反覆調用該方法:

;(function(){
        setInterval(draw, 100);
    })();

網上實現draw方法使用的map方法,其實同樣,貼代碼:

$(document).ready(function() {
        var s = window.screen;
        var width = q.width = s.width;
        var height = q.height;
        var yPositions = Array(300).join(0).split('');
        var ctx = q.getContext('2d');

        var draw = function() {
            ctx.fillStyle = 'rgba(0,0,0,.05)';
            ctx.fillRect(0, 0, width, height);
            ctx.fillStyle = 'red';
            ctx.font = '10pt Georgia';
            yPositions.map(function(y, index) {
                text = String.fromCharCode(1e2 + Math.random() * 33);
                x = (index * 10) + 10;
                q.getContext('2d').fillText(text, x, y);
                if(y > Math.random() * 1e4) {
                    yPositions[index] = 0;
                } else {
                    yPositions[index] = y + 10;
                }
            });
        };
        RunMatrix();

        function RunMatrix() {
            Game_Interval = setInterval(draw, 1000);

        }
    });

最後,總體代碼:

<canvas id="canvas">請使用高版本瀏覽器,IE8以及一下不支持canvas</canvas>
<script type="text/javascript">
window.onload = function() {
  var width,height,
  canvas = document.getElementById("canvas");
  canvas.width = width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  canvas.height = height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  var ctx = canvas.getContext('2d');
  var num = Math.ceil(width / 10);
  var y = Array(num).join(0).split('');
  var draw = function() {
        ctx.fillStyle = 'rgba(0,0,0,.05)'; //核心代碼,建立黑色背景,透明度爲0.05的填充色。
        ctx.fillRect(0, 0, width, height);
        ctx.fillStyle = '#0f0'; //設置了字體顏色爲綠色
        ctx.font = '10px Microsoft YaHei';//設置字體大小與family
        for(i = 0; i < num; i++) {
            var x = (i * 10) + 10;
            text = String.fromCharCode(65 + Math.random() * 62);
            var y1 = y[i];
            ctx.fillText(text, x, y1);
            if(y1 > Math.random() * 10 * height) {
                y[i] = 0;
            } else {
                y[i] = parseInt(y[i]) + 10;
            }
        }
    }

    ;(function(){
        setInterval(draw, 100);
    })();

}
</script>

參考地址:http://www.cnblogs.com/chedabang/p/6291504.html

相關文章
相關標籤/搜索