This repository has been archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.spritemation.min.js
4 lines (4 loc) · 5.27 KB
/
jquery.spritemation.min.js
1
2
3
4
/*! Spritemation - CSS sprite animator - v0.1.0 - 2012-08-18
* https://github.com/Zenovations/spritemation
* Copyright (c) 2012 Michael 'Kato' Wulf; Licensed MIT, GPL */
(function(a){function b(a){this.first=Math.min(a.start,a.end),this.last=Math.max(a.start,a.end),this.frames=this.last-this.first+1,this.modulate=!!a.modulate,this.maxSteps=this.frames*a.cycles,this.direction=a.start>a.end?-1:1,this.curStep=-1,this.curFrame=this.direction<0?this.last:this.first}function c(a,b,c){return function(d){a.css("background-position",b),d(),c&&c(a.queue("spritemation").length<1)}}function d(b,c,d){return a.Deferred(function(f){function k(a){j.vertical=a,j.frameWidth||(j.frameWidth=d.innerWidth()),j.frameHeight||(j.frameHeight=d.innerHeight()),j.start===i&&(j.start=h(d,j.frameHeight,j.frameWidth,j.vertical));var b=Math.abs(j.end-j.start)+1;if(j.duration){var e=b*j.cycles;j.modulate&&(e*=2),j.fps=Math.round(e/c*1e3)}g||d.data("spritemation",{frameHeight:j.frameHeight,frameWidth:j.frameWidth,vertical:j.vertical,clearQueue:!1}),j.cache=g,f.resolve(j)}var g=d.data("spritemation");b&&typeof b!="object"?b={end:~~b}:b||(b={}),!(c in b)&&typeof c in{number:1,string:1}&&(b.duration=~~c);var i,j=a.extend({start:i,end:0,frameHeight:i,frameWidth:i,vertical:i,fps:30,cycles:1,cycleType:"restart"},g,b);"vertical"in b?k(b.vertical):e(d).then(k)})}function e(b){return a.Deferred(function(c){var d=b.css("background-image");a("<img />").hide().appendTo("body").on("load",function(){c.resolve(a(this).height()>a(this).width()),a(this).remove()}).attr("src",d.substr(5,d.length-7))})}function f(a,b){var c=a.vertical?0:g(a,b,!1),d=a.vertical?g(a,b,!0)+"px":0;return[c?c+"px":0,d?d+"px":0].join(" ")}function g(a,b,c){return c?a.frameHeight*-b:a.frameWidth*-b}function h(b,c,d,e){var f=b.css("background-position"),g=e?c:d,h=e?2:1,i=f&&f.match(/^(-?[0-9]+(?:px|em|%)?) ?(-?[0-9]+(?:px|em|%)?)?$/);return i?Math.floor(Math.abs(a.UnitConverter.px(b,i[h])/g)):0}function i(a,b,c){return b>c&&(b=b^c,c=b^c,b=b^c),Math.max(b,Math.min(c,a))}a.fn.spritemation=function(e,g){return this.each(function(){d(e,g,a(this)).done(a.proxy(function(d){var e=a(this).stop("spritemation",!0),g=new b(d);if(d.duration===0||d.start==d.end)e.css("background-position",f(d,d.end)),d.callback&&d.callback(d.end);else{var h,i=1e3/d.fps,j;d.callback&&(j=function(a){a&&d.callback&&d.callback(h)});while(g.hasNext())h=g.next(),e.queue("spritemation",c(e,f(d,h),j)).delay(i,"spritemation");e.dequeue("spritemation")}},this))})},b.prototype.next=function(){if(this.hasNext()){this.curStep++;if(this.modulate){if(this.direction>0&&this.curFrame===this.last||this.direction<0&&this.curFrame===this.first)this.direction=-this.direction;this.curFrame=this.curFrame+this.direction}else{var a=this.curStep%this.frames;this.direction<0?this.curFrame=this.last-a:this.curFrame=this.first+a}return this.curFrame}return typeof console=="object"&&console.warn&&console.warn("CycleIterator.next() called after last element; that was probably a mistake"),!1},b.prototype.hasNext=function(){return this.curStep<this.maxSteps-1},b.prototype.reset=function(){this.curStep=-1,this.curFrame=this.direction<0?this.last:this.first}})(jQuery),function(){function a(a,d,e){this.pxHeight=typeof a=="number"?a:c($(a)),typeof d=="undefined"?b(this,0):b(this,d,e)}function b(a,b,c){var f=typeof b;if(f==="string"){if(b.match(/(smallest|small|medium|large|largest)/))throw new Error("I don't know what to do with smallest/small/medium/large/largest yet :(");b.match(/[^0-9.-]/)?(c=e(b),b=d(b)):b===""?b=0:b=parseFloat(b)}else if(f!=="number")throw new Error("not a valid number",b);a.amt=b,a.units=c?c:"px"}function c(a){var b,c=$('<div style="display: none; font-size: 1em; margin: 0; padding:0; height: auto; line-height: 1; border:0;"> </div>').appendTo(a);return b=c.height(),c.remove(),b}function d(a){return parseFloat(a.replace(/[^0-9.-]/,""))}function e(a){if(a.match(/(em|px|%)$/))return a.replace(/.*(em|px|%)$/,"$1");throw new Error("Unable to determine units from "+a)}function f(a,b){var c=a.amt,d=a.pxHeight;if(c===0)return 0;switch(b){case"px":switch(a.units){case"px":return c;case"em":return h(d,c);case"%":return i(d,c);default:throw Error("I don't know what type \""+a.units+'" is')};case"em":switch(a.units){case"px":return g(d,c);case"em":return c;case"%":return j(d,c);default:throw Error("I don't know what type \""+a.units+'" is')};case"%":switch(a.units){case"px":return k(d,c);case"em":return l(d,c);case"%":return c;default:throw Error("I don't know what type \""+a.units+'" is')};default:throw Error("I don't know what type \""+a.units+'" is')}}function g(a,b){return b>0?m(b/a,3):0}function h(a,b){return b>0?m(b*a,3):0}function i(a,b){return m(a*b/100,3)}function j(a,b){return g(a,a*b/100)}function k(a,b){return m(b/a*100)}function l(a,b){return k(a,h(a,b))}function m(a,b){return b||(b=0),Math.round(a*Math.pow(10,b))/Math.pow(10,b)}a.prototype.clone=function(b){return new a(b||this.pxHeight,this.amt,this.units)},a.prototype.load=function(a,c){return b(this,a,c),this},a.prototype.add=function(b){return b instanceof a||(b=this.clone().load(b)),this.amt+=b.convert(this.units),this},a.prototype.convert=function(a){return f(this,a)},a.prototype.px=function(){return this.convert("px")},a.prototype.toString=function(){return""+this.amt+this.units},a.px=function(b,c){return(new a(b,c)).convert("px")},$.UnitConverter=a}();