오윤석

gif render

...@@ -7,12 +7,16 @@ class GifGenerator { ...@@ -7,12 +7,16 @@ class GifGenerator {
7 this.canvas = canvas; 7 this.canvas = canvas;
8 this.width = canvas.getWidth(); 8 this.width = canvas.getWidth();
9 this.height = canvas.getHeight(); 9 this.height = canvas.getHeight();
10 - this.gif = new GIF(this.width, this.height);
11 10
12 - this.gif.start(); 11 + this._initializeGif();
12 + }
13 +
14 + _initializeGif() {
15 + this.gif = new GIF(this.width, this.height);
13 this.gif.setTransparent(null); 16 this.gif.setTransparent(null);
14 this.gif.setRepeat(0); 17 this.gif.setRepeat(0);
15 this.gif.setQuality(10); 18 this.gif.setQuality(10);
19 + this.gif.start();
16 } 20 }
17 21
18 _addFrame(delay = 0) { 22 _addFrame(delay = 0) {
...@@ -28,6 +32,8 @@ class GifGenerator { ...@@ -28,6 +32,8 @@ class GifGenerator {
28 } 32 }
29 33
30 make() { 34 make() {
35 + this._initializeGif();
36 +
31 const fabricObjs = this.canvas.getObjects(); 37 const fabricObjs = this.canvas.getObjects();
32 const objs = []; 38 const objs = [];
33 39
...@@ -41,33 +47,41 @@ class GifGenerator { ...@@ -41,33 +47,41 @@ class GifGenerator {
41 } 47 }
42 }); 48 });
43 49
44 - if (objs.length > 0) { 50 + return new Promise((resolve, reject) => {
45 - let objIdx = 0; 51 + if (objs.length > 0) {
46 - let isAddMode = true; 52 + let objIdx = 0;
47 - const draw = () => { 53 + let isAddMode = true;
48 - const obj = objs[objIdx]; 54 + const draw = () => {
49 - if (isAddMode) { 55 + const obj = objs[objIdx];
50 - const fabricObj = obj.getCurrentFabricObject(); 56 + if (isAddMode) {
51 - obj.next(); 57 + const fabricObj = obj.getCurrentFabricObject();
52 - if (obj.end()) { 58 + obj.next();
53 - if (objIdx < objs.length - 1) objIdx++;
54 - else this.canvas.off("after:render", draw);
55 - } else {
56 isAddMode = false; 59 isAddMode = false;
60 + this.canvas.add(fabricObj);
61 + } else {
62 + this._addFrame(1);
63 + isAddMode = true;
64 + if (obj.end()) {
65 + if (objIdx < objs.length - 1) {
66 + objIdx++;
67 + draw();
68 + } else {
69 + this.canvas.off("after:render", draw);
70 + resolve(this._render());
71 + }
72 + } else {
73 + this.canvas.remove(
74 + this.canvas._objects[this.canvas._objects.length - 1]
75 + );
76 + }
57 } 77 }
58 - this.canvas.add(fabricObj); 78 + };
59 - } else { 79 + this.canvas.on("after:render", draw);
60 - this.canvas.remove( 80 + draw();
61 - this.canvas._objects[this.canvas._objects.length - 1] 81 + } else {
62 - ); 82 + reject(new Error("no objects"));
63 - isAddMode = true; 83 + }
64 - } 84 + });
65 - };
66 - this.canvas.on("after:render", draw);
67 - draw();
68 - }
69 -
70 - console.log(objs);
71 } 85 }
72 } 86 }
73 87
......