-
Notifications
You must be signed in to change notification settings - Fork 19
/
sprite.min.js
11 lines (11 loc) · 7.44 KB
/
sprite.min.js
1
2
3
4
5
6
7
8
9
10
11
/**
* A powerful, easy-to-use Sprite animation library for HTML5 Canvas.
*
* @author Isaac Sukin (IceCreamYou)
* @license MIT License: http://opensource.org/licenses/mit-license.php
* @ignore
*/
(function(){function t(t,i,s){var e="function"==typeof s.postInitCallback?s.postInitCallback:null,r=this;s.postInitCallback=function(t){r.sprite=t,r.baseImage=t.image,r.cachedImages={"00":r.baseImage},r.maps={};for(var s in i)i.hasOwnProperty(s)&&r.set(s,i[s]);e&&e.apply(this,arguments)},this.sprite=new Sprite(t,s),this.sprite.spriteMap=this}t.prototype={set:function(t,i){i instanceof Array&&(i={startRow:i[0],startCol:i[1],endRow:i[2],endCol:i[3],squeeze:i[4],flipped:i[5]}),this.maps[t]={startRow:i.startRow!==void 0?i.startRow:0,startCol:i.startCol!==void 0?i.startCol:0,endRow:i.endRow!==void 0?i.endRow:this.sprite.rows-1,endCol:i.endCol!==void 0?i.endCol:this.sprite.cols-1,squeeze:i.squeeze!==void 0?i.squeeze:!1,flipped:i.flipped!==void 0?i.flipped:{horizontal:!1,vertical:!1}};var s=this.maps[t].flipped,e=(s.horizontal?"1":"0")+(s.vertical?"1":"0");return this.sprite.cachedImages[e]===void 0&&(this.sprite.cachedImages[e]=this.sprite._prerender(this.baseImage,s)),this},unset:function(t){return this.maps.hasOwnProperty(t)&&delete this.maps[t],this},use:function(t,i){if(this.activeLoop==t&&!i)return this;this.activeLoop=t;var s=this.maps[t];return this.sprite.setLoop(s.startRow,s.startCol,s.endRow,s.endCol,s.squeeze,s.flipped),this},start:function(t){return t&&this.use(t),this.sprite.startLoop(),this},stop:function(){return this.sprite.stopLoop(),this},reset:function(){return this.sprite.reset(),this},runOnce:function(t,i){return i&&this.use(i),this.sprite.runLoop(t),this},draw:function(t,i,s,e,r){return this.sprite.draw(t,i,s,e,r),this},clone:function(){return new t(this.sprite.sourceFile,this.maps,this.sprite)}},this.SpriteMap=t}).call(this),function(){function t(i,s){if("string"==typeof i){this.sourceFile=i;var e=t.getImageFromCache(i);if(e)this._init(e,s);else{var r=new Image,h=this;r.onload=function(){h._init(this,s)},r._src=i,r.src=i,t.saveImageToCache(i,r)}}else if(i instanceof HTMLImageElement||i instanceof Image){if(!i.src)return;if(this.sourceFile=i._src||i.src,i.complete||i.width&&i.height)t.saveImageToCache(this.sourceFile,i),this._init(i,s);else{if(i._src)return;var o=i.onload,h=this;i.onload=function(){"function"==typeof o&&o(),t.saveImageToCache(h.sourceFile,i),h._init(this,s)}}}else i instanceof HTMLCanvasElement&&this._init(i,s)}t.prototype={_init:function(t,i){this.width=t.width,this.height=t.height,this.frameW=i.frameW||t.width,this.frameH=i.frameH||t.height,this.projectedW=i.projectedW||this.frameW,this.projectedH=i.projectedH||this.frameH,this.rows=Math.floor(this.height/this.frameH),this.cols=Math.floor(this.width/this.frameW),this.startRow=i.startRow||0,this.startCol=i.startCol||0,this.endRow=i.endRow===void 0?this.rows-1:i.endRow,this.endCol=i.endCol===void 0?this.cols-1:i.endCol,this.row=this.startRow,this.col=this.startCol,this.frame=1,this.squeeze=i.squeeze||!1,this.interval=i.interval===void 0?125:i.interval,this.useTimer=i.useTimer===void 0?!0:i.useTimer,this.advanceFramesManually=i.advanceFramesManually||!1,this.lastFrameUpdateTime=0,this.flipped=i.flipped||{horizontal:!1,vertical:!1},this.flipped.horizontal=this.flipped.horizontal||!1,this.flipped.vertical=this.flipped.vertical||!1,this.cachedImages={"00":t};var s=this.flipped,e=(s.horizontal?"1":"0")+(s.vertical?"1":"0");this.cachedImages[e]===void 0&&(this.cachedImages[e]=this._prerender(t,s)),this.image=this.cachedImages[e],this._runOnce=!1,this.squeeze&&(this.cols=this.endCol-this.startCol+1),this.numFrames=this.getNumFrames(),i.postInitCallback&&i.postInitCallback(this)},_prerender:function(t,i){var s=document.createElement("canvas");s.width=this.width,s.height=this.height;var e=s.getContext("2d");return(i.horizontal||i.vertical)&&(e.translate(i.horizontal?s.width:0,i.vertical?s.height:0),e.scale(i.horizontal?-1:1,i.vertical?-1:1)),e.drawImage(t,0,0),s},draw:function(t,i,s,e,r){try{t.save(),e=e||this.projectedW,r=r||this.projectedH;var h=this.col*this.frameW,o=this.row*this.frameH;this.flipped.horizontal&&(h=this.width-h-this.frameW),this.flipped.vertical&&(o=this.height-o-this.frameH),t.drawImage(this.image,h,o,this.frameW,this.frameH,i,s,e,r),t.restore()}catch(a){console&&console.error&&console.error(a)}return!this.useTimer&&!this.advanceFramesManually&&Date.now()-this.lastFrameUpdateTime>this.interval&&this.nextFrame(),this},reset:function(){return this.row=this.startRow,this.col=this.startCol,this.frame=1,this.lastFrameUpdateTime=0,this},changeFrame:function(t){return this.frame+=t,this.setFrame(this.frame),this},setFrame:function(t,i){if(i!==void 0)this.row=t,this.col=i,this.frame=this.squeeze?this.cols*(this.row-this.startRow+1)-(this.endCol-this.col):this.cols*(this.row-this.startRow+1)-(this.cols-(this.col+1))-this.startCol;else{var s=this.frameNumberToRowCol(t);this.frame=s.frame,this.row=s.row,this.col=s.col}return this.lastFrameUpdateTime=Date.now(),this},setLoop:function(t,i,s,e,r,h){if(this.stopLoop(),(null===s||s===void 0)&&(s=this.rows-1),(null===e||e===void 0)&&(e=this.cols-1),r!==void 0&&(this.squeeze=r),h!==void 0){this.flipped=h;var o=this.flipped,a=(o.horizontal?"1":"0")+(o.vertical?"1":"0");this.cachedImages[a]===void 0&&(this.cachedImages[a]=this._prerender(this.image,o)),this.image=this.cachedImages[a]}return this.startRow=t,this.startCol=i,this.endRow=s,this.endCol=e,this.reset(),this.numFrames=this.getNumFrames(),this},startLoop:function(t,i,s,e,r,h){if(t!==void 0&&i!==void 0&&this.setLoop(t,i,s,e,r,h),this.lastFrameUpdateTime=Date.now(),this.interval&&this.useTimer){var o=this;this.intervalID=setInterval(function(){o.nextFrame()},this.interval)}return this},stopLoop:function(){return this.lastFrameUpdateTime=0,this.intervalID&&clearInterval(this.intervalID),this},runLoop:function(t,i,s,e,r,h,o){return this.runLoopCallback=t||function(){},this._runOnce=!0,Array.prototype.shift.call(arguments),this.startLoop.apply(this,arguments),this},prevFrame:function(){return changeFrame(-1),this},nextFrame:function(){return this.col++,this.frame++,this.row==this.endRow&&this.col>this.endCol?this._runOnce?(this.stopLoop(),this._runOnce=!1,this.runLoopCallback(this)):this.reset():this.squeeze&&this.col>this.endCol?(this.col=this.startCol,this.row++):!this.squeeze&&this.col>=this.cols&&(this.col=0,this.row++),this.lastFrameUpdateTime=Date.now(),this},getFrame:function(){return{row:this.row,col:this.col,frame:this.frame}},getNumFrames:function(){return this.squeeze?(this.endRow-this.startRow+1)*(this.endCol-this.startCol+1):(this.endRow-this.startRow)*this.cols-this.startCol+this.endCol+1},frameNumberToRowCol:function(t){var i,s;return t=(t+this.numFrames)%this.numFrames||this.numFrames,this.squeeze?(i=this.startRow+Math.floor((t-1)/this.cols),s=(t-1)%this.cols+this.startCol):(i=this.startRow+Math.floor((t+this.startCol-1)/this.cols),s=(t+this.startCol-1)%this.cols),{frame:t,row:i,col:s}},clone:function(){return new t(this.sourceFile,this)}},this.Sprite=t}.call(this),function(){var t={};Sprite.getImageFromCache=function(i){return t[i]?t[i]:null},Sprite.saveImageToCache=function(i,s){t[i]=s},Sprite.preloadImages=function(t,i){var s=t.length,e=-1,r,h,o=function(t,r){e++,"function"==typeof t&&t(r,e,s),e==s&&"function"==typeof i.finishCallback&&i.finishCallback(s)};o();for(var a=function(){Sprite.saveImageToCache(this._src,this),o(i.itemCallback,this._src)};t.length;)r=t.pop(),h=new Image,h._num=s-t.length,h._src=r,h.onload=a,h.src=r}}.call(this);
/*
//@ sourceMappingURL=sprite.min.map
*/