Today, my project was to make a matrix in my web browser. It is really slow, so help with optimizing it would be nice. It runs slow on my PC, yet alone my iPod 4 which this is going to be for.
<html>
<head>
<title>Matrix</title>
<style type="text/css">
body {
background-color:black;
background-size: 100%;
}
#mat {
font-size:8px;
font-family:"Courier";
font-weight:bold;
}
</style>
<body onLoad="randomMatrix()">
<div id="mat">
<span style="color:#005400"> hi </span><span>hi</span>
</div>
<script type="text/javascript">
function randomLine()
{
var text = "<span>";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i=0; i < 63; i++ ) text += "</span><span style=\"color:"+randomGreen()+"\">"+possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function randomMatrix() {
document.getElementById("mat").innerHTML = randomLine();
for(var i = 0; i<45;i++) document.getElementById("mat").innerHTML += "<br/>"+randomLine();
sleep(125, randomMatrix);
}
function sleep(millis, callback) {
setTimeout(function()
{ callback(); }
, millis);
}
function randomGreen() {
return "#00"+randInt(0,255).toString(16)+"00";
}
function randInt(min,max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>
</body>
</html>
You can try it out here. ANY speed improvements would greatly help!