-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomize_text.js
144 lines (120 loc) · 3.84 KB
/
randomize_text.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
(function($) {
$.fn.randomizeText = function(options) {
singleRandomText = function(v, settings) {
parseTextToArray = function(text, cleanSpaces) {
if(cleanSpaces) {
text = text.replace(' ', '');
}
return text.split('');
}
renderText = function(renderArray, el, targetText, refreshRate) {
var a = [];
$.each(renderArray, function(k, v){
a.push(v.getLetter());
});
var t = a.join('');
$(el[0]).html(t);
if (targetText != t) {
setTimeout(function() {
renderText(renderArray, el, targetText, refreshRate);
}, refreshRate);
}
}
addLetterToText = function(textArray, renderArray, settings) {
var l = textArray.slice(renderArray.length, renderArray.length+1);
var lo = new randomLetter(l[0], settings);
renderArray.push(lo);
if (renderArray.length < textArray.length) {
setTimeout(function() {
addLetterToText(textArray, renderArray, settings)
}, settings.timePerLetter);
}
}
randomLetter = function(l, settings) {
this.letter = l;
this.settings = settings;
this.currentLetter = "";
this.currentRandomTry = 1;
var letter = this;
this.getLetter = function() {
return this.currentLetter;
};
setTimeout(function() {
generateRandomLetter(letter);
}, settings.randomTime);
}
generateRandomLetter = function(letter) {
var i = parseInt(Math.random()*letter.settings.randomLetters.length);
letter.currentLetter = letter.settings.randomLetters[i];
if(letter.maxRandomTries != 0) {
letter.currentRandomTry++;
if(letter.currentRandomTry == letter.maxRandomTries) {
letter.currentLetter = letter.letter;
}
}
if(letter.letter != letter.currentLetter) {
setTimeout(function() {
generateRandomLetter(letter);
}, settings.randomTime);
}
}
init = function(v, settings) {
var renderArray = [],
textArray = [],
el = $(v),
text = '';
// get text
if(settings.text.length == 0) {
if(el.html().length == 0) {
text = "you need to add a text by adding {text:'my text'} as argument.";
} else {
text = el.html();
el.html('');
}
} else {
text = settings.text;
}
textArray = parseTextToArray(text, false);
maxRandomValue = textArray.length;
// create randomLetters
if(settings.randomLetters.length == 0) {
settings.randomLetters = parseTextToArray(text, false);
} else {
settings.randomLetters = parseTextToArray(settings.randomLetters, false);
}
// start gameloop
setTimeout(function() {
renderText(renderArray, el, text, settings.refreshRate);
}, settings.refreshRate);
setTimeout(function() {
addLetterToText(textArray, renderArray, settings);
}, settings.timePerLetter);
}
init(v, settings);
};
// return false if no dom is present
if(this.length == 0) {
console.log('Randomize Text: No document object found.');
return
}
// options and defaults
var settings = $.extend(
$.fn.randomizeText.defaults,
options
);
// iterate through elements
$.each(this, function(k,v) {
var s = jQuery.extend(true, {}, settings);
new singleRandomText(v, s);
});
}
// Plugin defaults – added as a property on our plugin function.
$.fn.randomizeText.defaults = {
text: '',
refreshRate: 50,
timePerLetter: 50,
randomTime: 50,
maxRandomTries: 10,
randomLetters: ''
};
}(jQuery));