forked from themadcreator/gifler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifler.bare.js
396 lines (341 loc) · 11.2 KB
/
gifler.bare.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Generated by CoffeeScript 1.10.0
var Animator, Decoder, Gif, GifReader, Promise, gifler,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
GifReader = require('omggif').GifReader;
Promise = require('bluebird');
/*---
head : 'gifler()'
text :
- This is the main entrypoint to the library.
- Prepares and sends an XHR request to load the GIF file.
- Returns a <b>Gif</b> instance for interacting with the library.
args :
url : 'URL to .gif file'
return : 'a Gif instance object'
*/
gifler = function(url) {
var aync, promise, xhr;
xhr = new XMLHttpRequest();
xhr.open('GET', url, aync = true);
xhr.responseType = 'arraybuffer';
promise = new Promise(function(resolve, reject) {
return xhr.onload = function(e) {
return resolve(this.response);
};
});
xhr.send();
return new Gif(promise);
};
Gif = (function() {
Gif.getCanvasElement = function(selector) {
var element, ref;
if (typeof selector === 'string' && ((ref = (element = document.querySelector(selector))) != null ? ref.tagName : void 0) === 'CANVAS') {
return element;
} else if ((selector != null ? selector.tagName : void 0) === 'CANVAS') {
return selector;
} else {
throw new Error('Unexpected selector type. Valid types are query-selector-string/canvas-element');
}
};
function Gif(dataPromise) {
this._animatorPromise = dataPromise.then(function(data) {
var reader;
reader = new GifReader(new Uint8Array(data));
return Decoder.decodeFramesAsync(reader).then(function(frames) {
return new Animator(reader, frames);
});
});
}
/*---
head : 'gif.animate()'
text :
- >
Animates the loaded GIF, drawing each frame into the canvas.
This matches the look of an <img> tag.
args :
selector : 'A <canvas> element or query selector for a <canvas> element.'
*/
Gif.prototype.animate = function(selector) {
var canvas;
canvas = Gif.getCanvasElement(selector);
return this._animatorPromise.then(function(animator) {
return animator.animateInCanvas(canvas);
});
};
/*---
head : 'gif.frames()'
text :
- >
Runs the animation on the loaded GIF, but passes the
canvas context and GIF frame to the <b>onDrawFrame</b>
callback for rendering.
- >
This gives you complete control of how the frame is drawn
into the canvas context.
args :
selector : 'A <canvas> element or query selector for a <canvas> element.'
onDrawFrame : 'A callback that will be invoked when each frame should be drawn into the canvas. see Animator.onDrawFrame.'
setDimesions : 'OPTIONAL. If true, the canvas''s width/height will be set to the dimension of the loaded GIF. default: false.'
*/
Gif.prototype.frames = function(selector, onDrawFrame, setCanvasDimesions) {
var canvas;
if (setCanvasDimesions == null) {
setCanvasDimesions = false;
}
canvas = Gif.getCanvasElement(selector);
return this._animatorPromise.then(function(animator) {
animator.onDrawFrame = onDrawFrame;
return animator.animateInCanvas(canvas, setCanvasDimesions);
});
};
/*---
head : 'gif.get()'
text :
- >
To get even more control, and for your convenience,
this method returns a promise that will be fulfilled with
an <b>Animator</b> instance. The animator will be in an unstarted state,
but can be started with a call to <b>animator.animateInCanvas()</b>
*/
Gif.prototype.get = function(callback) {
return this._animatorPromise;
};
return Gif;
})();
/*
These methods decode the pixels for each frame (decompressing and de-interlacing)
into a Uint8ClampedArray, which is suitable for canvas ImageData.
*/
Decoder = (function() {
function Decoder() {}
Decoder.decodeFramesSync = function(reader) {
var j, ref, results;
return (function() {
results = [];
for (var j = 0, ref = reader.numFrames(); 0 <= ref ? j < ref : j > ref; 0 <= ref ? j++ : j--){ results.push(j); }
return results;
}).apply(this).map(function(frameIndex) {
return Decoder.decodeFrame(reader, frameIndex);
});
};
Decoder.decodeFramesAsync = function(reader) {
var concurrency, j, ref, results;
return Promise.map((function() {
results = [];
for (var j = 0, ref = reader.numFrames(); 0 <= ref ? j < ref : j > ref; 0 <= ref ? j++ : j--){ results.push(j); }
return results;
}).apply(this), (function(i) {
return Decoder.decodeFrame(reader, i);
}), concurrency = 1);
};
Decoder.decodeFrame = function(reader, frameIndex) {
var frameInfo;
frameInfo = reader.frameInfo(frameIndex);
frameInfo.pixels = new Uint8ClampedArray(reader.width * reader.height * 4);
reader.decodeAndBlitFrameRGBA(frameIndex, frameInfo.pixels);
return frameInfo;
};
return Decoder;
})();
Animator = (function() {
/*---
head : 'animator::createBufferCanvas()'
text :
- >
Creates a buffer canvas element since it is much faster
to call <b>.putImage()</b> than <b>.putImageData()</b>.
- >
The omggif library decodes the pixels into the full gif
dimensions. We only need to store the frame dimensions,
so we offset the putImageData call.
args :
frame : A frame of the GIF (from the omggif library)
width : width of the GIF (not the frame)
height : height of the GIF
return : A <canvas> element containing the frame's image.
*/
Animator.createBufferCanvas = function(frame, width, height) {
var bufferCanvas, bufferContext, imageData;
bufferCanvas = document.createElement('canvas');
bufferContext = bufferCanvas.getContext('2d');
bufferCanvas.width = frame.width;
bufferCanvas.height = frame.height;
imageData = bufferContext.createImageData(width, height);
imageData.data.set(frame.pixels);
bufferContext.putImageData(imageData, -frame.x, -frame.y);
return bufferCanvas;
};
function Animator(_reader, _frames) {
var ref;
this._reader = _reader;
this._frames = _frames;
this._advanceFrame = bind(this._advanceFrame, this);
this._nextFrameRender = bind(this._nextFrameRender, this);
this._nextFrame = bind(this._nextFrame, this);
ref = this._reader, this.width = ref.width, this.height = ref.height;
this._loopCount = this._reader.loopCount();
this._loops = 0;
this._frameIndex = 0;
this._running = false;
}
/*---
head : 'animator.start()'
text :
- Starts running the GIF animation loop.
*/
Animator.prototype.start = function() {
this._lastTime = new Date().valueOf();
this._delayCompensation = 0;
this._running = true;
setTimeout(this._nextFrame, 0);
return this;
};
/*---
head : 'animator.stop()'
text :
- Stops running the GIF animation loop.
*/
Animator.prototype.stop = function() {
this._running = false;
return this;
};
/*---
head : 'animator.reset()'
text :
- Resets the animation loop to the first frame.
- Does not stop the animation from running.
*/
Animator.prototype.reset = function() {
this._frameIndex = 0;
this._loops = 0;
return this;
};
/*---
head : 'animator.running()'
return : A boolean indicating whether or not the animation is running.
*/
Animator.prototype.running = function() {
return this._running;
};
Animator.prototype._nextFrame = function() {
requestAnimationFrame(this._nextFrameRender);
};
Animator.prototype._nextFrameRender = function() {
var frame, ref;
if (!this._running) {
return;
}
frame = this._frames[this._frameIndex];
if ((ref = this.onFrame) != null) {
ref.apply(this, [frame, this._frameIndex]);
}
return this._enqueueNextFrame();
};
Animator.prototype._advanceFrame = function() {
this._frameIndex += 1;
if (this._frameIndex >= this._frames.length) {
if (this._loopCount !== 0 && this._loopCount === this._loops) {
this.stop();
} else {
this._frameIndex = 0;
this._loops += 1;
}
}
};
Animator.prototype._enqueueNextFrame = function() {
var actualDelay, delta, frame, frameDelay;
this._advanceFrame();
while (this._running) {
frame = this._frames[this._frameIndex];
delta = new Date().valueOf() - this._lastTime;
this._lastTime += delta;
this._delayCompensation += delta;
frameDelay = frame.delay * 10;
actualDelay = frameDelay - this._delayCompensation;
this._delayCompensation -= frameDelay;
if (actualDelay < 0) {
this._advanceFrame();
continue;
} else {
setTimeout(this._nextFrame, actualDelay);
break;
}
}
};
/*---
head : 'animator.animateInCanvas()'
text :
- >
This method prepares the canvas to be drawn into and sets up
the callbacks for each frame while the animation is running.
- >
To change how each frame is drawn into the canvas, override
<b>animator.onDrawFrame()</b> before calling this method.
If <b>animator.onDrawFrame()</b> is not set, we simply draw
the frame directly into the canvas as is.
- >
You may also override <b>animator.onFrame()</b> before calling
this method. onFrame handles the lazy construction of canvas
buffers for each frame as well as the disposal method for each frame.
args :
canvas : A canvas element.
setDimensions : 'OPTIONAL. If true, the canvas width/height will be set to match the GIF. default: true.'
*/
Animator.prototype.animateInCanvas = function(canvas, setDimensions) {
var ctx;
if (setDimensions == null) {
setDimensions = true;
}
if (setDimensions) {
canvas.width = this.width;
canvas.height = this.height;
}
ctx = canvas.getContext('2d');
if (this.onDrawFrame == null) {
this.onDrawFrame = function(ctx, frame, i) {
return ctx.drawImage(frame.buffer, frame.x, frame.y);
};
}
if (this.onFrame == null) {
this.onFrame = (function(_this) {
return function(frame, i) {
var ref, saved;
if (frame.buffer == null) {
frame.buffer = Animator.createBufferCanvas(frame, _this.width, _this.height);
}
if (typeof _this.disposeFrame === "function") {
_this.disposeFrame();
}
switch (frame.disposal) {
case 2:
_this.disposeFrame = function() {
return ctx.clearRect(0, 0, canvas.width, canvas.height);
};
break;
case 3:
saved = ctx.getImageData(0, 0, canvas.width, canvas.height);
_this.disposeFrame = function() {
return ctx.putImageData(saved, 0, 0);
};
break;
default:
_this.disposeFrame = null;
}
return (ref = _this.onDrawFrame) != null ? ref.apply(_this, [ctx, frame, i]) : void 0;
};
})(this);
}
this.start();
return this;
};
return Animator;
})();
gifler.Gif = Gif;
gifler.Decoder = Decoder;
gifler.Animator = Animator;
if (typeof window !== "undefined" && window !== null) {
window.gifler = gifler;
}
if (typeof module !== "undefined" && module !== null) {
module.exports = gifler;
}