-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwordTypeErase.js
297 lines (219 loc) · 7.49 KB
/
wordTypeErase.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// ===================
// wordTypeErase.js
// by Ben Lodge
// github.com/benrlodge
// ===================
'use strict';
$.fn.wordTypeErase = function(options) {
var settings = $.extend({
delayOfStart: 1200, // delay before typing begins
letterSpeed: 125, // delay between letters typed
highlightSpeed: 22, //
delayOfWords: 1400, // delay between words being typed
destination: this, // optional destination for text
naturalTypingSpeed: true, // randomizes the delay between letters to simulate a more natural typing
loop: false, // whether the animation should be restarted at the end
delayBetweenLoop: 1200, // delay between loop
}, options);
var firstHighlight = true;
var words = '';
var firstWord = '';
var lastWord = '';
// Clear timeouts if someone navigates
// away opening a new browser tab
var timeouts = [];
// Use tab change function to determine
// when someone navigates away, and if so
// clear the timeouts
TabChange(function(){
for (var i = 0; i < timeouts.length; i++){
clearTimeout(timeouts[i]);
}
console.log('Restart timers')
// When visitor returns, set word to last in the list
$(settings.destination).html(lastWord);
})
// The reverse highlighting of each letter
var highlight = function(wordLength){
//get length of string working with
var i = 0;
(function iterator() {
var target = wordLength - i;
var nextChar = "[data-pos='"+target+"']";
$(settings.destination).find(nextChar).addClass('highlight');
// Loop through letters adding highlight class
if(i++ < wordLength) {
timeouts.push(setTimeout(iterator, settings.highlightSpeed));
}
if(i == wordLength && firstHighlight){
firstHighlight = false;
// Kick off rest of words in data attribute
getOnWithTheWords();
}
})();
};
// Here we type out each individual word, one letter at a time
var wordType = function(word, iteration, iterations, randArr){
var wordLength = word.length;
var i = 0;
var l = wordLength;
(function iterator() {
var letter = word[i];
$(settings.destination).append("<span data-pos='"+i+"'>"+letter+"</span>");
// Loop through letters typing out
if(++i < l) {
timeouts.push(setTimeout(iterator, randArr[i]));
}
// After all letters are typed out, delay a bit, then highlight them
if(i == l){
timeouts.push(setTimeout(function(){
if(iteration === iterations){
return false;
}
highlight(wordLength, iteration, iterations);
}, 1300));
}
})();
};
// Here we get the the length of each word (number of letters)
// figure out the delays based on how long each word is
// And then start the iterator which will call wordType()
var wordTypePrep = function(){
$(settings.destination).empty();
var i = 0;
var l = words.length;
var interations = l - 1;
var randArr = [];
// iterate through each word from the list
(function iterator() {
//clear the text area
$(settings.destination).empty();
//Add a space after the word
var word = words[i].trim();
// create random speeds for letters
randArr = [];
var randSum = 0;
for(var g = 0; g < word.length; g++){
var rand = Math.floor( Math.random() * 40 );
var randType = Math.round(Math.random());
if(randType){
randArr.push(settings.letterSpeed - rand);
randSum += settings.letterSpeed - rand;
}
else{
randArr.push(settings.letterSpeed + rand);
randSum += settings.letterSpeed + rand;
}
}
//start the typing with wordType()
var delay = (randSum) + (settings.letterSpeed * (word.length));
wordType(word, i, interations, randArr);
//loop until all words are typed
if(++i < l) {
// Set timeout for new words by how long it will take to type out and remove words
// Note - There is an issue here - the delay is tied to the length of the
// phrase - so very long phrases have very long delays but I'd rather they not.
// If anyone has a solution I'm open for pull requests or recommendations on Github issues
timeouts.push(setTimeout(iterator, delay + settings.delayOfWords));
}
// Loop
if (i == l && settings.loop) {
timeouts.push(setTimeout(function() {
if (words[0] != firstWord) {
words.unshift(firstWord);
}
firstHighlight = true;
highlight(words[words.length-1].length);
}, delay + settings.delayBetweenLoop));
}
})();
};
// Turn first word in html as spans needed for highlight
var firstWordSpanIze = function(firstWord){
var firstWordSpanned = '';
for(var i = 0; i<firstWord.length; i++){
firstWordSpanned += "<span data-pos='"+i+"'>"+firstWord[i]+"</span>";
}
return firstWordSpanned;
};
// Kick off data attribute words
var getOnWithTheWords = function(){
timeouts.push(setTimeout(function(){
$(settings.destination).empty();
wordTypePrep(words);
}, 800));
};
// plugin return
return this.each(function() {
var scope = this;
// Get comma separated word list from data attribute.
words = $(this).data('type-words').split(',');
// Set first and last words
firstWord = ($(scope).text());
lastWord = (words[words.length-1]).trim()
var firstWordLength = firstWord.length;
// Turn first word in html as spans needed for highlight
var fwSpan = firstWordSpanIze(firstWord);
$(this).html(fwSpan);
// Highlight first word
timeouts.push(setTimeout(function(){
highlight(firstWordLength);
}, settings.delayOfStart));
});
};
function TabChange(callback){
// Get Browser Prefix
var prefix = getBrowserPrefix();
var hidden = hiddenProperty(prefix);
var visibilityState = visibilityState(prefix);
var visibilityEvent = visibilityEvent(prefix);
// Get Browser-Specifc Prefix
function getBrowserPrefix() {
// Check for the unprefixed property.
if ('hidden' in document) {
return null;
}
// All the possible prefixes.
var browserPrefixes = ['moz', 'ms', 'o', 'webkit'];
for (var i = 0; i < browserPrefixes.length; i++) {
var prefix = browserPrefixes[i] + 'Hidden';
if (prefix in document) {
return browserPrefixes[i];
}
}
// The API is not supported in browser.
return null;
}
// Get Browser Specific Hidden Property
function hiddenProperty(prefix) {
if (prefix) {
return prefix + 'Hidden';
} else {
return 'hidden';
}
}
// Get Browser Specific Visibility State
function visibilityState(prefix) {
if (prefix) {
return prefix + 'VisibilityState';
} else {
return 'visibilityState';
}
}
// Get Browser Specific Event
function visibilityEvent(prefix) {
if (prefix) {
return prefix + 'visibilitychange';
} else {
return 'visibilitychange';
}
}
// Visibility Change
document.addEventListener(visibilityEvent, function(event) {
if (document[hidden]) {
if (callback && typeof(callback == 'function')){
callback();
}
}
});
}