!function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():e.SuperGif=t()}(this,function(){var e=function(e){return e.reduce(function(e,t){return 2*e+t},0)},t=function(e){for(var t=[],n=7;n>=0;n--)t.push(!!(e&1<=this.data.length)throw new Error("Attempted to read past end of stream.");return e instanceof Uint8Array?e[this.pos++]:255&e.charCodeAt(this.pos++)},this.readBytes=function(e){for(var t=[],n=0;e>n;n++)t.push(this.readByte());return t},this.read=function(e){for(var t="",n=0;e>n;n++)t+=String.fromCharCode(this.readByte());return t},this.readUnsigned=function(){var e=this.readBytes(2);return(e[1]<<8)+e[0]}},r=function(e,t){for(var n,r,a=0,i=function(e){for(var n=0,r=0;e>r;r++)t.charCodeAt(a>>3)&1<<(7&a)&&(n|=1<t;t++)c[t]=[t];c[u]=[],c[l]=null};;)if(r=n,n=i(f),n!==u){if(n===l)break;if(nf&&f++}else s();return o},a=function(n,a){a||(a={});var i=function(e){for(var t=[],r=0;e>r;r++)t.push(n.readBytes(3));return t},o=function(){var e,t;t="";do e=n.readByte(),t+=n.read(e);while(0!==e);return t},u=function(){var r={};if(r.sig=n.read(3),r.ver=n.read(3),"GIF"!==r.sig)throw new Error("Not a GIF file.");r.width=n.readUnsigned(),r.height=n.readUnsigned();var o=t(n.readByte());r.gctFlag=o.shift(),r.colorRes=e(o.splice(0,3)),r.sorted=o.shift(),r.gctSize=e(o.splice(0,3)),r.bgColor=n.readByte(),r.pixelAspectRatio=n.readByte(),r.gctFlag&&(r.gct=i(1<l;l++)for(var f=i[l];r>f;f+=o[l])a(f,u),u++;return n};u.leftPos=n.readUnsigned(),u.topPos=n.readUnsigned(),u.width=n.readUnsigned(),u.height=n.readUnsigned();var f=t(n.readByte());u.lctFlag=f.shift(),u.interlaced=f.shift(),u.sorted=f.shift(),u.reserved=f.splice(0,2),u.lctSize=e(f.splice(0,3)),u.lctFlag&&(u.lct=i(1<0&&(3===p?null!==d?h.putImageData(_[d].data,0,0):h.clearRect(g.leftPos,g.topPos,g.width,g.height):d=t-1,2===p&&h.clearRect(g.leftPos,g.topPos,g.width,g.height));var r=h.getImageData(e.leftPos,e.topPos,e.width,e.height),a=r.data;e.pixels.forEach(function(e,t){e!==f&&(a[4*t+0]=n[e][0],a[4*t+1]=n[e][1],a[4*t+2]=n[e][2],a[4*t+3]=255)}),r.data.set(a),h.putImageData(r,e.leftPos,e.topPos),m||(m=!0),g=e},D=function(){var e=-1,n=0,r=function(){var t=v?1:-1;return(e+t+_.length)%_.length},a=function(t){e+=t,l()},i=function(){null!==T&&T(x),n++},o=function(){var t=!1,o=function(){if(t=y){a(1);var u=10*_[e].delay;u||(u=100);var l=r();0===l&&(u+=P,setTimeout(i,u-1)),(k!==!1||0!==l||0>n)&&setTimeout(o,u)}};return function(){t||setTimeout(o,0)}}(),l=function(){var t;e=parseInt(e,10),e>_.length-1&&(e=0),0>e&&(e=0),t=w[e],B.getContext("2d").putImageData(_[e].data,t.x,t.y)},f=function(){y=!0,o()},c=function(){y=!1};return{init:function(){u||(t.auto_play?o():(e=0,l()))},step:o,play:f,pause:c,playing:y,move_relative:a,current_frame:function(){return e},length:function(){return _.length},move_to:function(t){e=t,l()},get_frames:function(){return _},buffer:function(){return b},get_playing:function(){return y}}}(),M=function(){},R=function(e,t){return function(t){e(t)}},N={hdr:R(A),gce:R(G),com:R(M),app:{NETSCAPE:R(M)},img:R(F,!0),eof:function(e){z(),D.init(),l=!1,console.log("ok"),j&&j(x)}},O=function(){b=t.p5inst.createImage(1,1),B=b.canvas,L=!0},H=function(){var e;return e=t.max_width&&o&&o.width>t.max_width?t.max_width/o.width:1},L=!1,j=!1,q=function(e){return l?!1:(j=e?e:!1,l=!0,_=[],C(),d=null,p=null,h=null,g=null,!0)};return{play:D.play,pause:D.pause,move_relative:D.move_relative,move_to:D.move_to,get_playing:D.get_playing,get_canvas:function(){return canvas},get_canvas_scale:function(){return H()},get_loading:function(){return l},get_auto_play:function(){return t.auto_play},get_length:function(){return D.length()},get_current_frame:function(){return D.current_frame()},get_frames:function(){return D.get_frames()},buffer:function(){return D.buffer()},load_url:function(e,t){if(q(t)){var r=new XMLHttpRequest;r.overrideMimeType("text/plain; charset=x-user-defined"),r.onloadstart=function(){L||O()},r.onload=function(e){i=new n(r.responseText),setTimeout(S,0)},r.onerror=function(){U("xhr")},r.open("GET",e,!0),r.send()}},load:function(e){this.load_url(x.src,e)},load_raw:function(e,t){q(t)&&(L||O(),i=new n(e),setTimeout(S,0))},set_frame_offset:I}};return i}),function(){p5.prototype.loadGif=function(e,t){var n=new SuperGif({gif:e,p5inst:this});n.load(t);var r=n.buffer();return r.play=n.play,r.pause=n.pause,r.playing=n.get_playing,r.frames=n.get_frames,r.totalFrames=n.get_length,r.loaded=function(){return!n.get_loading()},r.frame=function(e){return"number"!=typeof e?n.get_current_frame():void n.move_to(e)},r},p5.prototype.loadRawGif=function(e,t){var n=new SuperGif({gif:"",p5inst:this});n.load_raw(e,t);var r=n.buffer();return r.play=n.play,r.pause=n.pause,r.playing=n.get_playing,r.frames=n.get_frames,r.totalFrames=n.get_length,r.loaded=function(){return!n.get_loading()},r.frame=function(e){return"number"!=typeof e?n.get_current_frame():void n.move_to(e)},r}}();