-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.cspinner.lite.min.js
19 lines (19 loc) · 3.93 KB
/
jquery.cspinner.lite.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*!
* jQuery.cSpinner, v0.3.0
* https://github.com/mrienstra/jQuery.cSpinner
*
* Copyright 2012, Michael Rienstra
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* A jQuery plugin to create animated loading indicators using the canvas element
*
* Date: Wed Jan 18, 2012
*/
(function(b){var Z="cSpinner",w={init:function(m){var f=this,j=function(){var b=document.createElement("canvas");return b.getContext&&b.getContext("2d")?0===b.toDataURL("image/png").indexOf("data:image/png")?2:1:0}();this.each(function(w){var a={speed:1,scale:1,lineWidth:3,lineCap:"round",innerRadius:8.7,outerRadius:14.42,color:"#000",minimumOpacity:0.18,segments:12,dimmestSegments:1,shadow:!1,shadowOffsetX:1,shadowOffsetY:1,shadowBlur:1,shadowColor:"rgba(10, 10, 10, 0.5)",pixelRatio:1,autoStart:!0,
pauseOnBlur:!0,fallbackSourcesArray:void 0,checkExistsInterval:1E3,preserveExisting:["id","class","style"],drawSegment:function(a,c,b,m,f,g,d,e){a.globalAlpha=c();a.rotate(2*Math.PI/b);a.beginPath();a.moveTo(0,d.innerRadius);a.lineTo(0,e);a.stroke()}},o="<canvas /><canvas />",n,c,t,A="lineWidth,innerRadius,outerRadius,shadowOffsetX,shadowOffsetY,shadowBlur".split(","),g,B,C,h,u,x,r,k,d,D=b(this),y,l,z,p,E,v,i,q=[],s,F,G,e=0;m&&b.extend(a,m);if(0===j){if("undefined"===typeof a.fallbackSourcesArray)return!0;
o=""}n=a.pixelRatio;g=a.segments;1!==n&&(a.scale*=n);for(c=0,t=A.length;c<t;c+=1)a[A[c]]*=a.scale;B=1E3/a.speed/g;C=a.outerRadius;h=C+a.lineWidth;if(a.shadow)u=a.shadowOffsetX,x=a.shadowOffsetY,h+=u>x?u:x,h+=a.shadowBlur;h=Math.ceil(h);r=k=2*h;1!==n&&(k/=n);d=b("<div>"+o+"</div>");D.replaceWith(function(){for(c=0,t=a.preserveExisting.length;c<t;c+=1)y=a.preserveExisting[c],D.attr(y)&&d.attr(y,D.attr(y));return d});o=d[0];d.css({width:""+k+"px",height:""+k+"px",overflow:"hidden","background-size":""+
k+"px "+(""+k)+"px"});n=d.attr("id");""===n?(l=Z+(""+Math.random()).substring(2),d.attr("id",l),l="#"+l):1!==b("#"+n).length?(l=Z+(""+Math.random()).substring(2),d.addClass(l),l="."+l):l="#"+n;if(0!==j){if(1===j)z=b("canvas:eq(0)",o),p=z[0],p.width=r,p.height=r*g,E=p.getContext("2d"),z.css({width:""+k+"px",height:""+k*g+"px"});p=b("canvas:eq(1)",o);v=p[0];p.css("display","none");v.width=v.height=r;i=v.getContext("2d");i.translate(h,h);i.lineWidth=a.lineWidth;i.lineCap=
a.lineCap;i.strokeStyle=a.color;if(a.shadow)i.shadowOffsetX=u,i.shadowOffsetY=x,i.shadowBlur=a.shadowBlur,i.shadowColor=a.shadowColor}else b.ajax({type:"GET",url:a.fallbackSourcesArray,dataType:"json",success:function(a){q=a;for(c=0,t=q.length;c<t;c+=1)b("<img />").attr("src",q[c])}});2===j&&b("canvas:eq(0)",o).remove();s=a.minimumOpacity;0>=s&&(s=1.0E-4);F=Math.round(a.checkExistsInterval/B);G=function(){var b=(g+c-e)%g;return b>=a.dimmestSegments-1?(b=b-a.dimmestSegments+1,b*(1-s)/(g-a.dimmestSegments)+
s):s};u=function(){i.clearRect(-h,-h,r,r);for(c=0;c<g;c+=1)a.drawSegment(i,G,g,c,e,s,a,C);2===j?(q.push(v.toDataURL("image/png")),b("<img />").attr("src",q[q.length-1])):E.drawImage(v,0,e*r);e+=1};if(0!==j){for(;e<g;)u();e=0;p.remove()}d.data(Z,{startAnimating:function(){var a=setInterval(function(){1===j?z.css("margin-top",""+-1*e%g*k+"px"):"undefined"!==typeof q[e%g]&&d.css({"background-image":"url("+q[e%g]+")"});0===e%F&&1!==b(l).length&&clearInterval(a);e+=1},B);return a}});
!0===a.autoStart&&d.cSpinner("start");!0===a.pauseOnBlur&&b(window).bind("blur.cSpinner",function(){d.cSpinner("stop")}).bind("focus.cSpinner",function(){d.cSpinner("start")});o!==this&&(f[w]=o)});return f},start:function(){return this.each(function(){var m=b(this),f=m.data(Z),j;f&&f.startAnimating&&!f.intervalID&&(j=f.startAnimating(),m.data(Z,{startAnimating:f.startAnimating,intervalID:j}))})},stop:function(){return this.each(function(){var m=b(this),f=m.data(Z);f&&f.intervalID&&
(clearInterval(f.intervalID),m.data(Z,{startAnimating:f.startAnimating}))})}};b.fn.cSpinner=function(b){if(w[b])return w[b].apply(this,Array.prototype.slice.call(arguments,1));if("object"===typeof b||!b)return w.init.apply(this,arguments)}})(jQuery);