index.js
901 Bytes
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
import GIF from "./lib/GIFEncoder";
class GifGenerator {
constructor(canvas) {
this.canvas = canvas;
this.width = canvas.getWidth();
this.height = canvas.getHeight();
this.gif = new GIF(this.width, this.height);
this.gif.writeHeader();
this.gif.setTransparent(null);
this.gif.setRepeat(0);
this.gif.setQuality(10);
this.gif.setDither(false);
this.gif.setGlobalPalette(false);
}
addFrame(delay = 0) {
this.gif.setDelay(delay);
this.gif.addFrame(
this.canvas.getContext().getImageData(0, 0, this.width, this.height).data
);
}
render() {
this.gif.finish();
const stream = this.gif.stream();
let bytes = [];
stream.pages.map((page) => {
bytes = bytes.concat([...page]);
});
bytes = new Uint8Array(bytes);
return new Blob([bytes], { type: "image/gif" });
}
}
window.GifGenerator = GifGenerator;