Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
2 lines (1 sloc) 4.95 KB
function CoinStackBar(a){this.container=a.container;this.coinimgsrc=a.coinimgsrc;this.coinimgwidth=a.coinimgwidth||1;this.coinimgheight=a.coinimgheight||1;this.coinheight=a.coinheight||1;this.min=a.min||0;this.max=a.max||100;this.value=0;this.maxstackheight=a.maxstackheight||10;this.xoffset=a.xoffset||10;this.yoffset=a.yoffset||5;this.setDimensions(a.containerwidth,a.containerheight);this.seed=a.seed||Math.random();this.reseed="aways"==a.reseed||"never"==a.reseed||"onzero"==a.reseed?a.reseed:"always"; this.coineffectduration=a.coineffectduration||300;this.showshadow="undefined"!=typeof a.showshadow?a.showshadow:!0;this.coinanimxdrop=a.coinanimxdrop||0;this.coinanimydrop=a.coinanimydrop||300;this.coins=[];this.container.style.position="relative";this.containerheight=(this.coinimgheight+this.maxstackheight*this.coinheight)*this.stacksize;this.containerwidth=(this.coinimgwidth+2*this.xoffset)*this.stacksize;this.container.style.height=this.containerheight+"px";this.container.style.width=this.containerwidth+ "px";this.shadow=document.createElement("div");this.shadow.style.position="relative";this.shadowwidth=this.coinimgwidth*this.stacksize*2;this.shadow.style.width=this.shadowwidth+"px";this.shadow.style.height=this.coinimgheight*this.stacksize*2+"px";this.shadow.style.top=this.containerheight-this.coinimgheight*this.stacksize*1.5+"px";this.shadow.style.left=-(this.shadowwidth/4)+"px";this.shadow.style.opacity=this.showshadow?1:0;this.shadow.style.background="-webkit-radial-gradient(ellipse closest-side, black, rgba(0,0,0,0))"; this.shadow.style.background="radial-gradient(ellipse closest-side, black, rgba(0,0,0,0))";this.container.appendChild(this.shadow);this.container.CoinStackBar=this;this.setValue(a.startvalue||0);this.update()}CoinStackBar.prototype.rand=function(a,c){return Math.cos(a*Math.cos(a*c))}; CoinStackBar.prototype.setDimensions=function(a,c){var b="",d=a/this.coinimgwidth,e=c/(this.coinimgheight+this.maxstackheight*this.coinheight);"undefined"!=typeof a&&"undefined"!=typeof c&&(b=d<e?"width":"height");this.stacksize="undefined"==typeof a||"height"==b?e:"undefined"==typeof c||"width"==b?d:1};CoinStackBar.prototype.updateMinMax=function(a,c){"undefined"!=typeof a&&(this.min=a);"undefined"!=typeof c&&(this.max=c)}; CoinStackBar.prototype.setValue=function(a,c,b){"undefined"==typeof c&&"undefined"==typeof b||this.updateMinMax(c,b);a=Math.min(Math.max(a,this.min),this.max);if(a==this.value)return!1;c=Math.round(a/this.max*this.maxstackheight);b=Math.round(this.value/this.max*this.maxstackheight);if("always"==this.reseed||0==c&&0!=b&&"onzero"==this.reseed)this.seed=this.rand(this.seed,this.seed+1);if(c>b)for(var d=b;d<c;d++)this.createNewCoin(d,d-b);else if(c<b)for(d=b;d>=c;d--)this.fadeOutCoin(d,b-d);this.showshadow&& (this.shadow.style.opacity=c/this.maxstackheight);this.value=a;return!0}; CoinStackBar.prototype.createNewCoin=function(a,c){var b=document.createElement("img");if("[object Array]"===Object.prototype.toString.call(this.coinimgsrc)){var d=Math.floor(Math.abs(this.rand(a+1,this.seed)*this.coinimgsrc.length));b.src=this.coinimgsrc[d]}else b.src=this.coinimgsrc;var d=+this.rand(a,this.seed)*this.xoffset*this.stacksize,e=a*this.coinheight*this.stacksize+this.rand(a,this.seed)*this.yoffset*this.stacksize;b.style.position="absolute";b.style.width=this.coinimgwidth*this.stacksize+ "px";b.style.height=this.coinimgheight*this.stacksize+"px";b.style.opacity=0;this.container.appendChild(b);var f=Date.now();0<this.coins.length&&(f=Math.max(this.coins[this.coins.length-1].starttime+0.3*this.coineffectduration,f));this.coins.push({element:b,starttime:f,xpos:d,ypos:e,xstart:this.coinanimxdrop,ystart:this.coinanimydrop,duration:this.coineffectduration,status:"fadein"})}; CoinStackBar.prototype.fadeOutCoin=function(a,c){this.coins[a]&&"fadeout"!=this.coins[a].status&&(this.coins[a].starttime=Date.now()+c*this.coins[a].duration*0.3,this.coins[a].status="fadeout")};CoinStackBar.prototype.removeCoin=function(a){var c=this.container.getElementsByTagName("img");c[a]&&(this.container.removeChild(c[a]),this.coins[a]&&this.coins.splice(a,1))}; CoinStackBar.prototype.update=function(){this.coins&&this.coins.forEach(function(a,b,d){var e=1-(a.starttime+a.duration-Date.now())/a.duration,e=Math.max(Math.min(1,e),0);"fadein"==a.status&&(a.element.style.bottom=a.ypos+a.ystart*(1-e)*this.stacksize+"px",a.element.style.left=a.xpos+a.xstart*(1-e)*this.stacksize+"px",a.element.style.opacity=e,1<=e&&(a.status="idle"));"fadeout"==a.status&&(a.element.style.bottom=a.ypos+a.ystart*e*this.stacksize+"px",a.element.style.left=a.xpos+a.xstart*(1-e)*this.stacksize+ "px",a.element.style.opacity=1-e,1<=e&&this.removeCoin(b),this.coins[b-1]&&(a.element.style.opacity=Math.min(a.element.style.opacity,d[b-1].element.style.opacity)))},this);var a=this;(function(a){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1E3/60)}})()(function(){a.update()})};