오윤석

Brush, Text 동작 추가

This diff could not be displayed because it is too large.
1 -import ComponentInterface from "./ComponentInterface" 1 +import ComponentInterface from "./ComponentInterface";
2 -import Color from "./Color" 2 +import Color from "./Color";
3 +import { fabric } from "fabric";
3 4
4 class Brush extends ComponentInterface { 5 class Brush extends ComponentInterface {
5 - constructor(fabricObj) { 6 + constructor(fabricObj) {
6 - super(); 7 + super(fabricObj.path.length);
7 - this.color = new Color(fabricObj.stroke); 8 + this.color = new Color(fabricObj.stroke);
8 - this.paths = fabricObj.path; 9 + this.paths = fabricObj.path;
9 - this.size = fabricObj.strokeWidth; 10 + this.size = fabricObj.strokeWidth;
10 - } 11 + this.position = {
12 + top: fabricObj.top,
13 + left: fabricObj.left,
14 + };
15 + }
16 +
17 + getCurrentFabricObject() {
18 + const paths = this.paths.filter((_, i) => i < this.state.current);
19 + if (paths.length > 0) {
20 + const popCount = paths[paths.length - 1].length - 3;
21 + for (let i = 0; i < popCount; i++) {
22 + paths[paths.length - 1].pop();
23 + }
24 + paths[paths.length - 1][0] = "L";
25 + }
26 + return new fabric.Path(paths, {
27 + top: this.position.top,
28 + left: this.position.left,
29 + stroke: this.color.getRgba(),
30 + strokeWidth: this.size,
31 + });
32 + }
33 +
34 + next() {
35 + return this.addState(10);
36 + }
11 } 37 }
12 38
13 -export default Brush
...\ No newline at end of file ...\ No newline at end of file
39 +export default Brush;
......
1 class Color { 1 class Color {
2 - constructor(colorData) { 2 + constructor(colorData) {
3 - if(colorData[0] == "#") { 3 + if (colorData[0] == "#") {
4 - this.r = parseInt(colorData.substring(1, 3), 16) 4 + this.r = parseInt(colorData.substring(1, 3), 16);
5 - this.g = parseInt(colorData.substring(3, 5), 16) 5 + this.g = parseInt(colorData.substring(3, 5), 16);
6 - this.b = parseInt(colorData.substring(5, 7), 16) 6 + this.b = parseInt(colorData.substring(5, 7), 16);
7 - this.a = 1 7 + this.a = 1;
8 - } else { 8 + } else {
9 - const rgba = colorData.substring(5, colorData.length - 1).split(","); 9 + const rgba = colorData.substring(5, colorData.length - 1).split(",");
10 - this.r = parseInt(rgba[0]); 10 + this.r = parseInt(rgba[0]);
11 - this.g = parseInt(rgba[1]); 11 + this.g = parseInt(rgba[1]);
12 - this.b = parseInt(rgba[2]); 12 + this.b = parseInt(rgba[2]);
13 - this.a = parseFloat(rgba[3]); 13 + this.a = parseFloat(rgba[3]);
14 - } 14 + }
15 - } 15 + }
16 +
17 + getColorCode() {
18 + return `#${this.r.toString(16)}${this.g.toString(16)}${this.b.toString(
19 + 16
20 + )}`;
21 + }
22 +
23 + getRgba() {
24 + return `rgba(${this.r},${this.g},${this.b},${this.a})`;
25 + }
16 } 26 }
17 27
18 -export default Color;
...\ No newline at end of file ...\ No newline at end of file
28 +export default Color;
......
1 class ComponentInterface { 1 class ComponentInterface {
2 + constructor(maxState) {
3 + this.state = {
4 + current: 0,
5 + max: maxState,
6 + };
7 + }
2 8
9 + getCurrentFabricObject() {}
10 +
11 + addState(count = 1) {
12 + if (this.state.current == this.state.max) {
13 + this.state.current++;
14 + } else {
15 + this.state.current = Math.min(this.state.current + count, this.state.max);
16 + }
17 + }
18 +
19 + end() {
20 + return this.state.current == this.state.max + 1;
21 + }
3 } 22 }
4 23
5 -export default ComponentInterface;
...\ No newline at end of file ...\ No newline at end of file
24 +export default ComponentInterface;
......
1 -import ComponentInterface from "./ComponentInterface" 1 +import ComponentInterface from "./ComponentInterface";
2 -import Color from "./Color" 2 +import Color from "./Color";
3 -import Hangul from "hangul-js" 3 +import Hangul from "hangul-js";
4 +import { fabric } from "fabric";
4 5
5 class Text extends ComponentInterface { 6 class Text extends ComponentInterface {
6 - constructor(fabricObj) { 7 + constructor(fabricObj) {
7 - super(); 8 + const textSplited = Hangul.disassemble(fabricObj.text);
8 - this.color = new Color(fabricObj.fill); 9 +
9 - this.text = { 10 + super(textSplited.length);
10 - plain:fabricObj.text, 11 + this.color = new Color(fabricObj.fill);
11 - splited:Hangul.disassemble(fabricObj.text) 12 + this.text = {
12 - }; 13 + plain: fabricObj.text,
13 - this.position = { 14 + splited: textSplited,
14 - top:fabricObj.top, 15 + };
15 - left:fabricObj.left 16 + this.position = {
16 - } 17 + top: fabricObj.top,
17 - this.font = { 18 + left: fabricObj.left,
18 - size:fabricObj.fontSize, 19 + };
19 - style:fabricObj.fontStyle, 20 + this.font = {
20 - weight:fabricObj.fontWeight, 21 + size: fabricObj.fontSize,
21 - family:fabricObj.fontFamily 22 + style: fabricObj.fontStyle,
22 - } 23 + weight: fabricObj.fontWeight,
23 - } 24 + family: fabricObj.fontFamily,
25 + };
26 + }
27 +
28 + getCurrentFabricObject() {
29 + return new fabric.Text(
30 + Hangul.assemble(
31 + this.text.splited.filter((_, i) => i < this.state.current)
32 + ),
33 + {
34 + top: this.position.top,
35 + left: this.position.left,
36 + fontFamily: this.font.family,
37 + fontSize: this.font.size,
38 + fontWeight: this.font.weight,
39 + fontStyle: this.font.style,
40 + fill: this.color.getColorCode(),
41 + }
42 + );
43 + }
44 +
45 + next() {
46 + return this.addState();
47 + }
24 } 48 }
25 49
26 -export default Text
...\ No newline at end of file ...\ No newline at end of file
50 +export default Text;
......
1 -import Brush from "./Brush" 1 +import Brush from "./Brush";
2 -import Text from "./Text" 2 +import Text from "./Text";
3 3
4 const Component = { 4 const Component = {
5 - Brush, 5 + Brush,
6 - Text 6 + Text,
7 }; 7 };
8 8
9 -export default Component;
...\ No newline at end of file ...\ No newline at end of file
9 +export default Component;
......
1 import GIF from "gifencoder"; 1 import GIF from "gifencoder";
2 -import {fabric} from "fabric" 2 +import { fabric } from "fabric";
3 import Component from "./components"; 3 import Component from "./components";
4 4
5 class GifGenerator { 5 class GifGenerator {
...@@ -30,14 +30,21 @@ class GifGenerator { ...@@ -30,14 +30,21 @@ class GifGenerator {
30 make() { 30 make() {
31 const fabricObjs = this.canvas.getObjects(); 31 const fabricObjs = this.canvas.getObjects();
32 const objs = []; 32 const objs = [];
33 - 33 +
34 fabricObjs.map((fabricObj) => { 34 fabricObjs.map((fabricObj) => {
35 - if(fabricObj instanceof fabric.Path) { 35 + if (fabricObj instanceof fabric.Path) {
36 - objs.push(new Component.Brush(fabricObj)) 36 + objs.push(new Component.Brush(fabricObj));
37 - } else if(fabricObj.text !== undefined) { 37 + } else if (fabricObj.text !== undefined) {
38 - objs.push(new Component.Text(fabricObj)) 38 + objs.push(new Component.Text(fabricObj));
39 + }
40 + });
41 +
42 + objs.map((obj) => {
43 + while (!obj.end()) {
44 + console.log(obj.getCurrentFabricObject());
45 + obj.next();
39 } 46 }
40 - }) 47 + });
41 48
42 console.log(objs); 49 console.log(objs);
43 } 50 }
......