Skip to content

Instantly share code, notes, and snippets.

@AVGP
Created January 30, 2012 00:25
Show Gist options
  • Save AVGP/1701563 to your computer and use it in GitHub Desktop.
Save AVGP/1701563 to your computer and use it in GitHub Desktop.
matrix.js allows easy creation of an animated matrix-style background with JavaScript and CSS.
div.matrix {
color: #060;
position: absolute;
font-family: mono-space, sans-serif;
font-size: 10px;
top: 5px;
left: 10px;
z-index: 2;
width: 99%;
overflow: hidden;
}
$(document).ready(function(){
var numLines = document.documentElement.clientHeight / 10;
var numChars = document.documentElement.clientWidth / 5;
for(var n=0;n<numLines;n++) {
var line = "<div class=\"matrix\" style=\"top:"+(5+n*10)+"px\">";
for(var i=0;i<numChars;i++) line += "<span id=\"matrix_"+n+"_"+i+"\">"+Math.round(Math.random())+"</span>";
line += "</div>";
$("body").prepend(line);
}
var last_pos = [];
setInterval(function(){
var row = Math.round(Math.random()*(numLines-1));
var col = Math.round(Math.random()*(numChars-1));
if(last_pos.length >= 50) {
$("#matrix_"+last_pos[0].row+"_"+last_pos[0].col)
.css('color','#060');
last_pos = last_pos.slice(1);
}
$("#matrix_"+row+"_"+col)
.text(Math.round(Math.random()))
.css('color','#0f0');
last_pos.push({'row':row, 'col' :col});
}, 100);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment