-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsvg-path.js
240 lines (221 loc) · 6.4 KB
/
svg-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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* SvgPath
* Chainable SVG path string generator with some sugar added
* Supports Node, AMD and browser environments (EcmaScript 5+ or shims)
* No dependencies
* @version 0.2.1
* @author Igor Zalutsky
* @license MIT
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.SvgPath = factory();
}
}(this, function () {
"use strict";
var absCommands = ['M', 'Z', 'L', 'H', 'V', 'C', 'S', 'Q', 'T', 'A'];
var relCommands = absCommands.map(function(letter){
return letter.toLowerCase();
});
var commands = absCommands.concat(relCommands);
/**
* Creates a path builder. Can be invoked without new.
* @returns {SvgPath}
* @constructor
*/
function SvgPath(){
//TODO is this check robust enough?
if (this instanceof SvgPath){
this.relative = false;
this.commands = [];
} else {
return new SvgPath();
}
}
/**
* Turns relative mode on (lowercase commands will be used)
* @returns {SvgPath}
*/
SvgPath.prototype.rel = function(){
this.relative = true;
return this;
};
/**
* Turns relative mode off (uppercase commands will be used)
* @returns {SvgPath}
*/
SvgPath.prototype.abs = function(){
this.relative = false;
return this;
};
/**
* Closes subpath (Z command)
* @returns {SvgPath}
*/
SvgPath.prototype.close = function(){
return this.Z();
};
/**
* Moves pen (M or m command)
* Also accepts point, i.e. { x: 10, y: 20 }
* @param x
* @param y
* @returns {SvgPath}
*/
SvgPath.prototype.to = function(x, y){
var point = (typeof x === 'object') ? x : { x: x, y: y };
return this._cmd('M')(point.x, point.y);
};
/**
* Draws line (L or l command)
* Also accepts point, i.e. { x: 10, y: 20 }
* @param x
* @param y
* @returns {SvgPath}
*/
SvgPath.prototype.line = function(x, y){
var point = (typeof x === 'object') ? x : { x: x, y: y };
return this._cmd('L')(point.x, point.y);
};
/**
* Draws horizontal line (H or h command)
* @param x
* @returns {SvgPath}
*/
SvgPath.prototype.hline = function(x){
return this._cmd('H')(x);
};
/**
* Draws vertical line (V or v command)
* @param y
* @returns {SvgPath}
*/
SvgPath.prototype.vline = function(y){
return this._cmd('V')(y);
};
/**
* Draws cubic bezier curve (C or c command)
* Also accepts 2 or 3 points, i.e. { x: 10, y: 20 }
* If last point is omitted, acts like shortcut (S or s command)
* @param x1
* @param y1
* @param x2
* @param y2
* @param x
* @param y
* @returns {SvgPath}
*/
SvgPath.prototype.bezier3 = function(x1, y1, x2, y2, x, y){
var usePoints = (typeof x1 === 'object');
var shortcut = usePoints ? arguments.length < 3 : arguments.length < 6;
var p1 = { x: x1, y: y1 };
var p2 = { x: x2, y: y2 };
var end = shortcut ? p2 : { x: x, y: y };
if (usePoints){
p1 = x1;
p2 = y1;
end = shortcut ? p2 : x2;
}
if (!shortcut) {
return this._cmd('C')(p1.x, p1.y, p2.x, p2.y, end.x, end.y);
} else {
return this._cmd('S')(p1.x, p1.y, end.x, end.y);
}
};
/**
* Draws quadratic bezier curve (Q or q command)
* Also accepts 1 or 2 points, i.e. { x: 10, y: 20 }
* If last point is omitted, acts like shortcut (T or t command)
* @param x1
* @param y1
* @param x
* @param y
* @returns {SvgPath}
*/
SvgPath.prototype.bezier2 = function(x1, y1, x, y){
var usePoints = (typeof x1 === 'object');
var shortcut = usePoints ? arguments.length < 2 : arguments.length < 4;
var p1 = { x: x1, y: y1 };
var end = shortcut ? p1 : { x: x, y: y };
if (usePoints){
p1 = x1;
end = shortcut ? p1 : y1;
}
if (!shortcut) {
return this._cmd('Q')(p1.x, p1.y, end.x, end.y);
} else {
return this._cmd('T')(end.x, end.y);
}
};
/**
* Draws an arc (A or a command)
* Also accepts end point, i.e. { x: 10, y: 20 }
* @param rx
* @param ry
* @param rotation
* @param large
* @param sweep
* @param x
* @param y
* @returns {*}
*/
SvgPath.prototype.arc = function(rx, ry, rotation, large, sweep, x, y){
var point = (typeof x === 'object') ? x : { x: x, y: y };
return this._cmd('A')(rx, ry, rotation, large, sweep, point.x, point.y);
};
/**
* String representation of command chain
* @returns {string}
*/
SvgPath.prototype.str = function(){
return this.commands.map(function(command){
return command.toString();
}).join(' ');
};
//setting letter commands
commands.forEach(function(commandName){
SvgPath.prototype[commandName] = function(){
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(commandName);
var command = new Command(args);
this.commands.push(command);
return this;
};
});
/**
* Gets either absolute (uppercase) or relative (lowercase) version of command depending on mode
* @param letter
* @returns {function}
* @private
*/
SvgPath.prototype._cmd = function(letter){
var actualName = this.relative ?
letter.toLowerCase() : letter.toUpperCase();
//TODO maybe direct invokation is better than binding?
return this[actualName].bind(this);
};
/**
* Represents a single command
* @param name
* @constructor
*/
function Command(name){
//TODO more robust array detection
var args = name.length > 0 && name.slice ?
name : Array.prototype.slice.call(arguments, 0);
this.name = args[0];
this.args = args.slice(1);
}
/**
* String representation of a command
* @returns {string}
*/
Command.prototype.toString = function(){
return this.name + ' ' + this.args.join(' ');
};
return SvgPath;
}));