2018-12-10 黑客帝國js隨筆

js黑客帝國效果頁面實現(xiàn)-By羅溫柔

1.先上效果圖:

黑客帝國效果圖

2.具體怎么實現(xiàn)請看代碼:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>羅溫柔的黑客帝國</title>

<style type="text/css">

canvas { display: block;}

</style>

</head>

<body>

<canvas id="canvas"></canvas>

<script>

function $(id) {? return document.getElementById(id);? }?

var canvas = $("canvas");

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

//window對象獲取屏幕的寬度和高度,把寬和高給畫布

var mywindow = window.screen;

canvas.width=mywindow.width;

canvas.height=mywindow.height;

//將字符串轉(zhuǎn)化為數(shù)組

var strs ="0123456789".split("");

//設(shè)置字體的大小

var fontSize=20;

//獲取一行中的所有列數(shù)

var columns=canvas.width/fontSize;

var drops=[];

//將所有的列設(shè)置為第一行

for(var i=0;i<columns;i++){? ? drops[i]=1;? ? }

function draw(){?

ctx.fillStyle="rgba(0,0,0,0.05)";

ctx.fillRect(0,0,canvas.width,canvas.height);//(x坐標(biāo),y坐標(biāo),畫布的寬,畫布的高)

ctx.fillStyle = "#00EE00";

ctx.font=fontSize+"px arial";

for(var i=0;i<columns;i++){

var text=strs[Math.floor(Math.random()*(strs.length))];//畫布中的內(nèi)容,顯示0~9的數(shù)字

ctx.fillText(text,i*fontSize,drops[i]*fontSize);//(內(nèi)容,x坐標(biāo),y坐標(biāo))

if(drops[i]*fontSize>canvas.height || Math.random()>0.95){

? ? //當(dāng)y下落的高度大于畫布的高度或隨機(jī)數(shù)大于0.95時,下落高度為0

? ? drops[i]=0;? }

drops[i]++;

}

};

setInterval(draw,33);

</script>

</body>

</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容