gif-end-detector
Version:
A library for detecting GIF animation end events, based on libgif.js
2 lines (1 loc) • 2.54 kB
JavaScript
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).gifEndDetector=i()}(this,(function(){"use strict";function t(i){return this instanceof t?"undefined"!=typeof window&&window.SuperGif?(this.source=i,this._gif=null,this._canvas=null,this._totalFrames=0,this._currentFrame=0,this._isPlaying=!1,this._endCallbacks=[],this._frameCallbacks=[],this._loadCallbacks=[],this._errorCallbacks=[],this._onDrawFrame=null,this):(console.error("libgif.js (SuperGif) が見つかりません。先にロードしてください。"),this.source=i,this._gif=null,this._canvas=null,this._totalFrames=0,this._currentFrame=0,this._isPlaying=!1,this._endCallbacks=[],this._frameCallbacks=[],this._loadCallbacks=[],this._errorCallbacks=[],this._onDrawFrame=null,this):new t(i)}return t.prototype={load:function(t){var i=this;return t&&"function"==typeof t&&this._loadCallbacks.push(t),this._gif=new window.SuperGif({src:this.source,loop_mode:!1,auto_play:!1,on_end:function(){i._handleEnd()}}),this._gif.load((function(){i._canvas=i._gif.get_canvas(),i._totalFrames=i._gif.get_length(),i._loadCallbacks.forEach((function(t){t(i)}))})),this},onEnd:function(t){return t&&"function"==typeof t&&this._endCallbacks.push(t),this},onFrame:function(t){return t&&"function"==typeof t&&this._frameCallbacks.push(t),this},onError:function(t){return t&&"function"==typeof t&&this._errorCallbacks.push(t),this},setOnDrawFrame:function(t){if("function"==typeof t&&(this._onDrawFrame=t,this._gif)){var i=this,n=this._gif.drawFrame.bind(this._gif);this._gif.drawFrame=function(){n();var t=i._gif.getCurrentFrame();if(i._currentFrame=t,i._canvas&&i._onDrawFrame){var s=i._canvas.getContext("2d");s&&i._onDrawFrame(s,t,i._totalFrames)}i._frameCallbacks.forEach((function(n){n(t,i._totalFrames)})),t===i._totalFrames-1&&i._handleEnd()}}return this},play:function(){return this._gif&&!this._isPlaying&&(this._isPlaying=!0,this._gif.play()),this},pause:function(){return this._gif&&this._isPlaying&&(this._isPlaying=!1,this._gif.pause()),this},reset:function(){return this._gif&&this._gif.move_to(0),this},moveToFrame:function(t){return this._gif&&t>=0&&t<this._totalFrames&&this._gif.move_to(t),this},getCanvas:function(){return this._canvas},getTotalFrames:function(){return this._totalFrames},getCurrentFrame:function(){return this._currentFrame},_handleEnd:function(){this._isPlaying=!1,this._endCallbacks.forEach((function(t){t()}))}},t}));