Skip to content

Instantly share code, notes, and snippets.

@Lotuashvili
Created June 14, 2014 20:50
Show Gist options
  • Save Lotuashvili/c55a5c7cca7801df4374 to your computer and use it in GitHub Desktop.
Save Lotuashvili/c55a5c7cca7801df4374 to your computer and use it in GitHub Desktop.
Matrix animation in Web with Canvas
<!DOCTYPE html>
<html>
<head>
<title>Matrix - GWSMaster</title>
<meta charset="utf-8">
<style type="text/css">
* {margin: 0; padding: 0;}
body {background: black;}
canvas {display: block;}
</style>
</head>
<body>
<canvas id="matrix"></canvas>
<script type="text/javascript">
var c = document.getElementById("matrix");
var ctx = c.getContext("2d");
c.height = window.innerHeight;
c.width = window.innerWidth;
var content = "0101010101010101010101010101";
content = content.split("");
var font_size = 10;
var columns = c.width/font_size;
var drops = [];
for(var x = 0; x < columns; x++)
drops[x] = 1;
function draw()
{
ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = "#0F0"; //green text
ctx.font = font_size + "px arial";
for(var i = 0; i < drops.length; i++)
{
var text = content[Math.floor(Math.random()*content.length)];
ctx.fillText(text, i*font_size, drops[i]*font_size);
if(drops[i]*font_size > c.height && Math.random() > 0.975)
drops[i] = 0;
drops[i]++;
}
}
setInterval(draw, 33);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment