69 lines
1.9 KiB
JavaScript
69 lines
1.9 KiB
JavaScript
|
var Messenger = function (el) {
|
||
|
'use strict';
|
||
|
var m = this;
|
||
|
|
||
|
m.init = function () {
|
||
|
m.codeletters = "&#*+%?£@§$";
|
||
|
m.current_length = 0;
|
||
|
m.fadeBuffer = false;
|
||
|
m.message = el.textContent.length > 0 ? el.textContent : ''
|
||
|
|
||
|
setTimeout(m.animateIn, 300);
|
||
|
};
|
||
|
|
||
|
m.generateRandomString = function (length) {
|
||
|
var random_text = '';
|
||
|
while (random_text.length < length) {
|
||
|
random_text += m.codeletters.charAt(Math.floor(Math.random() * m.codeletters.length));
|
||
|
}
|
||
|
|
||
|
return random_text;
|
||
|
};
|
||
|
|
||
|
m.animateIn = function () {
|
||
|
if (m.current_length < m.message.length) {
|
||
|
m.current_length = m.current_length + 2;
|
||
|
if (m.current_length > m.message.length) {
|
||
|
m.current_length = m.message.length;
|
||
|
}
|
||
|
|
||
|
var message = m.generateRandomString(m.current_length);
|
||
|
el.textContent = message;
|
||
|
|
||
|
setTimeout(m.animateIn, 60);
|
||
|
} else {
|
||
|
setTimeout(m.animateFadeBuffer, 60);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
m.animateFadeBuffer = function () {
|
||
|
if (m.fadeBuffer === false) {
|
||
|
m.fadeBuffer = [];
|
||
|
for (var i = 0; i < m.message.length; i++) {
|
||
|
m.fadeBuffer.push({ c: (Math.floor(Math.random() * 12)) + 1, l: m.message.charAt(i) });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var do_cycles = false;
|
||
|
var message = '';
|
||
|
|
||
|
for (var i = 0; i < m.fadeBuffer.length; i++) {
|
||
|
var fader = m.fadeBuffer[i];
|
||
|
if (fader.c > 0) {
|
||
|
do_cycles = true;
|
||
|
fader.c--;
|
||
|
message += m.codeletters.charAt(Math.floor(Math.random() * m.codeletters.length));
|
||
|
} else {
|
||
|
message += fader.l;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
el.textContent = message;
|
||
|
|
||
|
setTimeout(m.animateFadeBuffer, 150);
|
||
|
};
|
||
|
|
||
|
m.init();
|
||
|
}
|
||
|
|
||
|
window.Messenger = Messenger;
|