-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmp-segmentize.js
50 lines (40 loc) · 995 Bytes
/
tmp-segmentize.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
var quadratic = require('adaptive-quadratic-curve');
module.exports = segmentizePath;
var segmentCache = {};
function segmentizePath(commands) {
var polygons = [];
var segments = [];
commands.forEach(function(command, i) {
switch (command.type) {
case 'M':
if (segments.length > 1) {
polygons.push(segments);
segments = [];
} else {
segments.push([command.x, command.y]);
}
break;
case 'Q':
Array.prototype.push.apply(segments, quadratic(
[commands[i-1].x, commands[i-1].y],
[command.x1, command.y1],
[command.x, command.y],
1
));
case 'L':
segments.push([command.x, command.y]);
break;
case 'Z':
if (segments.length) {
polygons.push(segments);
segments = [];
}
break;
}
});
if (segments.length) {
polygons.push(segments);
segments = [];
}
return polygons;
}