-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextualizer.min.js
10 lines (10 loc) · 4.7 KB
/
textualizer.min.js
1
2
3
4
5
6
7
8
9
10
(function(b,j){function x(a){var b,d,f,n={};if(j.getComputedStyle)if(a=j.getComputedStyle(a,null),a.length){d=0;for(f=a.length;d<f;d++)b=a[d].replace(/\-([a-z])/,function(a,b){return b.toUpperCase()}),n[b]=a.getPropertyValue(a[d])}else for(b in a)"function"!==typeof a[b]&&"length"!==b&&(n[b]=a[b]);else for(b in a=a.currentStyle||a.style,a)Object.prototype.hasOwnProperty.call(a,b)&&(n[b]=a[b]);return n}function H(){this.pos=this.domNode=this.character=null;this.visited=this.inserted=this.used=!1}function A(){this.str=
"";this.characterList=[]}var q,I=1E3,J=500,K=2E3,L=2E3,M="textualizer.changed";A.prototype={use:function(a){var c=null;b.each(this.characterList,function(){if(this.character===a&&!this.used)return this.used=!0,c=this,!1});return c},reset:function(){b.each(this.characterList,function(){this.used=this.inserted=!1})}};q=function(a,c,d){function f(a){var c=b.fn.textualizer.effects,f="random"===d.effect?c[Math.floor(Math.random()*(c.length-2))+1][1]:s,e=b.Deferred(),g=[];b.each(a.characterList,function(a,
d){if(!d.inserted){d.domNode.css({left:d.pos.left,top:d.pos.top});var c=b.Deferred();j.setTimeout(function(){f({item:d,container:h,dfd:c})},Math.random()*K);g.push(c)}});b.when.apply(null,g).done(function(){e.resolve()});return e.promise()}function n(){e===t.length-1&&(b.each(u,function(a,b){b.reset()}),e=-1,d.loop||G.pause());e++;q(e)}function q(c){if(!y){var e=b.Deferred(),o=u[c];if(!o){var h=[],g=new A,k,i,r,m;g.str=t[c];u.push(g);k=0;for(m=g.str.length;k<m;k++)i=g.str.charAt(k),""===i?l.append(" "):
(r=new H,r.character=i,r.domNode=b("<span/>").text(i),l.append(r.domNode),h.push(r));var s=d.centered?(z-l.height())/2:0;b.each(h,function(a,b){b.pos=b.domNode.position();b.domNode=b.domNode.clone();b.pos.top+=s;b.domNode.css({left:b.pos.left,top:b.pos.top,position:"absolute"});g.characterList.push(b)});l.html("");o=g}if(v){var h=v,x=o,B=[],C=[],D=b.Deferred(),E,p;k=[function(a){p=b.Deferred();a.animate({top:F.bottom,opacity:"hide"},p.resolve);return p.promise()},function(a){p=b.Deferred();a.fadeOut(1E3,
p.resolve);return p.promise()}];E=k[Math.floor(Math.random()*k.length)];var w;b.each(h.characterList,function(a,d){if(w=x.use(d.character))w.domNode=d.domNode,w.inserted=!0,B.push(w);else{var c=b.Deferred();C.push(c);E(d.domNode.delay(Math.random()*L)).done(function(){d.domNode.remove();c.resolve()})}});b.when.apply(null,C).done(function(){return D.resolve(B)});D.promise().done(function(a){var c=o,g=b.Deferred(),h=[];j.setTimeout(function(){b.each(a,function(a,c){var e=b.Deferred();c.domNode.animate({left:c.pos.left,
top:c.pos.top},d.rearrangeDuration,e.resolve);h.push(e.promise())});b.when.apply(null,h).done(function(){j.setTimeout(function(){f(c).done(function(){g.resolve()})},J)})},I);g.promise().done(function(){e.resolve()})})}else f(o).done(function(){e.resolve()});v=o;e.promise().done(function(){a.trigger(M,{index:c});j.setTimeout(n,d.duration)})}}var G=this,t=[],u,e,v,s=null,i=!1,y=!1,z,F,m,h,l;"random"!==d.effect&&b.each(b.fn.textualizer.effects,function(){if(this[0]===d.effect)return s=this[1],!1});m=
a.clone().removeAttr("id").appendTo(j.document.body);m.css(x(a[0]));m.css({position:"absolute",top:"-1000px"});l=b("<div />").css({position:"relative",visibility:"hidden"}).appendTo(m);a.css("overflow","hidden");h=b("<div />").css("position","relative").appendTo(a);z=a.height();F={bottom:z};this.data=function(a){this.stop();t=a;u=[]};this.stop=function(){this.pause();i=!1;v=null;e=0;h.empty();l.empty()};this.pause=function(){y=!0;i=!1};this.start=function(){0===t.length||i||(e=e||0,i=!0,y=!1,q(e))};
this.destroy=function(){h.parent().removeData("textualizer").end().remove();l.remove()};c&&c instanceof Array&&this.data(c)};b.fn.textualizer=function(){var a=arguments,c,d,f;f=this.data("textualizer");f||(c=[],1===a.length&&a[0]instanceof Array?c=a[0]:1===a.length&&"object"===typeof a[0]?d=a[0]:2===a.length&&(c=a[0],d=a[1]),0===c.length&&this.find("p").each(function(){c.push(b(this).text())}),this.html(""),f=new q(this,c,b.extend({},b.fn.textualizer.defaults,d)),this.data("textualizer",f));d=f;"string"===
typeof a[0]&&d[a[0]]&&d[a[0]].apply(d,Array.prototype.slice.call(a,1));return this};b.fn.textualizer.defaults={effect:"random",duration:2E3,rearrangeDuration:1E3,centered:!1,loop:!0};b.fn.textualizer.effects=[["none",function(a){a.container.append(a.item.domNode.show())}],["fadeIn",function(a){a.container.append(a.item.domNode.fadeIn(2E3,a.dfd.resolve));return a.dfd.promise()}],["slideLeft",function(a){a.item.domNode.appendTo(a.container).css({left:-1E3}).show().animate({left:a.item.pos.left},2E3,
a.dfd.resolve);return a.dfd.promise()}],["slideTop",function(a){a.item.domNode.appendTo(a.container).css({top:-1E3}).show().animate({top:a.item.pos.top},2E3,a.dfd.resolve);return a.dfd.promise()}]]})(jQuery,window);