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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
# svg.js
A lightweight (3.5k gzipped) library for manipulating SVG.
Svg.js is licensed under the terms of the MIT License.
## 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(100, 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="#f06"></rect>
</svg>
</div>
```
By default the svg canvas follows the dimensions of its parent, in this case `#paper`:
```javascript
var draw = svg('paper').size('100%', '100%');
```
## Elements
### Rect
Rects have two arguments, their `width` and `height`:
```javascript
var text = draw.rect(100, 100);
```
### Ellipse
Ellipses, like rects, have two arguments, their `width` and `height`:
```javascript
var ellipse = draw.ellipse(100, 100);
```
### Circle
The only argument necessary for a circle is the diameter:
```javascript
var circle = draw.circle(100);
```
_Note that this generates an `<ellipse>` element instead of a `<circle>`. This choice has been made to keep the size of the library down._
### Polyline
The polyline element defines a set of connected straight line segments. Typically, polyline elements define open shapes:
```javascript
// polyline('x,y x,y x,y')
var polyline = draw.polyline('10,20 30,40 50,60');
```
Polyline strings consist of a list of points separated by spaces: `x,y x,y x,y`.
_Not unlike the `<circle>` element, the svg `<line>` element has not been implemented to keep the library as small as possible. Therefore you might want to use the `polyline()` method to create a single line segment._
### Polygon
The polygon element, unlike the polyline element, defines a closed shape consisting of a set of connected straight line segments:
```javascript
// polygon('x,y x,y x,y')
var polygon = draw.polygon('10,20 30,40 50,60');
```
Polygon strings are exactly the same as polyline strings. There is no need to close the shape as the first and last point will be connected automatically.
### Path
The path string is similar to the polygon string but much more complex in order to support curves:
```javascript
// path('path data')
var path = draw.path('M10,20L30,40');
```
For more details on path data strings, please refer to the SVG documentation:
http://www.w3.org/TR/SVG/paths.html#PathData
### Image
When creating images the `width` and `height` values should be defined:
```javascript
// image(src, width, height)
var image = draw.image('/path/to/image.jpg', 200, 200).move(100, 100);
```
### Text
The first argument of a text element is the actual text content:
```javascript
var text = draw.text("svg\nto\nthe\npoint.").move(300, 0);
```
Changing text afterwards is also possible with the `text()` method:
```javascript
text.text('Brilliant!');
```
To get the raw text content:
```javascript
text.content;
```
The sugar.js module provides some syntax sugar specifically for this element type:
```javascript
text.font({
family: 'Helvetica',
size: 144,
anchor: 'middle',
leading: 1.5
});
```
## 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()` method elements can be scaled, rotated, translated and skewed:
```javascript
rect.transform({
rotation: 45,
cx: 100,
cy: 100
});
```
All available transformations are:
```javascript
rect.transform({
x: [translation on x-axis],
y: [translation on y-axis],
rotation: [degrees],
cx: [x rotation point],
cy: [y rotation point],
scaleX: [scaling on x-axis],
scaleX: [scaling on y-axis],
skewX: [skewing on x-axis],
skewY: [skewing on y-axis]
});
```
Important: matrix transformations are not yet supported.
### 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` and `y` reference differ between element types. For example, rect uses the upper left corner with the `x` and `y` attributes, circle and ellipse use their center with the `cx` and `cy` attributes and thereby simply ignoring the `x` and `y` values you might assign.
### 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.
### Center
This is an extra method to move an element by its center:
```javascript
rect.center(150, 150);
```
### Hide and show
We all love to have a little hide:
```javascript
rect.hide();
```
and show:
```javascript
rect.show();
```
### Removing elements
Pretty straightforward:
```javascript
rect.remove();
```
To remove all elements in the svg document:
```javascript
draw.clear();
```
### Bounding box
```javascript
path.bbox();
```
This will return an object with the following values:
```javascript
{ height: 20, width: 20, y: 20, x: 10, cx: 30, cy: 20 }
```
As opposed to the native `getBBox()` method any translations used with the `transform()` method will be taken into account.
### Iterating over all children
If you would iterate over all the `children()` of the svg document, you might notice also the `<defs>` and `<g>` elements will be included. To iterate the shapes only, you can use the `each()` method:
```javascript
draw.each(function(i, children) {
this.fill({ color: '#f06' });
});
```
## 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 center of the element:
```javascript
// rotate(degrees)
rect.rotate(45);
```
Unless you also define a rotation point:
```javascript
// rotate(degrees, cx, cy)
rect.rotate(45, 100, 100);
```
### Skew
The `skew()` method will take an `x` and `y` value:
```javascript
// skew(x, y)
rect.skew(0, 45);
```
_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.ellipse(80, 40).move(10, 10);
});
```
You can also reuse clip paths for multiple elements using `clipTo()`.
```javascript
var clipPath = draw.defs().clip();
var ellipse = clipPath.ellipse(80, 40).move(10, 10);
rect.clipTo(clipPath);
```
_This functionality requires the clip.js module which is included in the default distribution._
## Masking elements
Masking elements works very much the same as clipping with either `mask()` or `maskTo()`.
Using `mask()` creates a mask in the parents 'defs' node, and passes it to a block:
```javascript
var gradient = draw.gradient('linear', function(stop) {
stop.at({ offset: 0, color: '#000' });
stop.at({ offset: 90, color: '#fff' });
});
rect.mask(function(mask) {
mask.ellipse(80, 40).move(10, 10).fill({ color: gradient.fill() });
});
```
You can also reuse masks for multiple elements using `maskTo()`.
```javascript
var mask = draw.defs().mask();
var ellipse = mask.ellipse(80, 40).move(10, 10).fill({ color: gradient.fill() });
rect.maskTo(mask);
```
_This functionality requires the mask.js module which is included in the default distribution._
## Arranging elements
You can arrange elements within their parent SVG document using the following methods.
Move element to the front:
```javascript
rect.front();
```
Move element to the back:
```javascript
rect.back();
```
Note that `back()` will move the element to position 1, not 0, because the `<defs>` node is already located at position 0.
Move element one step forward:
```javascript
rect.forward();
```
Move element one step backward:
```javascript
rect.backward();
```
The arrange.js module brings some additional methods. To get all siblings of rect, including rect itself:
```javascript
rect.siblings();
```
Get the position (a number) of rect between its siblings:
```javascript
rect.position();
```
Get the next sibling:
```javascript
rect.next();
```
Get the previous sibling:
```javascript
rect.previous();
```
_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('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._
## Gradients
There are linear and radial gradients. The linear gradient can be created like this:
```javascript
var gradient = draw.gradient('linear', function(stop) {
stop.at({ offset: 0, color: '#333', opacity: 1 });
stop.at({ offset: 100, color: '#fff', opacity: 1 });
});
```
The `offset` and `color` parameters are required for stops, `opacity` is optional. Offset is an integer expressed in percentage. To define the direction you can set from `x`, `y` and to `x`, `y`:
```javascript
gradient.from(0, 0).to(0, 100);
```
The from and to values are also expressed in percent.
Finally, to use the gradient on an element:
```javascript
rect.attr({ fill: gradient.fill() });
```
Radial gradients have a `radius()` method to define the outermost radius to where the inner color should develop:
```javascript
var gradient = draw.gradient('radial', function(stop) {
stop.at({ offset: 0, color: '#333', opacity: 1 });
stop.at({ offset: 100, color: '#fff', opacity: 1 });
});
gradient.from(50, 50).to(50, 50).radius(50);
```
A gradient can also be updated afterwards:
```javascript
gradient.update(function(stop) {
stop.at({ offset: 10, color: '#333', opacity: 0.2 });
stop.at({ offset: 90, color: '#f03', opacity: 1 });
});
```
And even a single stop can be updated:
```javascript
var s1, s2, s3;
draw.gradient('radial', function(stop) {
s1 = stop.at({ offset: 0, color: '#000', opacity: 1 });
s2 = stop.at({ offset: 50, color: '#f03', opacity: 1 });
s3 = stop.at({ offset: 100, color: '#066', opacity: 1 });
});
s1.update({ offset: 10, color: '#0f0', opacity: 1 });
```
[W3Schools](http://www.w3schools.com/svg/svg_grad_linear.asp) has a great example page on how
[linear gradients](http://www.w3schools.com/svg/svg_grad_linear.asp) and
[radial gradients](http://www.w3schools.com/svg/svg_grad_radial.asp) work.
_This functionality requires the gradient.js module which is included in the default distribution._
## Events
All usual events are accessible on elements:
```javascript
rect.click(function() {
this.fill({ color: '#f06' });
});
```
Available events are `click`, `dblclick`, `mousedown`, `mouseup`, `mouseover`, `mouseout`, `mousemove`, `touchstart`, `touchend`, `touchmove` and `touchcancel`.
## 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 an ellipse apply a slightly different color:
```javascript
SVG.extend(SVG.Ellipse, {
paintRed: function() {
return this.fill({ color: 'orangered' });
}
});
```
The complete inheritance stack for `SVG.Ellipse` is:
_SVG.Ellipse < 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;
}
});
```
## Building
Starting out with the default distribution of svg.js is good. Although you might want to remove some modules to keep the size at minimum.
You will need ruby, RubyGems, and rake installed on your system.
``` sh
# dependencies:
$ ruby -v
$ gem -v
$ rake -V
# required to generate the minified version:
$ gem install uglifier
```
Build Zepto by running `rake`:
``` sh
$ rake
Original version: 17.010k
Minified: 9.083k
Minified and gzipped: 2.760k, compression factor 6.163
```
The resulting files are:
1. `dist/svg.js`
2. `dist/svg.min.js`
To include optional modules and remove default ones, use the `concat` task. In
this example, 'clip' is removed, but 'group' and 'arrange' are added:
```
$ rake concat[-clip:group:arrange] dist
```
_The Rakefile has been borrowed from [madrobby's](https://github.com/madrobby) [Zepto](https://github.com/madrobby/zepto)_
## 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)
- Text on path module (write text along paths)
- Nested SVG (add a svg document inside another svg document)
## Compatibility
### Desktop
- Firefox 3+
- Chrome 4+
- Safari 3.2+
- Opera 9+
- IE9 +
### Mobile
- iOS Safari 3.2+
- Android Browser 3+
- Opera Mobile 10+
- Chrome for Android 18+
- Firefox for Android 15+
Visit the [svg.js test page](http://svgjs.com/test) if you want to check compatibility with different browsers.
Important: this library is still in alpha, therefore the API might be subject to change in the course of development.
|