@smoud/tiny
Version:
Fast and tiny JavaScript library for HTML5 game and playable ads creation.
1 lines • 2.7 kB
JavaScript
!function(){"use strict";var t={bgColor:'#ffffff',bgAlpha:1,value:1,width:230,height:30,radius:15,colors:['#ff0000','#ffc322','#08c013'],animated:!0,duration:1e3,easing:Tiny.Easing.Cubic.InOut,strokeColor:'#e1e1e1',strokeWidth:0,strokeAlpha:1,resolution:1},i=function(i){for(var e in i=i||{},t)null==i[e]&&(i[e]=t[e]);var s=new Tiny.Graphics;s.beginFill(i.bgColor,i.bgAlpha),s.drawRoundedRect(i.strokeWidth/2,i.strokeWidth/2,i.width,i.height,i.radius),s.endFill(),i.strokeWidth&&(s.lineStyle(i.strokeWidth,i.strokeColor,i.strokeAlpha),s.drawRoundedRect(0,0,i.width+i.strokeWidth,i.height+i.strokeWidth,i.radius+i.strokeWidth/2)),Tiny.Sprite.call(this,s.generateTexture(i.resolution)),this.anchor.set(.5),this.game=i.game||this.game,this.animated=i.animated,this.duration=i.duration,this.easing=i.easing,this.value=i.value,this.originalWidth=i.width*i.resolution,this._tween=null,this._crop=new Tiny.Rectangle(0,0,this.originalWidth,i.height*i.resolution),this._frame=new Tiny.Rectangle(0,0,this.originalWidth,i.height*i.resolution),this._trim=new Tiny.Rectangle(.5*-this.originalWidth,0,0,i.height*i.resolution),this._uvs=new Tiny.TextureUvs,this._sprites=[];for(var h=0;h<i.colors.length;h++){s.clear(),s.beginFill(i.colors[h]),s.drawRoundedRect(0,0,i.width,i.height,i.radius),s.endFill();var r=new Tiny.Sprite(s.generateTexture(i.resolution));r.anchor=this.anchor,r.texture.frame=this._frame,r.texture.crop=this._crop,r.texture.trim=this._trim,r.texture._uvs=this._uvs,this.add(r),this._sprites.push(r)}s.destroy(),this._setValue(this.value)};(i.prototype=Object.create(Tiny.Sprite.prototype)).constructor=i,i.prototype._setValue=function(t){var i;if((t=Math.min(1,Math.max(0,t)))!=this._value&&(this._value=t,this._frame.width=this.originalWidth*t,this._crop.width=this._frame.width,this._sprites[0].texture._updateUvs(),0!=t&&1!==this._sprites.length))if(1!=t)if(2==this._sprites.length)this._sprites[1].alpha=t;else if(3==this._sprites.length){var e;t>=(i=.5)?(e=1-(1-t)/i,this._sprites[1].alpha=1,this._sprites[2].alpha=e*e):t<i&&(e=(1-t-i)/i,this._sprites[1].alpha=1-e*e,this._sprites[2].alpha=0)}else{var s=this._sprites.length-1;i=1/s;for(var h=Math.floor(t*s)+1,r=(t-i*(h-1))/i,a=0;a<this._sprites.length;a++)this._sprites[a].alpha=0;this._sprites[h-1].alpha=1,this._sprites[h].alpha=r}else this._sprites[this._sprites.length-1].alpha=1},i.prototype.setValue=function(t){if(t=Math.min(1,Math.max(0,t)),this.value=t,this.animated){this._tween&&this._tween.stop();var i=this,e={value:i._value};this._tween=this.game.tweens.add(e).to({value:t},this.duration).easing(this.easing).onUpdate((function(){i._setValue(e.value)})).start()}else this._setValue(t)},i.defaultOptions=t,Tiny.ProgressBar=i}();