index.js
1.82 KB
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
import GIF from "gifencoder";
import { fabric } from "fabric";
import Component from "./components";
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.start();
this.gif.setTransparent(null);
this.gif.setRepeat(0);
this.gif.setQuality(10);
}
_addFrame(delay = 0) {
this.gif.setDelay(delay);
this.gif.addFrame(this.canvas.getContext());
}
_render() {
this.gif.finish();
const byte = new Uint8Array(this.gif.out.data);
return new Blob([byte], { type: "image/gif" });
}
make() {
const fabricObjs = this.canvas.getObjects();
const objs = [];
fabricObjs.map((fabricObj) => {
if (fabricObj instanceof fabric.Path) {
objs.push(new Component.Brush(fabricObj));
this.canvas.remove(fabricObj);
} else if (fabricObj.text !== undefined) {
objs.push(new Component.Text(fabricObj));
this.canvas.remove(fabricObj);
}
});
if (objs.length > 0) {
let objIdx = 0;
let isAddMode = true;
const draw = () => {
const obj = objs[objIdx];
if (isAddMode) {
const fabricObj = obj.getCurrentFabricObject();
obj.next();
if (obj.end()) {
if (objIdx < objs.length - 1) objIdx++;
else this.canvas.off("after:render", draw);
} else {
isAddMode = false;
}
this.canvas.add(fabricObj);
} else {
this.canvas.remove(
this.canvas._objects[this.canvas._objects.length - 1]
);
isAddMode = true;
}
};
this.canvas.on("after:render", draw);
draw();
}
console.log(objs);
}
}
window.GifGenerator = GifGenerator;