forked from ngari/jquery.shuffleLetters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.shuffleLetters.js
163 lines (121 loc) · 3.61 KB
/
jquery.shuffleLetters.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
/**
* @name Shuffle Letters
* @author Martin Angelov
* @version 1.1
* @url http://tutorialzine.com/2011/09/shuffle-letters-effect-jquery/
* @license MIT License
*/
(function($){
$.fn.shuffleLetters = function(prop){
var options = $.extend({
"step" : 8, // How many times should the letters be changed
"fps" : 25, // Frames Per Second
"text" : "", // Use this text instead of the contents
"pool" : "", // Random character pool override
"callback" : function(){} // Run once the animation is complete
},prop)
return this.each(function(){
var el = $(this),
str = "";
// Preventing parallel animations using a flag;
if(el.data('animated')){
return true;
}
el.data('animated',true);
if(options.text) {
str = options.text.split('');
}
else {
str = el.text().split('');
}
// The types array holds the type for each character;
// Letters holds the positions of non-space characters;
var types = [],
letters = [];
// Looping through all the chars of the string
for(var i=0;i<str.length;i++){
var ch = str[i];
if(ch == " "){
types[i] = "space";
continue;
}
else if(/[a-z]/.test(ch)){
types[i] = "lowerLetter";
}
else if(/[A-Z]/.test(ch)){
types[i] = "upperLetter";
}
else if(/[0-9]/){
types[i] = "numeric";
}
else {
types[i] = "symbol";
}
letters.push(i);
}
el.html("");
// Self executing named function expression:
(function shuffle(start){
// This code is run options.fps times per second
// and updates the contents of the page element
var i,
len = letters.length,
strCopy = str.slice(0); // Fresh copy of the string
if(start>len){
// The animation is complete. Updating the
// flag and triggering the callback;
el.data('animated',false);
options.callback(el);
return;
}
// All the work gets done here
for(i=Math.max(start,0); i < len; i++){
// The start argument and options.step limit
// the characters we will be working on at once
if( i < start+options.step){
// Generate a random character at thsi position
strCopy[letters[i]] = randomChar(types[letters[i]], options.pool);
}
else {
strCopy[letters[i]] = "";
}
}
el.text(strCopy.join(""));
setTimeout(function(){
shuffle(start+1);
},1000/options.fps);
})(-options.step);
});
};
function randomChar(type, pool){
var pool = pool || "";
// A pool has not been passed
if (!pool.length) {
var pools = {
"lowerLetter": "abcdefghijklmnopqrstuvwxyz0123456789",
"upperLetter": "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"symbol": ",.?/\\(^)![]{}*&^%$#'\"",
"numeric": "0123456789",
all: function() {
return lowerLetter + upperLetter + symbol + numeric;
}
}
if (type == "lowerLetter"){
pool = pools.lowerLetter;
}
else if (type == "upperLetter"){
pool = pools.upperLetter;
}
else if (type == "symbol"){
pool = pools.symbol;
}
else if (type == "numeric"){
pool = pools.numeric
} else {
pool = polls.all();
}
}
var arr = pool.split('');
return arr[Math.floor(Math.random()*arr.length)];
}
})(jQuery);