-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordsChanger.js
121 lines (97 loc) · 3.83 KB
/
wordsChanger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*!
* wordsChanger - v0.1 - 2015-01-11
* https://github.com/darkvovich/wordsChanger
* Licensed MIT (https://github.com/darkvovich/wordsChanger/blob/master/LICENSE)
*/
(function ($) {
$.fn.wordsChanger = function(options) {
// default settings
var settings = $.extend({
chars : 'абвгдежзийклмнопрстуфхцчшщьыъэюя',
interval : 4000
}, options);
function getRandStr(len) {
var alphabet = settings.chars,
i,
str = '',
alp_len = alphabet.length;
for (i = 0; i < len; i += 1) {
str += alphabet[Math.round(Math.random() * (alp_len - 1))];
}
return str;
}
function replaceAt(str, index, character) {
return str.substr(0, index) + character + str.substr(index+character.length);
}
function trimWord(word) {
return word.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function changeWord(el, alphabet, freq, newWord, anim) {
var chars = [],
interval,
clone = el.parentNode.querySelector('.js-word-changer-clone'),
updWord = function() {
var rand = getRandStr(newWord.length, alphabet),
addChar = (freq - Math.random()) > 0,
resStr, char;
if (addChar) {
char = Math.round(Math.random() * (newWord.length - 1));
while(chars.indexOf(char) !== -1) {
char = Math.round(Math.random() * (newWord.length - 1));
}
chars.push(char);
}
for (var i = 0; i < chars.length; i ++) {
rand = replaceAt(rand, chars[i], newWord[chars[i]]);
}
el.textContent = rand;
if (chars.length === newWord.length) {
clearInterval(interval);
}
};
if (clone === null) {
clone = el.cloneNode();
el.parentNode.insertBefore(clone, el.nextSibling);
clone.classList.add('js-word-changer-clone');
clone.style.position = 'absolute';
clone.style.left = '-9999px';
}
alphabet = alphabet.split('');
//set final width for element
clone.innerHTML = newWord;
el.style.width = clone.offsetWidth + 'px';
if (anim) {
interval = setInterval(updWord, 30);
} else {
el.innerHTML = newWord;
}
}
window.changeWord = changeWord;
// each wordsChanger
return this.each(function() {
var wordsParsed = $(this).attr('data-words'),
word = $(this).find('i'),
alphabet = settings.chars;
if (wordsParsed) {
var wordsList = wordsParsed.split(','),
wordsLength = wordsList.length,
word_i = 1,
prev_t = 0;
function changeLoop(t) {
requestAnimationFrame(changeLoop);
if (t - prev_t < 3000) {
return;
}
prev_t = t;
if (word_i >= wordsLength) {
word_i = 0;
}
changeWord(word[0], alphabet, 0.7, trimWord(wordsList[word_i]), true);
word_i ++;
}
changeWord(word[0], alphabet, 0, trimWord(wordsList[0]), false);
requestAnimationFrame(changeLoop);
}
});
};
}(jQuery));