UNPKG

2.78 kBJavaScriptView Raw
1export default class SvgPath {
2
3 constructor(startX, startY, path, endX, endY) {
4 this.pathSB = [];
5 this.startX = startX;
6 this.startY = startY;
7 this.pathSB.push(path);
8 this.endX = endX;
9 this.endY = endY;
10 }
11
12 addPath(svgPath) {
13 const x1 = svgPath.startX;
14 const y1 = svgPath.startY;
15 const path = svgPath.getPath();
16 const x2 = svgPath.endX;
17 const y2 = svgPath.endY;
18 if (x1 != this.endX || y1 != this.endY) {
19 if (x1 == this.endX && y1 == this.endY + 1) {
20 this.pathSB.push("v", y1 - y2);
21 } else if (y1 == this.endY && x1 == this.endX + 1) {
22 this.pathSB.push("h", x1 - x2);
23 } else {
24 this.pathSB.push("m", x1 - this.endX);
25 if (y1 - this.endY >= 0) {
26 this.pathSB.push(" ");
27 }
28 this.pathSB.push(y1 - this.endY);
29 }
30 }
31 this.pathSB.push(path);
32 this.endX = x2;
33 this.endY = y2;
34 }
35
36 addLine(svgLine) {
37 const x1 = svgLine.getX1();
38 const y1 = svgLine.getY1();
39 const x2 = svgLine.getX2();
40 const y2 = svgLine.getY2();
41 if (x1 == x2 && this.endX == x1) {
42 if (this.endY == y1 || this.endY == y1 - 1) {
43 this.pathSB.push("v", y2 - this.endY);
44 this.endY = y2;
45 return;
46 }
47 if (this.endY == y2 || this.endY == y2 + 1) {
48 this.pathSB.push("v", y1 - this.endY);
49 this.endY = y1;
50 return;
51 }
52 } else if (y1 == y2 && this.endY == y1) {
53 if (this.endX == x1 || this.endX == x1 - 1) {
54 this.pathSB.push("h", x2 - this.endX);
55 this.endX = x2;
56 return;
57 }
58 if (this.endX == x2 || this.endX == x2 + 1) {
59 this.pathSB.push("h", x1 - this.endX);
60 this.endX = x1;
61 return;
62 }
63 }
64 this.pathSB.push("m", x1 - this.endX);
65 if (y1 - this.endY >= 0) {
66 this.pathSB.push(" ");
67 }
68 this.pathSB.push(y1 - this.endY);
69 if (x1 == x2) {
70 this.pathSB.push("v", y2 - y1);
71 } else if (y1 == y2) {
72 this.pathSB.push("h", x2 - x1);
73 } else {
74 this.pathSB.push("l", x2 - x1);
75 if (y2 - y1 >= 0) {
76 this.pathSB.push(" ");
77 }
78 this.pathSB.push(y2 - y1);
79 }
80 this.endX = x2;
81 this.endY = y2;
82 }
83
84 getPath() {
85 return this.pathSB.join("");
86 }
87
88}
\No newline at end of file