-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.js
161 lines (150 loc) · 4.6 KB
/
Path.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @file 蜡烛图path函数
* @author liuliang<[email protected]>
*/
import _ from 'lodash';
export default function path(instructions = []) {
let push = (arr, el) => [...arr, el];
let areEqualPoints = ([a1, b1], [a2, b2]) => (a1 === a2) && (b1 === b2);
let trimZeros = (string) => {
return parseFloat(string) + '';
};
let round = (number, digits) => {
const str = number.toFixed(digits);
return trimZeros(str);
};
let printInstrunction = ({ command, params }) => {
let numbers = params.map((param) => round(param, 6));
return `${ command } ${ numbers.join(' ') }`;
};
let point = ({ command, params }, [prevX, prevY]) => {
switch (command) {
case 'M':
return [params[0], params[1]];
case 'L':
return [params[0], params[1]];
case 'H':
return [params[0], prevY];
case 'V':
return [prevX, params[0]];
case 'Z':
return null;
case 'C':
return [params[4], params[5]];
case 'S':
return [params[2], params[3]];
case 'Q':
return [params[2], params[3]];
case 'T':
return [params[0], params[1]];
case 'A':
return [params[5], params[6]];
}
};
let verbosify = (keys, f) =>
function (...arg) {
let a = arg[0];
let args = (_.isPlainObject(a)) ? keys.map((k) => a[k]) : arg;
return f.apply(null, args);
};
let plus = (instruction) => path(push(instructions, instruction));
return ({
moveto: verbosify(['x', 'y'], (x, y) =>
plus({
command: 'M',
params: [x, y]
})
),
lineto: verbosify(['x', 'y'], (x, y) =>
plus({
command: 'L',
params: [x, y]
})
),
hlineto: verbosify(['x'], (x) =>
plus({
command: 'H',
params: [x]
})
),
vlineto: verbosify(['y'], (y) =>
plus({
command: 'V',
params: [y]
})
),
closepath() {
return plus({
command: 'Z',
params: []
});
},
curveto: verbosify(['x1', 'y1', 'x2', 'y2', 'x', 'y'], (x1, y1, x2, y2, x, y) =>
plus({
command: 'C',
params: [x1, y1, x2, y2, x, y]
})
),
smoothcurveto: verbosify(['x2', 'y2', 'x', 'y'], (x2, y2, x, y) =>
plus({
command: 'S',
params: [x2, y2, x, y]
})
),
qcurveto: verbosify(['x1', 'y1', 'x', 'y'], (x1, y1, x, y) =>
plus({
command: 'Q',
params: [x1, y1, x, y]
})
),
smoothqcurveto: verbosify(['x', 'y'], (x, y) =>
plus({
command: 'T',
params: [x, y]
})
),
arc: verbosify(['rx', 'ry', 'xrot', 'largeArcFlag', 'sweepFlag', 'x', 'y'],
({rx, ry, xrot, largeArcFlag, sweepFlag, x, y}) =>
// 注意.eslintrc允许参数最大个数为6个,所以只要用解构来跳过
plus({
command: 'A',
params: [rx, ry, xrot, largeArcFlag, sweepFlag, x, y]
})
),
print() {
return instructions.map(printInstrunction).join(' ');
},
toString() {
return this.print();
},
points() {
let ps = [];
let prev = [0, 0];
for (let instruction of instructions) {
let p = point(instruction, prev);
prev = p;
if (p) {
ps.push(p);
}
}
return ps;
},
instructions() {
return instructions.slice(0, instructions.length);
},
connect(path) {
let ps = this.points();
let last = ps[ps.length - 1];
// console.log(path);
let first = path.points()[0];
let newInstructions = path.instructions().slice(1);
if (!areEqualPoints(last, first)) {
newInstructions.unshift({
command: 'L',
params: first
});
}
return path(this.instructions().concat(newInstructions));
}
});
}