VictorGa/LightCinematic

View on GitHub
build/LightCinematic.min.js

Summary

Maintainability
A
0 mins
Test Coverage
var LightManager=function(){function LightManager(properties){this._stop=!1,this._then=0,this._update=this.update.bind(this),this._properties=properties,this._fpsInterval=1e3/this._properties.fps,this._properties.canvas.width=this._properties.width,this._properties.canvas.height=this._properties.height,this._ctx=this._properties.canvas.getContext("2d"),this._children=[]}return Object.defineProperty(LightManager.prototype,"fps",{set:function(value){this._fpsInterval=1e3/value},enumerable:!0,configurable:!0}),Object.defineProperty(LightManager.prototype,"stop",{set:function(value){this._stop=value},enumerable:!0,configurable:!0}),LightManager.prototype.appendChild=function(child){child.setContext(this._ctx),this._children.push(child)},LightManager.prototype.removeChild=function(child){var index=this._children.indexOf(child);-1!==index&&this._children.splice(index,0)},LightManager.prototype.reuseChild=function(properties){for(var i=0;i<this._children.length;i++)if(this._children[i].free)return this._children[i].setProperties(properties);return this._children.push(new LightCinematic(properties)),this._children[this._children.length-1]},LightManager.prototype.start=function(){this._stop=!1,requestAnimationFrame(this._update)},LightManager.prototype.update=function(timestamp){if(!this._stop){var elapsed=timestamp-this._then;if(elapsed>this._fpsInterval){this._ctx.clearRect(0,0,this._properties.canvas.width,this._properties.canvas.height);for(var length=this._children.length;length--;)this._children[length].free||this._children[length].draw();this._then=timestamp-elapsed%this._fpsInterval}requestAnimationFrame(this._update)}},LightManager.prototype.destruct=function(){for(this.stop=!0;this._children.length;)this._children.pop().destruct();this._children=null},LightManager}(),LightCinematic=function(){function LightCinematic(properties){this._currentFrame=0,this._spriteProps={iX:0,iY:0,x:0,y:0},this.setProperties(properties)}return LightCinematic.prototype.setProperties=function(properties){return this.free=!1,this._properties=properties,this._spriteProps.iX=this._spriteProps.iY=this._spriteProps.x=this._spriteProps.y=0,this._currentFrame=0,this.direction=LightCinematic.FORWARD,this},LightCinematic.prototype.setContext=function(ctx){this._ctx=ctx},LightCinematic.prototype.draw=function(){this.direction===LightCinematic.FORWARD&&this._currentFrame<this._properties.frames.count-1||this.direction===LightCinematic.REVERSE&&this._currentFrame>0?this._currentFrame+=this.direction:this._properties.loop&&(this._currentFrame=0),this.drawImage(this._currentFrame)},LightCinematic.prototype["goto"]=function(frame){this._ctx.clearRect(0,0,this._ctx.canvas.width,this._ctx.canvas.height),this.drawImage(frame)},LightCinematic.prototype.drawImage=function(frame){try{this._ctx.drawImage(this.getCurrentFrameImage(frame),this._spriteProps.x,this._spriteProps.y,this._properties.frames.width,this._properties.frames.height,this._properties.x,this._properties.y,this._properties.frames.width,this._properties.frames.height)}catch(e){console.error(e)}},LightCinematic.prototype.getCurrentFrameImage=function(frame){return this._properties.frames.src.length>1?this._properties.frames.src[frame]:(this.setFrameInfo(frame),this._properties.frames.src[0])},LightCinematic.prototype.setFrameInfo=function(frame){this._spriteProps.iX=frame%this._properties.frames.cols,this._spriteProps.iY=Math.floor(frame/this._properties.frames.cols),this._spriteProps.x=this._spriteProps.iX*this._properties.frames.width,this._spriteProps.y=this._spriteProps.iY*this._properties.frames.height},LightCinematic.prototype.destruct=function(){},LightCinematic.FORWARD=1,LightCinematic.REVERSE=-1,LightCinematic}();