I coded I simple pulsing binary background effect for my personal website, and I was wanting to get input on the most efficient way to do this (I'm pretty sure my method is very lackluster and not the best way to go about it).
My end goal would be to create something less static and more free flowing. Instead of having a set list of digits to pulse (as I currently have) I would like to have all the digits have a chance to pulse, but looping through 25000 spans
takes a good amount of resources on the client's side and creates some undesirable lag.
Another goal of mine is to possibly make the digits fall like gravity is acting upon them, like a matrix style animation :D
Any information on how to make this possible would be appreciated!
The code and JSFiddle is given: JSFiddle
document.write("<div id='binaryWrapper'>");
for (var i = 0; i < 25000; i++) {
var digit = Math.round(Math.random());
if (Math.random() > 0.03) {
document.write("<span class='digit binaryDigit'>" + digit + "</span>");
} else {
document.write("<span class='digit binaryDigitColored'>" + digit + "</span>");
}
}
document.write("</div>");
var elements = document.getElementsByClassName("binaryDigitColored");
var staticElements = [];
for (var i = 0; i < elements.length; i++) {
staticElements.push(elements[i]);
}
setInterval(function() {
for (var i = 0; i < staticElements.length; i++) {
if (Math.round(Math.random()) > 0.02) {
staticElements[i].setAttribute("class", "binaryDigit");
} else {
var index = Math.round(Math.random() * (staticElements.length - 1));
staticElements[index].removeAttribute("class", "binaryDigit");
staticElements[index].setAttribute("class", "binaryDigitColored");
}
}
}, 500);
#binaryWrapper {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
word-wrap: break-word;
z-index: -1;
background-color: #000000;
}
.binaryDigit {
color: #222222;
transition: color 1s ease-in;
}
.binaryDigitColored {
color:lightgreen;
transition: color 1s ease-out;
}