2 * @author Martin Angelov
8 $.fn.shuffleLetters = function(prop){
10 var options = $.extend({
13 "text" : "", // Use this text instead of the contents
14 "callback" : function(){} // Run once the animation is complete
17 return this.each(function(){
23 // Preventing parallel animations using a flag;
25 if(el.data('animated')){
29 el.data('animated',true);
33 str = options.text.split('');
36 str = el.text().split('');
39 // The types array holds the type for each character;
40 // Letters holds the positions of non-space characters;
45 // Looping through all the chars of the string
47 for(var i=0;i<str.length;i++){
55 else if(/[a-z]/.test(ch)){
56 types[i] = "lowerLetter";
58 else if(/[A-Z]/.test(ch)){
59 types[i] = "upperLetter";
70 // Self executing named function expression:
72 (function shuffle(start){
74 // This code is run options.fps times per second
75 // and updates the contents of the page element
79 strCopy = str.slice(0); // Fresh copy of the string
83 // The animation is complete. Updating the
84 // flag and triggering the callback;
86 el.data('animated',false);
91 // All the work gets done here
92 for(i=Math.max(start,0); i < len; i++){
94 // The start argument and options.step limit
95 // the characters we will be working on at once
97 if( i < start+options.step){
98 // Generate a random character at thsi position
99 strCopy[letters[i]] = randomChar(types[letters[i]]);
102 strCopy[letters[i]] = "";
106 el.text(strCopy.join(""));
108 setTimeout(function(){
120 function randomChar(type){
123 if (type == "lowerLetter"){
124 pool = "0123456789abcdef";
126 else if (type == "upperLetter"){
127 pool = "0123456789ABCDEF";
129 else if (type == "symbol"){
133 var arr = pool.split('');
134 return arr[Math.floor(Math.random()*arr.length)];