-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequencer.min.js
1 lines (1 loc) · 4.72 KB
/
sequencer.min.js
1
!function(window,document){"use strict";var Sequencer=function(a){return this.options=a||{},this.fps=this.options.fps||50,this.fps=1e3/this.fps,this.canvas=this.options.canvas||document.getElementById("canvas"),this.x=this.options.x||0,this.y=this.options.y||0,this.width=this.options.width||!1,this.height=this.options.height||!1,this.data={},this.ctx=this.canvas.getContext("2d"),this.playing=!1,this.current=0,this.total=0,this.loops=1,this.image=new Image,this.reversed=!1,window.raf=window.requestAnimationFrame,window.caf=window.cancelAnimationFrame,this};Sequencer.prototype.constructor=Sequencer,Sequencer.prototype.loadSequence=function(name,sequence,preloadimgs){var that=this;return sequence=sequence||this.sequence,name=name||"default",preloadimgs=preloadimgs||!1,this.get(sequence,function(data){that.data[name]=eval(data);var event=new CustomEvent("loaded",{detail:{sequence:name}});that.canvas.dispatchEvent(event),preloadimgs&&that.preload(name)}),this},Sequencer.prototype.preload=function(a){var b=this.data[a].length,c=0,d=this;for(var e in this.data[a]){var f=new Image,g=this.data[a][e];f.onload=function(){c++;var e=new CustomEvent("progress",{detail:{sequence:a,progress:c/b*100}});if(d.canvas.dispatchEvent(e),c===b){var e=new CustomEvent("preloaded",{detail:{sequence:a}});d.canvas.dispatchEvent(e)}},f.src=g,f.complete&&f.onload()}},Sequencer.prototype.play=function(a,b){var c=this;return"undefined"==typeof this.data[a]?!1:(this.loop=b,this.total=this.data[a].length,this.current=0,this.loops=0,this.currentSequence=a||"default",this.playing=!0,this.time=(new Date).getTime(),this.af=window.raf(function(){c.draw()}),this)},Sequencer.prototype.seek=function(a,b){var c=this;return"undefined"==typeof this.data[a]?!1:"undefined"==typeof this.data[a][b]?!1:(this.loop=!1,this.total=this.data[a].length,this.current=b,this.loops=0,this.currentSequence=a||"default",this.playing=!0,this.time=(new Date).getTime(),this.af=window.raf(function(){c.draw(!0)}),this)},Sequencer.prototype.reverse=function(a,b){return"undefined"==typeof this.data[a]?!1:(this.data[a].reverse(),this.reversed=!0,this.play(a,b))},Sequencer.prototype.dereverse=function(){return this.reversed&&(this.data[this.currentSequence].reverse(),this.reversed=!1),this},Sequencer.prototype.get=function(a,b){var c=this;try{var d=new(window.XMLHttpRequest||window.ActiveXObject)("MSXML2.XMLHTTP.3.0");d.open("GET",a,!0),d.setRequestHeader("X-Requested-With","XMLHttpRequest"),d.setRequestHeader("Content-type","application/x-www-form-urlencoded"),d.onreadystatechange=function(){return d.readyState>3&&b&&b(d.responseText,d)},d.onprogress=function(a){var b=new CustomEvent("xhrprogress",{detail:{sequence:this.currentSequence,progress:a}});c.canvas.dispatchEvent(b)},d.send(null)}catch(e){return window.console&&console.log("Ajax request failed",e)}},Sequencer.prototype.stop=function(){return window.caf(this.af),this.playing=!1,this.data=[],this},Sequencer.prototype.clean=function(){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height)},Sequencer.prototype.draw=function(a){if(!this.playing)return!1;var b,c=(new Date).getTime(),d=c-this.time,e=this,a=a||!1;if(!a){if(this.af=window.raf(function(){e.draw()}),d<this.fps)return!1;this.time=c-d%this.fps}return this.current===this.total&&this.loop?(this.current=0,this.loops++,b=new CustomEvent("stopped",{detail:{sequence:this.currentSequence,loops:this.loops}}),this.canvas.dispatchEvent(b),!1):this.current!==this.total||this.loop?(this.image.onload=function(){e.clean(),e.width&&e.height?e.ctx.drawImage(e.image,e.x,e.y,e.width,e.height):e.ctx.drawImage(e.image,e.x,e.y)},this.image.onerror=function(a){console.error("Image failed to load",a)},this.image.src=this.data[this.currentSequence][this.current],this.image.complete&&this.image.onload(),void(a||this.current++)):(window.caf(this.af),this.playing=!1,this.current=0,b=new CustomEvent("stopped",{detail:{sequence:this.currentSequence,loops:this.loops,reversed:this.reversed}}),this.canvas.dispatchEvent(b),!1)},function(){function a(a,b){b=b||{bubbles:!1,cancelable:!1,detail:void 0};var c=document.createEvent("CustomEvent");return c.initCustomEvent(a,b.bubbles,b.cancelable,b.detail),c}a.prototype=window.Event.prototype,window.CustomEvent=a}(),function(){for(var a=0,b=["webkit","moz"],c=0;c<b.length&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[b[c]+"CancelAnimationFrame"]||window[b[c]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(b){var c=(new Date).getTime(),d=Math.max(0,16-(c-a)),e=window.setTimeout(function(){b(c+d)},d);return a=c+d,e}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)})}(),window.Sequencer=Sequencer}(window,document);