/** * @author Martin Angelov * @license MIT License */ (function($){ $.fn.shuffleLetters = function(prop){ var options = $.extend({ "step" : 12, "fps" : 75, "text" : "", // Use this text instead of the contents "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;ilen){ // 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]]); } else { strCopy[letters[i]] = ""; } } el.text(strCopy.join("")); setTimeout(function(){ shuffle(start+1); },1000/options.fps); })(-options.step); }); }; function randomChar(type){ var pool = ""; if (type == "lowerLetter"){ pool = "0123456789abcdef"; } else if (type == "upperLetter"){ pool = "0123456789ABCDEF"; } else if (type == "symbol"){ pool = "xX"; } var arr = pool.split(''); return arr[Math.floor(Math.random()*arr.length)]; } })(jQuery);