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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
# Svg.js
Svg.js is a lightweight (2.5k gzipped) library for manipulating SVG.
Svg.js is licensed under the terms of the MIT License.
Important: this library is still in alpha, therefore the API might be subject to change in the course of development.
## Usage
### Create a SVG document
Use the 'svg()' function to create a SVG document within a given html element:
```javascript
var draw = svg('paper').size(300, 300);
var rect = draw.rect({ width:100, height:100 }).attr({ fill: '#f06' });
```
The first argument can either be an id of the element or the selected element itself.
This will generate the following output:
```html
<div id="paper">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xlink="http://www.w3.org/1999/xlink" width="300" height="300">
<rect width="100" height="100" fill-color="#f06"></rect>
</svg>
</div>
```
### Manipulating elements
#### Attributes
You can get and set an element's attributes directly using 'attr()':
```javascript
// get a single attribute
rect.attr('x');
// set a single attribute
rect.attr('x', 50);
// set multiple attributes at once
rect.attr({
fill: '#f06',
'fill-opacity': 0.5,
stroke: '#000',
'stroke-width': 10
});
// set an attribute with a namespace
rect.attr('x', 50, 'http://www.w3.org/2000/svg');
```
#### Transform
With the transform attribute elements can be scaled, rotated, translated, skewed... :
```javascript
rect.transform('rotate(45, 100, 100)');
```
These operations are always absolute. If every transformation needs remembered, so multiple rotate operations will be stacked together making them relative to previous operations, a boolean value can be passed as a second argument:
```javascript
rect.transform('rotate(45, 100, 100)', true);
```
More details on available transformations can be found here:
http://www.w3.org/TR/SVG/coords.html#TransformAttribute
#### Move
Move the element to a given x and y position by its upper left corner:
```javascript
rect.move(200, 350);
```
Note that you can also use the following code to move elements around:
```javascript
rect.attr({ x: 20, y: 60 });
```
Although 'move()' is much more convenient because it will always use the upper left corner as the position reference, whereas with using 'attr()' the x an y reference differ between element types. For example, rect uses the upper left corner and circle uses the centre.
#### Size
Set the size of an element by a given width and height:
```javascript
rect.size(200, 300);
```
Same as with 'move()' the size of an element could be set by using 'attr()'. But because every type of element is handles its size differently the 'size()' method is much more convenient.
#### Removing elements
Pretty straightforward:
```javascript
rect.remove();
```
### Text element
Text elements can be created with a single style applied.
```javascript
var text = draw.text({
text: "svg\nto\nthe\npoint.",
x: 300,
y: 0
});
```
Styling text can be done using the 'font()' method which is both a setter and a getter:
```javascript
// get a single font property
text.font('size');
// set a single font property
text.font('anchor', 'start');
// set multiple font properties at once
text.font({
family: 'Helvetica',
size: 36,
anchor: 'middle',
leading: 1.5
});
```
Changing text afterwards is also possible with the 'text()' method:
```javascript
text.text('Brilliant!');
```
To get the raw text content:
```javascript
text.content;
```
Setting attributes like fill, opacity, x and y is the same as other elements:
```javascript
text.attr({
fill: '#333',
x: 100
});
```
### Image element
When creating images the width and height values should be defined:
```javascript
var image = draw.image({
width: 200,
height: 200,
x: 100,
y: 100,
src: '/path/to/image.jpg'
});
```
### Path element
```javascript
var path = draw.path({ data: "M10,20L30,40" }).attr({ fill: '#9dffd3' });
```
For more details on path data strings, please read the SVG documentation:
http://www.w3.org/TR/SVG/paths.html#PathData
### Bounding box
```javascript
path.bbox();
```
This will return a SVGRect element as a js object:
```javascript
{ height: 20, width: 20, y: 20, x: 10 }
```
### Syntax sugar
Fill and stroke are used quite often. Therefore two convenience methods are provided:
#### Fill
The 'fill()' method is a pretty alternative to the 'attr()' method:
```javascript
rect.fill({ color: '#f06', opacity: 0.6 });
```
#### Stroke
The 'stroke()' method is similar to 'fill()':
```javascript
rect.stroke({ color: '#f06', opacity: 0.6, width: 5 });
```
#### Rotate
The 'rotate()' method will automatically rotate elements according to the centre of the element:
```javascript
rect.rotate({ deg: 45 });
```
But you also define a rotation point:
```javascript
rect.rotate({ deg: 45, x: 100, y: 100 });
```
To make the operation relative:
```javascript
rect.rotate({ deg: 45, x: 100, y: 100, relative: true });
```
_This functionality requires the sugar.js module which is included in the default distribution._
### Clipping elements
Clipping elements can be done with either 'clip()' or 'clipTo()'.
Using 'clip()' creates a clip path in the parents 'defs' node, and passes it to a block:
```javascript
rect.clip(function(clipPath) {
clipPath.rect({ x:10, y:10, width:80, height:80 });
});
```
You can also reuse clip paths for multiple elements using 'clipTo()'.
```javascript
var clipPath = doc.defs().clipPath();
clipRect = clipPath.rect({ x:10, y:10, width:80, height:80 });
rect.clipTo(clipPath);
```
_This functionality requires the clip.js module which is included in the default distribution._
### Arranging elements
You can arrange elements within their parent SVG document using the following methods:
```javascript
// move element to the front
rect.front();
// move element to the back
rect.back();
// move element one step forward
rect.forward();
// move element one step backward
rect.backward();
```
_This functionality requires the arrange.js module which is included in the default distribution._
### Grouping elements
Grouping elements is useful if you want to transform a set of elements as if it were one. All element within a group maintain their position relative to the group they belong to. A group has all the same element methods as the root svg document:
```javascript
var group = draw.group();
group.path({ data: "M10,20L30,40" });
```
Existing elements from the svg document can also be added to a group:
```javascript
group.add(rect);
```
_This functionality requires the group.js module which is included in the default distribution._
## Extending functionality
Svg.js has a modular structure. It is very easy to add you own methods at different levels. Let's say we want to add a method to all shape types then we would add our method to SVG.Shape:
```javascript
SVG.extend(SVG.Shape, {
paintRed: function() {
return this.fill({ color: 'red' });
}
});
```
Now all shapes will have the paintRed method available. Say we want to have the paintRed method on a circle apply a slightly different color:
```javascript
SVG.extend(SVG.Circle, {
paintRed: function() {
return this.fill({ color: 'orangered' });
}
});
```
The complete inheritance stack for 'SVG.Circle' is:
_SVG.Circle < SVG.Shape < SVG.Element_
The SVG document can be extended by using:
```javascript
SVG.extend(SVG.Doc, {
paintAllPink: function() {
var children = this.children();
for (var i = 0, l = children.length; i < l; i++) {
children[i].fill({ color: 'pink' });
};
return this;
}
});
```
## To-do
- Animation module (element animation, path tweens and easing)
- Draggable module (make elements and groups draggable)
- Shapes module (add preset shapes like star, n-gon)
## Compatibility
### Desktop
- Firefox 3+
- Chrome 4+
- Safari 3.2+
- Opera 9+
- IE 9+
### Mobile
- iOS Safari 3.2+
- Android Browser 3+
- Blackberry 7+
- Opera Mini 5+
- Opera Mobile 10+
- Chrome for Android 18+
- Firefox for Android 15+
|