UNPKG

3.4 kBJavaScriptView Raw
1/* eslint-disable prefer-template,complexity */
2'use strict';
3
4const svgShapesToPath = {
5 rectToPath: svgShapesToPathRectToPath,
6 polylineToPath: svgShapesToPathPolylineToPath,
7 lineToPath: svgShapesToPathLineToPath,
8 circleToPath: svgShapesToPathCircleToPath,
9 polygonToPath: svgShapesToPathPolygonToPath,
10};
11
12module.exports = svgShapesToPath;
13
14// Shapes helpers (should also move elsewhere)
15function svgShapesToPathRectToPath(attributes) {
16 const x = 'undefined' !== typeof attributes.x ? parseFloat(attributes.x) : 0;
17 const y = 'undefined' !== typeof attributes.y ? parseFloat(attributes.y) : 0;
18 const width =
19 'undefined' !== typeof attributes.width ? parseFloat(attributes.width) : 0;
20 const height =
21 'undefined' !== typeof attributes.height
22 ? parseFloat(attributes.height)
23 : 0;
24 const rx =
25 'undefined' !== typeof attributes.rx
26 ? parseFloat(attributes.rx)
27 : 'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry) : 0;
28 const ry =
29 'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry) : rx;
30
31 return (
32 '' +
33 // start at the left corner
34 'M' +
35 (x + rx) +
36 ' ' +
37 y +
38 // top line
39 'h' +
40 (width - rx * 2) +
41 // upper right corner
42 (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry : '') +
43 // Draw right side
44 'v' +
45 (height - ry * 2) +
46 // Draw bottom right corner
47 (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry : '') +
48 // Down the down side
49 'h' +
50 (width - rx * 2) * -1 +
51 // Draw bottom right corner
52 (rx || ry
53 ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry * -1
54 : '') +
55 // Down the left side
56 'v' +
57 (height - ry * 2) * -1 +
58 // Draw bottom right corner
59 (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry * -1 : '') +
60 // Close path
61 'z'
62 );
63}
64
65function svgShapesToPathPolylineToPath(attributes) {
66 return 'M' + attributes.points;
67}
68
69function svgShapesToPathLineToPath(attributes) {
70 // Move to the line start
71 return (
72 '' +
73 'M' +
74 (parseFloat(attributes.x1) || 0).toString(10) +
75 ' ' +
76 (parseFloat(attributes.y1) || 0).toString(10) +
77 ' ' +
78 ((parseFloat(attributes.x1) || 0) + 1).toString(10) +
79 ' ' +
80 ((parseFloat(attributes.y1) || 0) + 1).toString(10) +
81 ' ' +
82 ((parseFloat(attributes.x2) || 0) + 1).toString(10) +
83 ' ' +
84 ((parseFloat(attributes.y2) || 0) + 1).toString(10) +
85 ' ' +
86 (parseFloat(attributes.x2) || 0).toString(10) +
87 ' ' +
88 (parseFloat(attributes.y2) || 0).toString(10) +
89 'Z'
90 );
91}
92
93function svgShapesToPathCircleToPath(attributes) {
94 const cx = parseFloat(attributes.cx);
95 const cy = parseFloat(attributes.cy);
96 const rx =
97 'undefined' !== typeof attributes.rx
98 ? parseFloat(attributes.rx)
99 : parseFloat(attributes.r);
100 const ry =
101 'undefined' !== typeof attributes.ry
102 ? parseFloat(attributes.ry)
103 : parseFloat(attributes.r);
104
105 // use two A commands because one command which returns to origin is invalid
106 return (
107 '' +
108 'M' +
109 (cx - rx) +
110 ',' +
111 cy +
112 'A' +
113 rx +
114 ',' +
115 ry +
116 ' 0,0,0 ' +
117 (cx + rx) +
118 ',' +
119 cy +
120 'A' +
121 rx +
122 ',' +
123 ry +
124 ' 0,0,0 ' +
125 (cx - rx) +
126 ',' +
127 cy
128 );
129}
130
131function svgShapesToPathPolygonToPath(attributes) {
132 return 'M' + attributes.points + 'Z';
133}