billboard/billboard.js

55 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-07-04 23:24:29 +00:00
function genRandomText(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .?-!(),\"'";
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function genRandomInt(min, max) {
// inclusive min & max
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function shiftArrayToRight(arr, places) {
for (var i = 0; i < places; i++) {
arr.unshift(arr.pop());
}
}
function textFillsScreen() {
var root = document.compatMode == 'BackCompat' ? document.body : document.documentElement;
return root.scrollHeight > root.clientHeight;
}
function fillScreenWithWhitespace() {
var text = document.getElementById("text");
var charArray = ["&nbsp"];
var whitespace = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
while(!textFillsScreen()) {
text.innerHTML += whitespace;
charArray.push.apply(charArray, ["&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;",
"&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;"]);
}
for (var i = 0; i < 26; i++) {
text.innerHTML += whitespace;
charArray.push.apply(charArray, ["&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;",
"&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;"]);
}
return charArray;
}
window.onload = function () {
var textElem = document.getElementById('text');
var charArray = fillScreenWithWhitespace();
window.setInterval(function () {
for (var i = 0; i < genRandomInt(1, 4); i++) {
charArray[genRandomInt(0, charArray.length - 1)] = genRandomText(1);
textElem.innerHTML = charArray.join("");
}
shiftArrayToRight(charArray, 1);
}, 25)
}