forked from hughsk/gl-gif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (61 loc) · 2.03 KB
/
index.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
var GIFEncoder = require('./vendor/GIFEncoder')
var getPixels = require('canvas-pixels')
var tab64 = require('tab64')
var noop = (function(){})
module.exports = GIF
function GIF(gl, opts) {
if (!(this instanceof GIF)) return new GIF(gl, opts)
this.gl = gl
opts = opts || {}
var canvas = this.canvas = gl.canvas
var width = this.width = opts.width || canvas.width
var height = this.height = opts.height || canvas.height
var encoder = this.encoder = new GIFEncoder(width, height)
this._done = false
canvas.width = width
canvas.height = height
if (opts.dither) encoder.setDither(opts.dither)
encoder.setRepeat(opts.repeat || 0)
encoder.setFrameRate(opts.fps || 30)
encoder.setTransparent(opts.transparent || null)
encoder.setQuality(opts.quality || 10)
encoder.writeHeader()
}
GIF.prototype.tick = function() {
if (this._done) return false
if (this.canvas.width !== this.width) {
throw new Error('You cannot change the canvas width while recording')
}
if (this.canvas.height !== this.height) {
throw new Error('You cannot change the canvas height while recording')
}
var pixels = getPixels(this.gl)
var flipped = new Uint8Array(pixels.length)
for (var x = 0; x < this.width; x++)
for (var y = 0; y < this.height; y++) {
var i = 4 * (x + this.width * y)
var j = 4 * (x + this.width * (this.height - y - 1))
flipped[i++] = pixels[j++]
flipped[i++] = pixels[j++]
flipped[i++] = pixels[j++]
flipped[i ] = pixels[j ]
}
this.encoder.addFrame(flipped)
return true
}
GIF.prototype.done = function(opts) {
if (this._done) throw new Error('You may only encode a GIF once')
this._done = true
this.encoder.finish()
var data = this.encoder.stream().getData()
if (opts && opts.dataURI) {
return 'data:image/gif;base64,' + btoa(data)
}
var raw = tab64.decode(btoa(data), 'float32')
if (opts && opts.format === 'raw') {
return raw
}
var blob = new Blob([raw], { type: 'image/gif' })
var url = URL.createObjectURL(blob)
return url
}