Skip to content

Instantly share code, notes, and snippets.

@smigniot
Created February 28, 2023 21:40
Show Gist options
  • Save smigniot/a13c317dd999008f69b04335cf6ae5ec to your computer and use it in GitHub Desktop.
Save smigniot/a13c317dd999008f69b04335cf6ae5ec to your computer and use it in GitHub Desktop.
A Matrix rain effect
function _(tagname, attributes) {
var result = null;
if(attributes && attributes.xmlns) {
result = document.createElementNS(attributes.xmlns, tagname);
} else {
result = document.createElement(tagname || "div");
}
if(attributes) {
Object.keys(attributes).forEach(function(name) {
if("xmlns" == name) return;
var value = attributes[name];
result.setAttribute(name, value);
});
}
return result;
}
function startMatrix() {
var chinese = (function() {
var f=" 0123456789";
for(var i=0;i<45;i++) {
f+=String.fromCharCode(i+65393);
}
return f.split("");
})();
var c = _("canvas",{"width":window.innerWidth,
"height":window.innerHeight,"style":(
"position:fixed;top:0;left:0;z-index:-255;"
)});
var ctx = c.getContext("2d");
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";
ctx.font = font_size + "px monospace";
for(var i=0; i<drops.length; i++) {
var text = chinese[Math.floor(Math.random()*chinese.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]++;
}
}
document.body.appendChild(c);
setInterval(draw,33);
}
startMatrix();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment