-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflicker.min.js
1 lines (1 loc) · 3.24 KB
/
flicker.min.js
1
"use strict";var Flicker=function(){function e(r,c){return l.paused||c!==l._advanceRef?l:("forward"===l.direction?r>=l.animation.frames.length?(l.paused=!0,l.eventHandlers.flickerEnd.call(l,l.direction)):(s(r),r++):"reverse"===l.direction&&(0>r?(l.paused=!0,l.eventHandlers.flickerEnd.call(l,l.direction)):(s(r),r--)),setTimeout(requestAnimationFrame.bind(null,e.bind(null,r,c)),1e3/30),l)}function s(e){var s=""+l.rootPath+l.animation.frames[e].src,r=document.querySelector('.sprites img[src="'+s+'"]');l.currentSprite!==s&&l.eventHandlers.sequenceChange.call(l,s),l.currentSprite=s,l.currentFrame=e;try{q.drawImage(r,l.animation.frames[e].x,l.animation.frames[e].y,x.w,x.h,0,0,x.w,x.h)}catch(c){throw new u("CanvasDrawError","Failed to draw image at path '"+s+'\'\r\n. Try adding this within your flicker context:\r\n\r\n<img class="sprite" src="'+s+'" alt=" "/>')}}function r(){var s=arguments.length<=0||void 0===arguments[0]?l.currentFrame:arguments[0],r=arguments.length<=1||void 0===arguments[1]?!0:arguments[1],c=new Date;return l._advanceRef=c,l.paused=!1,l.direction=r?"forward":"reverse",e(s,c),l}function c(e){return r(e,!1)}function t(){return l.paused=!0,l}function n(e){return l._delay+=e,{play:function(){return setTimeout(r,1e3*l._delay),l},reverse:function(){return setTimeout(c,1e3*l._delay),l},pause:function(){return setTimeout(t,1e3*l._delay),l},wait:function(){return setTimeout(n,1e3*l._delay),l}}}function p(){return l.paused=!0,s(this.getAttribute("value")),l}function i(e,s){if(e in l.eventHandlers)return l.eventHandlers[e]=s;throw new u("EventHandlerRegistrationError","The specified event '"+e+"' does not exist")}function u(e,s){var r=new Error(s);return r.name=e,r}var g=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],a=function(){if(g.container)return g.container;throw new u("UnspecifiedFlickerContextError","You must specify a container for the Flicker context")}(),y=function(){var e=Array.prototype.slice.call(a.querySelectorAll(".sprite"));if(e.length>0)return e;throw new u("EmptySpriteFlickerError","Each Flicker context must contain at least one sprite")}(),o=a.querySelector("canvas"),x={w:o.getAttribute("width"),h:o.getAttribute("height")},q=o.getContext("2d"),_=a.querySelector(".controls input"),j={waitOnImages:function(e){var s=[];s.push=function(){Array.prototype.push.apply(this,arguments),this.length>=y.length&&e.call(l)};for(var r=0;r<y.length;r++)y[r].onload=function c(e){y[e].complete?s.push(y[e]):setTimeout(c.bind(null,e),10)}(r)},throttle:function(e,s,r){s||(s=100);var c,t;return function(){var n=r||this,p=+new Date,i=arguments;c&&c+s>p?(clearTimeout(t),t=setTimeout(function(){c=p,e.apply(n,i)},s)):(c=p,e.apply(n,i))}}},l={animation:function(){if(g.animation)return g.animation;throw new u("AnimationInitialisationError","You must initialise the Flicker with the object generated in flicker_map.json - have you run the Flicker package yet?")}(),rootPath:g.rootPath||"flicker/",container:a,paused:!1,direction:"forward",currentFrame:0,currentSprite:y[0].getAttribute("src"),play:r,reverse:c,pause:t,wait:n,seek:p,utils:j,on:i,eventHandlers:{sequenceChange:function(){return null},flickerEnd:function(){return null}},_advanceRef:null,_delay:0};return _.setAttribute("max",l.animation.frames.length-1),_.addEventListener("input",j.throttle(p)),l};