-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvgPaths.js
104 lines (79 loc) · 2.61 KB
/
svgPaths.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
!function (name, definition) {
if (typeof module != 'undefined') {
module.exports = definition();
} else if (typeof define != 'undefined') {
define(definition);
} else {
this[name] = definition();
}
}('SvgPaths', function() {
return function(str, option) {
if(!str) return;
// Lets split this SVG string into its commands
var previous_relative_point = [0,0],
previous_absolute_point = [0,0],
sections = str.match(/([mcvhl]{1}[0-9 \,\.z -]+)/gi).map(function(text) {
var cmd = text.match(/^\W*[mcvhl]/ig)[0],
points,
relative_point,
absolute_point,
is_relative = cmd == cmd.toLowerCase(),
has_z = text.match(/z/gi);
/**
*
* Lets get the list of points
*
**/
if(cmd == "v") {
} else if(cmd == "V") {
} else if(cmd == "h") {
} else if(cmd == "H") {
} else {
// Get a list of points from the command
// if(!text.match(/([0-9\.]+[ \,][0-9\.]+)+/ig))
// console.log(text, text.match(/([0-9\\.]+[ \,][0-9\.]+)+/ig));
points = text.match(/([0-9\.\-]+[\W\,][0-9\.\-]+)+/ig).map(function(item) { return item.replace(/,/, " ").split(" ").map(Number); });
}
/**
*
* Lets see if we have a relative or absolute points, then lets find the other one.
*
**/
if(is_relative) {
relative_points = points;
absolute_points = points.map(function(point) {
return [previous_absolute_point[0] + point[0], previous_absolute_point[1] + point[1]]
});
} else {
relative_points = points.map(function(point) {
return [point[0] - previous_absolute_point[0], point[1] - previous_absolute_point[1]]
});
absolute_points = points;
}
/**
*
* Lets rebuild our commands
*
**/
if(cmd.toLowerCase() == "v") {
} else if(cmd.toLowerCase() == "h") {
} else {
relative_text = cmd.toLowerCase() + " " + relative_points.map(function(point) { return point.join(","); }).join(" ") + (has_z ? " z" : "");
absolute_text = cmd.toUpperCase() + " " + absolute_points.map(function(point) { return point.join(","); }).join(" ") + (has_z ? " z" : "");
}
// Lets hold onto this absolute point for the next command.
previous_absolute_point = absolute_points[absolute_points.length-1];
return {
// Lets find the command
cmd: cmd,
// List the points relative or absolute
relative_points: relative_points,
absolute_points: absolute_points,
// Lets save the full text of the command
relative: relative_text,
absolute: absolute_text
};
});
return sections.map(function(item) { return item && item[option]; }).join(" ");
}
})