本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2013年08月14日 统计字数: 854字 阅读时间: 2分钟阅读 本文链接: https://soulteary.com/2013/08/14/javascript-draw-matrix.html ----- # Javascript使用canvas绘制Matrix [![2013-08-14_023153](https://attachment.soulteary.com/2013/08/14/2013-08-14_023153.png "2013-08-14_023153")](https://attachment.soulteary.com/2013/08/14/2013-08-14_023153.png) 在cssdeck看到了一段简洁有趣的效果,不敢私藏。 ## 正文 > 出处:http://cssdeck.com/labs/the-matrix > 预览地址:http://thecdn.sinaapp.com/page/demo/matrix-canvas/ 首先是创建一个画布,比如使用下面简单的结构。 ```html ``` 脚本代码 ```js var s = window.screen; var width = q.width = s.width; var height = q.height = s.height; var letters = Array(256).join(1).split(''); var draw = function () { q.getContext('2d').fillStyle='rgba(0,0,0,.05)'; q.getContext('2d').fillRect(0,0,width,height); q.getContext('2d').fillStyle='#0F0'; letters.map(function(y_pos, index){ text = String.fromCharCode(3e4+Math.random()*33); x_pos = index * 10; q.getContext('2d').fillText(text, x_pos, y_pos); letters[index] = (y_pos > 758 + Math.random() * 1e4) ? 0 : y_pos + 10; }); }; setInterval(draw, 33); ```