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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
|
# svg.js
A lightweight library for manipulating and animating SVG.
Svg.js has no dependencies and aims to be as small as possible.
Svg.js is licensed under the terms of the MIT License.
See [svgjs.com](http://svgjs.com) for an introduction, [documentation](http://documentup.com/wout/svg.js) and [some action](http://svgjs.com/test).
[![Wout on Gittip](http://files.wout.co.uk/github/gittip.png)](https://www.gittip.com/wout/)
## Usage
### Create a SVG document
Use the `SVG()` function to create a SVG document within a given html element:
```javascript
var draw = SVG('drawing').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="drawing">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns: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 drawing follows the dimensions of its parent, in this case `#drawing`:
```javascript
var draw = SVG('drawing').size('100%', '100%')
```
### Checking for SVG support
By default this library assumes the client's browser supports SVG. You can test support as follows:
```javascript
if (SVG.supported) {
var draw = SVG('drawing')
var rect = draw.rect(100, 100)
} else {
alert('SVG not supported')
}
```
### SVG document
Svg.js also works outside of the HTML DOM, inside an SVG document for example:
```xml
<?xml version="1.0" encoding="utf-8" ?>
<svg id="drawing" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" >
<script type="text/javascript" xlink:href="svg.min.js"></script>
<script type="text/javascript">
<![CDATA[
var draw = SVG('drawing')
draw.rect(100,100).animate().fill('#f03').move(100,100)
]]>
</script>
</svg>
```
### Sub pixel offset fix
By default sub pixel offset won't be corrected. To enable it, call the `fixSubPixelOffset()` method:
```javascript
var draw = SVG('drawing').fixSubPixelOffset()
```
## Parent elements
### Main svg document
The main svg.js initializer function creates a root svg node in the given element and retuns an instance of `SVG.Doc`:
```javascript
var draw = SVG('drawing')
```
_Javascript inheritance stack: `SVG.Doc` < `SVG.Container` < `SVG.Parent`_
### Nested svg
With this feature you can nest svg documents within each other. Nested svg documents have exactly the same features as the main, top-level svg document:
```javascript
var nested = draw.nested()
var rect = nested.rect(200, 200)
```
_Javascript inheritance stack: `SVG.Nested` < `SVG.Container` < `SVG.Parent`_
### Groups
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)
```
_Javascript inheritance stack: `SVG.G` < `SVG.Container` < `SVG.Parent`_
### Hyperlink
A hyperlink or `<a>` tag creates a container that enables a link on all children:
```javascript
var link = draw.link('http://svgjs.com')
var rect = link.rect(100, 100)
```
The link url can be updated with the `to()` method:
```javascript
link.to('http://apple.com')
```
Furthermore, the link element has a `show()` method to create the `xlink:show` attribute:
```javascript
link.show('replace')
```
And the `target()` method to create the `target` attribute:
```javascript
link.target('_blank')
```
Elements can also be linked the other way around with the `linkTo()` method:
```javascript
rect.linkTo('http://svgjs.com')
```
Alternatively a block can be passed instead of a url for more options on the link element:
```javascript
rect.linkTo(function(link) {
link.to('http://svgjs.com').target('_blank')
})
```
_Javascript inheritance stack: `SVG.A` < `SVG.Container` < `SVG.Parent`_
### Defs
The `<defs>` element is a container element for referenced elements. Elements that are descendants of a ‘defs’ are not rendered directly. The `<defs>` node lives in the main `<svg>` document and can be accessed with the `defs()` method:
```javascript
var defs = draw.defs()
```
The defs are also availabel on any other element through the `doc()` method:
```javascript
var defs = rect.doc().defs()
```
The defs node works exactly the same as groups.
_Javascript inheritance stack: `SVG.Defs` < `SVG.Container` < `SVG.Parent`_
## Elements
### Rect
Rects have two arguments, their `width` and `height`:
```javascript
var rect = draw.rect(100, 100)
```
Rects can also have rounded corners:
```javascript
rect.radius(10)
```
This will set the `rx` and `ry` attributes to `10`. To set `rx` and `ry` individually:
```javascript
rect.radius(10, 20)
```
_Javascript inheritance stack: `SVG.Rect` < `SVG.Shape` < `SVG.Element`_
### Ellipse
Ellipses, like rects, have two arguments, their `width` and `height`:
```javascript
var ellipse = draw.ellipse(200, 100)
```
_Javascript inheritance stack: `SVG.Ellipse` < `SVG.Shape` < `SVG.Element`_
### Circle
The only argument necessary for a circle is the diameter:
```javascript
var circle = draw.circle(100)
```
_Javascript inheritance stack: `SVG.Ellipse` < `SVG.Shape` < `SVG.Element`_
_Note that this generates an `<ellipse>` element instead of a `<circle>`. This choice has been made to keep the size of the library down._
### Line
The line element always takes four arguments, `x1`, `y1`, `x2` and `y2`:
```javascript
var line = draw.line(0, 0, 100, 150).stroke({ width: 1 })
```
Updating a line is done with the `plot()` method:
```javascript
line.plot(50, 30, 100, 150)
```
_Javascript inheritance stack: `SVG.Line` < `SVG.Shape` < `SVG.Element`_
### 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('0,0 100,50 50,100').fill('none').stroke({ width: 1 })
```
Polyline strings consist of a list of points separated by spaces: `x,y x,y x,y`.
As an alternative an array of points will work as well:
```javascript
// polyline([[x,y], [x,y], [x,y]])
var polyline = draw.polyline([[0,0], [100,50], [50,100]]).fill('none').stroke({ width: 1 })
```
Polylines can be updated using the `plot()` method:
```javascript
polyline.plot([[0,0], [100,50], [50,100], [150,50], [200,50]])
```
The `plot()` method can also be animated:
```javascript
polyline.animate(3000).plot([[0,0], [100,50], [50,100], [150,50], [200,50], [250,100], [300,50], [350,50]])
```
_Javascript inheritance stack: `SVG.Polyline` < `SVG.Shape` < `SVG.Element`_
### 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('0,0 100,50 50,100').fill('none').stroke({ width: 1 })
```
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.
_Javascript inheritance stack: `SVG.Polygon` < `SVG.Shape` < `SVG.Element`_
### Path
The path string is similar to the polygon string but much more complex in order to support curves:
```javascript
var path = draw.path('M10,20L30,40')
```
Paths can be updated using the `plot()` method:
```javascript
path.plot('M100,200L300,400')
```
_Javascript inheritance stack: `SVG.Path` < `SVG.Shape` < `SVG.Element`_
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)
```
_Javascript inheritance stack: `SVG.Image` < `SVG.Shape` < `SVG.Element`_
### Text
Unlike html, text in svg is much harder to tame. There is no way to create flowing text, so newlines should be entered manually. In svg.js there are two ways to create text elements.
The first and easiest method is to provide a string of text, split by newlines:
```javascript
var text = draw.text("Lorem ipsum dolor sit amet consectetur.\nCras sodales imperdiet auctor.")
```
This will automatically create a block of text and insert newlines where necessary.
The second method will give you much more control but requires a bit more code:
```javascript
var text = draw.text(function(add) {
add.tspan('Lorem ipsum dolor sit amet ').newLine()
add.tspan('consectetur').fill('#f06')
add.tspan('.')
add.tspan('Cras sodales imperdiet auctor.').newLine().dx(20)
add.tspan('Nunc ultrices lectus at erat').newLine()
add.tspan('dictum pharetra elementum ante').newLine()
})
```
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.5em'
})
```
_Javascript inheritance stack: `SVG.Text` < `SVG.Shape` < `SVG.Element`_
### TextPath
A nice feature in svg is the ability to run text along a path:
```javascript
var text = draw.text(function(add) {
add.tspan('We go ')
add.tspan('up').fill('#f09').dy(-40)
add.tspan(', then we go down, then up again').dy(40)
})
text
.path('M 100 200 C 200 100 300 0 400 100 C 500 200 600 300 700 200 C 800 100 900 100 900 100')
.font({ size: 42.5, family: 'Verdana' })
```
When calling the `path()` method on a text element, the text element is mutated into an intermediate between a text and a path element. From that point on the text element will also feature a `plot()` method to update the path:
```javascript
text.plot('M 300 500 C 200 100 300 0 400 100 C 500 200 600 300 700 200 C 800 100 900 100 900 100')
```
Attributes specific to the `<textPath>` element can be applied to the textPath instance itself:
```javascript
text.textPath.attr('startOffset', 0.5)
```
And they can be animated as well of course:
```javascript
text.textPath.animate(3000).attr('startOffset', 0.8)
```
Referencing the linked path element directly:
```javascript
var path = text.track
```
_Javascript inheritance stack: `SVG.TextPath` < `SVG.Element`_
### Use
The use element simply emulates another existing element. Any changes on the master element will be reflected on all the `use` instances. The usage of `use()` is very straightforward:
```javascript
var rect = draw.rect(100, 100).fill('#f09')
var use = draw.use(rect).move(200, 200)
```
In the case of the example above two rects will appear on the svg drawing, the original and the `use` instance. In some cases you might want to hide the original element. the best way to do this is to create the original element in the defs node:
```javascript
var rect = draw.defs().rect(100, 100).fill('#f09')
var use = draw.use(rect).move(200, 200)
```
In this way the rect element acts as a library element. You can edit it but it won't be rendered.
_Javascript inheritance stack: `SVG.Use` < `SVG.Shape` < `SVG.Element`_
## Referencing elements
### By id
If you want to get an element created by svg.js by its id, you can use the `SVG.get()` method:
```javascript
var element = SVG.get('my_element')
element.fill('#f06')
```
### By class name
There is no DOM querying system built into svg.js but [jQuery](http://jquery.com/) or [Zepto](http://zeptojs.com/) will help you achieve this. Here is an example:
```javascript
/* add elements */
var draw = SVG('drawing')
var group = draw.group().attr('class', 'my-group')
var rect = group.rect(100,100).attr('class', 'my-element')
var circle = group.circle(100).attr('class', 'my-element').move(100, 100)
/* get elements in group */
var elements = $('#drawing g.my-group .my-element').each(function() {
this.instance.animate().fill('#f09')
})
```
## Circular reference
Every element instance within svg.js has a reference to the actual `node`:
### node
```javascript
element.node
```
### instance
Similarly, the node carries a reference to the svg.js `instance`:
```javascript
node.instance
```
## Parent reference
Every element has a reference to its parent:
### parent
```javascript
element.parent
```
Even the main svg document:
```javascript
var draw = SVG('drawing')
draw.parent //-> returns the wrappig html element with id 'drawing'
```
### doc()
For more specific parent filtering the `doc()` method can be used:
```javascript
var draw = SVG('drawing')
var rect = draw.rect(100, 100)
rect.doc() //-> returns draw
```
Alternatively a class can be passed as the first argument:
```javascript
var draw = SVG('drawing')
var nested = draw.nested()
var group = nested.group()
var rect = group.rect(100, 100)
rect.doc() //-> returns draw
rect.doc(SVG.Doc) //-> returns draw
rect.doc(SVG.Nested) //-> returns nested
rect.doc(SVG.G) //-> returns group
```
## Child references
### first()
To get the first child of a parent element:
```javascript
draw.first()
```
### last()
To get the last child of a parent element:
```javascript
draw.last()
```
### children()
An array of all children will can be retreives with the `children` method:
```javascript
draw.children()
```
### each()
The `each()` allows you to iterate over the all children of a parent element:
```javascript
draw.each(function(i, children) {
this.fill({ color: '#f06' })
})
```
Deep traversing is also possible by passing true as the second argument:
```javascript
// draw.each(block, deep)
draw.each(function(i, children) {
this.fill({ color: '#f06' })
}, true)
```
Note that `this` refers to the current child element.
### has()
Checking the existence of an element within a parent:
```javascript
var rect = draw.rect(100, 50)
var group = draw.group()
draw.has(rect) //-> returns true
group.has(rect) //-> returns false
```
### get()
Get an element on a given position in the children array:
```javascript
var rect = draw.rect(20, 30)
var circle = draw.circle(50)
draw.get(0) //-> returns rect
draw.get(1) //-> returns circle
```
## Manipulating elements
### attr()
You can get and set an element's attributes directly using `attr()`.
Get a single attribute:
```javascript
rect.attr('x')
```
Set a single attribute:
```javascript
rect.attr('x', 50)
```
Set multiple attributes at once:
```javascript
rect.attr({
fill: '#f06'
, 'fill-opacity': 0.5
, stroke: '#000'
, 'stroke-width': 10
})
```
Set an attribute with a namespace:
```javascript
rect.attr('x', 50, 'http://www.w3.org/2000/svg')
```
Explicitly remove an attribute:
```javascript
rect.attr('fill', null)
```
### transform()
With the `transform()` method elements can be scaled, rotated, translated and skewed:
```javascript
rect.transform({
rotation: 45
, cx: 100
, cy: 100
})
```
You can also provide two arguments as property and value:
```javascript
rect.transform('matrix', '1,0.5,0.5,1,0,0')
```
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]
, scaleY: [scaling on y-axis]
, skewX: [skewing on x-axis]
, skewY: [skewing on y-axis]
, matrix: [a 6-digit matrix string; e.g. '1,0,0,1,0,0']
, a: [the first matrix digit]
, b: [the second matrix digit]
, c: [the third matrix digit]
, d: [the fourth matrix digit]
, e: [the fifth matrix digit]
, f: [the sixth matrix digit]
})
```
Note that you can also apply transformations directly using the `attr()` method:
```javascript
rect.attr('transform', 'matrix(1,0.5,0.5,1,0,0)')
```
Although that would mean you can't use the `transform()` method because it would overwrite any manually applied transformations. You should only go down this route if you know exactly what you are doing and you want to achieve an effect that is not achievable with the `transform()` method.
### style()
With the `style()` method the `style` attribute can be managed like attributes with `attr`:
```javascript
rect.style('cursor', 'pointer')
```
Multiple styles can be set at once using an object:
```javascript
rect.style({ cursor: 'pointer', fill: '#f03' })
```
Or a css string:
```javascript
rect.style('cursor:pointer;fill:#f03;')
```
Similarly to `attr()` the `style()` method can also act as a getter:
```javascript
rect.style('cursor')
// => pointer
```
Or even a full getter:
```javascript
rect.style()
// => 'cursor:pointer;fill:#f03;'
```
Explicitly deleting individual style definitions works the same as with the `attr()` method:
```javascript
rect.style('cursor', null)
```
### move(), x() and y()
Move the element to a given `x` and `y` position by its upper left corner:
```javascript
rect.move(200, 350)
```
The `text` element has one optional argument:
```javascript
// move(x, y, anchor)
rect.move(200, 350, true)
```
The third argument can be used to move the text element by its anchor point rather than the calculated left top position.
Note that you can also use the following code to move some elements (like images and rects) 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.
### x()
Move element only along x-axis by its upper left corner:
```javascript
rect.x(200)
```
Without an argument the `x()` method serves as a getter as well:
```javascript
rect.x() //-> returns 200
```
### y()
Move element only along y-axis by its upper left corner:
```javascript
rect.y(350)
```
Without an argument the `y()` method serves as a getter as well:
```javascript
rect.y() //-> returns 350
```
### relative()
The `move()`, `x()` and `y()` methods move an element to an absolute position. With the `relative()` method, elements can be moved relatively to their current position. This can be done by calling the `relative()` method before the `move()`, `x()` or `y()` methods in the method chain:
```javascript
rect.move(100, 100).relative().move(20, -50)
```
This will set the `x` position of the element to `120` and the `y` position to `50`.
It works the same way for `relative().x()` and `relative().y()`.
### center()
This is an extra method to move an element by its center:
```javascript
rect.center(150, 150)
```
The `text` element has one optional argument:
```javascript
// center(x, y, anchor)
rect.center(150, 150, true)
```
The third argument can be used to center the text element by its anchor point rather than the calculated center position.
### cx()
Move element only along x-axis by its center:
```javascript
rect.cx(200)
```
Without an argument the `cx()` method serves as a getter as well:
```javascript
rect.cx() //-> returns 200
```
### cy()
Move element only along y-axis by its center:
```javascript
rect.cy(350)
```
Without an argument the `cy()` method serves as a getter as well:
```javascript
rect.cy() //-> returns 350
```
### size()
Set the size of an element by a given `width` and `height`:
```javascript
rect.size(200, 300)
```
Proporional resizing is also possible by leaving out `height`:
```javascript
rect.size(200)
```
Or by passing `null` as the value for `width`:
```javascript
rect.size(null, 200)
```
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.
### width()
Set only width of an element:
```javascript
rect.width(200)
```
This method also acts as a getter:
```javascript
rect.width() //-> returns 200
```
### height()
Set only height of an element:
```javascript
rect.height(325)
```
This method also acts as a getter:
```javascript
rect.height() //-> returns 325
```
### hide(), show() and visible()
We all love to have a little hide:
```javascript
rect.hide()
```
and show:
```javascript
rect.show()
```
To check if the element is visible:
```javascript
rect.visible()
```
### clone()
To make an exact copy of an element the `clone()` method comes in handy:
```javascript
var clone = rect.clone()
```
This will create an new, unlinked copy. If you want to make a linked clone have a look at the [use](#elements/use) element.
### remove()
Pretty straightforward:
```javascript
rect.remove()
```
### clear()
To remove all elements from a parent element:
```javascript
draw.clear()
```
### replace()
This method will replace the called element with the given element in the same position in the stack:
```javascript
rect.replace(draw.circle(100))
```
## Inserting elements
### add()
Elements can be moved between parents via the `add()` method on any parent:
```javascript
var rect = draw.rect(100, 100)
var group = draw.group()
group.add(rect) //-> returns group
```
### put()
Where the `add()` method returns the parent itself, the `put()` method returns the given element:
```javascript
group.put(rect) //-> returns rect
```
### addTo()
Similarly to the `add()` method on a parent element, elements have the `addTo()` method:
```javascript
rect.addTo(group) //-> returns rect
```
### putIn()
Similarly to the `put()` method on a parent element, elements have the `putIn()` method:
```javascript
rect.putIn(group) //-> returns group
```
## Geometry
### viewBox()
The `viewBox` attribute of an `<svg>` element can be managed with the `viewbox()` method. When supplied with four arguments it will act as a setter:
```javascript
draw.viewbox(0, 0, 297, 210)
```
Alternatively you can also supply an object as the first argument:
```javascript
draw.viewbox({ x: 0, y: 0, width: 297, height: 210 })
```
Without any arguments an instance of `SVG.ViewBox` will be returned:
```javascript
var box = draw.viewbox()
```
But the best thing about the `viewbox()` method is that you can get the zoom of the viewbox:
```javascript
var box = draw.viewbox()
var zoom = box.zoom
```
If the size of the viewbox equals the size of the svg drawing, the zoom value will be 1.
### bbox()
```javascript
path.bbox()
```
This will return an instance of `SVG.BBox` containing the following values:
```javascript
{ width: 20, height: 20, x: 10, y: 20, cx: 30, cy: 20 }
```
As opposed to the native `getBBox()` method any translations used with the `transform()` method will be taken into account.
The `SVG.BBox` has one other nifty little feature, enter the `merge()` method. With `merge()` two `SVG.BBox` instances can be merged into one new instance, basically being the bounding box of the two original bounding boxes:
```javascript
var box1 = draw.rect(100,100).move(50,50)
var box2 = draw.rect(100,100).move(200,200)
var box3 = box1.merge(box2)
```
### rbox()
Is similar to `bbox()` but will give you the box around the exact representation of the element, taking all transformations into account.
```javascript
path.rbox()
```
This will return an instance of `SVG.RBox`.
### inside()
To check if a given point is inside the bounding box of an element you can use the `inside()` method:
```javascript
var rect = draw.rect(100, 100).move(50, 50)
rect.inside(25, 30) //-> returns false
rect.inside(60, 70) //-> returns true
```
Note: the `x` and `y` positions are tested against the relative position of the element. Any offset on the parent element is not taken into account.
## Animating elements
### animate()
Animating elements is very much the same as manipulating elements, the only difference is you have to include the `animate()` method:
```javascript
rect.animate().move(150, 150)
```
The `animate()` method will take three arguments. The first is `duration`, the second `ease` and the third `delay`:
```javascript
rect.animate(2000, '>', 1000).attr({ fill: '#f03' })
```
Alternatively you can pass an object as the first argument:
```javascript
rect.animate({ ease: '<', delay: 1500 }).attr({ fill: '#f03' })
```
By default `duration` will be set to `1000`, `ease` will be set to `<>`.
### easing
All available ease types are:
- `<>`: ease in and out
- `>`: ease out
- `<`: ease in
- `-`: linear
- `=`: external control
- a function
For the latter, here is an example of the default `<>` function:
```javascript
function(pos) { return (-Math.cos(pos * Math.PI) / 2) + 0.5 }
```
For more easing equations, have a look at the [svg.easing.js](https://github.com/wout/svg.easing.js) plugin.
### Animatable method chain
Note that the `animate()` method will not return the targeted element but an instance of SVG.FX which will take the following methods:
Of course `attr()`:
```javascript
rect.animate().attr({ fill: '#f03' })
```
The `x()`, `y()` and `move()` methods:
```javascript
rect.animate().move(100, 100)
```
And the `cx()`, `cy()` and `center()` methods:
```javascript
rect.animate().center(200, 200)
```
If you include the sugar.js module, `fill()`, `stroke()`, `rotate()`, `skew()`, `scale()`, `matrix()` and `opacity()` will be available as well:
```javascript
rect.animate().rotate(45).skew(25, 0)
```
You can also animate non-numeric unit values unsing the `attr()` method:
```javascript
rect.attr('x', '10%').animate().attr('x', '50%')
```
### pause() and play()
Pausing and playing an animations is fairly straightforward:
```javascript
rect.animate().move(200, 200)
rect.mouseenter(function() { this.pause() })
rect.mouseleave(function() { this.play() })
```
### stop()
Animations can be stopped in two ways.
By calling the `stop()` method:
```javascript
rect.animate().move(200, 200)
rect.stop()
```
Or by invoking another animation:
```javascript
rect.animate().move(200, 200)
rect.animate().center(200, 200)
```
Stopping an animation is irreversable.
### during()
If you want to perform your own actions during the animations you can use the `during()` method:
```javascript
var position
, from = 100
, to = 300
rect.animate(3000).move(100, 100).during(function(pos) {
position = from + (to - from) * pos
})
```
Note that `pos` is `0` in the beginning of the animation and `1` at the end of the animation.
To make things easier a morphing function is passed as the second argument. This function accepts a `from` and `to` value as the first and second argument and they can be a number, unit or hex color:
```javascript
var ellipse = draw.ellipse(100, 100).attr('cx', '20%').fill('#333')
rect.animate(3000).move(100, 100).during(function(pos, morph) {
/* numeric values */
ellipse.size(morph(100, 200), morph(100, 50))
/* unit strings */
ellipse.attr('cx', morph('20%', '80%'))
/* hex color strings */
ellipse.fill(morph('#333', '#ff0066'))
})
```
### loop()
By default the `loop()` method creates and eternal loop:
```javascript
rect.animate(3000).move(100, 100).loop()
```
But the loop can also be a predefined number of times:
```javascript
rect.animate(3000).move(100, 100).loop(5)
```
### after()
Finally, you can add callback methods using `after()`:
```javascript
rect.animate(3000).move(100, 100).after(function() {
this.animate().attr({ fill: '#f06' })
})
```
Note that the `after()` method will never be called if the animation is looping eternally.
### to()
Say you want to control the position of an animation with an external event, then the `to()` method will proove very useful:
```javascript
var animate = draw.rect(100, 100).move(50, 50).animate('=').move(200, 200)
document.onmousemove = function(event) {
animate.to(event.clientX / 1000)
}
```
In order to be able use the `to()` method the duration of the animation should be set to `'='`. The value passed as the first argument of `to()` should be a number between `0` and `1`, `0` being the beginning of the animation and `1` being the end. Note that any values below `0` and above `1` will be normalized.
_This functionality requires the fx.js module which is included in the default distribution._
## 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 })
```
A single hex string will work as well:
```javascript
rect.fill('#f06')
```
### stroke()
The `stroke()` method is similar to `fill()`:
```javascript
rect.stroke({ color: '#f06', opacity: 0.6, width: 5 })
```
Like fill, a single hex string will work as well:
```javascript
rect.stroke('#f06')
```
### opacity()
To set the overall opacity of an element:
```javascript
rect.opacity(0.5)
```
### rotate()
The `rotate()` method will automatically rotate elements according to the center of the element:
```javascript
// rotate(degrees)
rect.rotate(45)
```
Although you can also define a specific rotation point:
```javascript
// rotate(degrees, cx, cy)
rect.rotate(45, 50, 50)
```
### skew()
The `skew()` method will take an `x` and `y` value:
```javascript
// skew(x, y)
rect.skew(0, 45)
```
### scale()
The `scale()` method will take an `x` and `y` value:
```javascript
// scale(x, y)
rect.scale(0.5, -1)
```
### translate()
The `translate()` method will take an `x` and `y` value:
```javascript
// translate(x, y)
rect.translate(0.5, -1)
```
### radius()
Rects and ellipses have a `radius()` method. On rects it defines rounded corners, on ellipses the radii:
```javascript
rect.radius(10)
```
This will set the `rx` and `ry` attributes to `10`. To set `rx` and `ry` individually:
```javascript
rect.radius(10, 20)
```
_This functionality requires the sugar.js module which is included in the default distribution._
## Masking elements
### maskWith()
The easiest way to mask is to use a single element:
```javascript
var ellipse = draw.ellipse(80, 40).move(10, 10).fill({ color: '#fff' })
rect.maskWith(ellipse)
```
### mask()
But you can also use multiple elements:
```javascript
var ellipse = draw.ellipse(80, 40).move(10, 10).fill({ color: '#fff' })
var text = draw.text('SVG.JS').move(10, 10).font({ size: 36 }).fill({ color: '#fff' })
var mask = draw.mask().add(text).add(ellipse)
rect.maskWith(mask)
```
If you want the masked object to be rendered at 100% you need to set the fill color of the masking object to white. But you might also want to use a gradient:
```javascript
var gradient = draw.gradient('linear', function(stop) {
stop.at({ offset: 0, color: '#000' })
stop.at({ offset: 1, color: '#fff' })
})
var ellipse = draw.ellipse(80, 40).move(10, 10).fill({ color: gradient })
rect.maskWith(ellipse)
```
### unmask()
Unmasking the elements can be done with the `unmask()` method:
```javascript
rect.unmask()
```
The `unmask()` method returns the masking element.
### remove()
Removing the mask alltogether will also `unmask()` all masked elements as well:
```javascript
mask.remove()
```
Or:
```javascript
rect.masker.remove()
```
### masker
For your convenience, the masking element is also referenced in the masked element. This can be useful in case you want to change the mask:
```javascript
rect.masker.fill('#fff')
```
_This functionality requires the mask.js module which is included in the default distribution._
## Clipping elements
Clipping elements works exactly the same as masking elements. The only difference is that clipped elements will adopt the geometry of the clipping element. Therefore events are only triggered when entering the clipping element whereas with masks the masked element triggers the event. Another difference is that masks can define opacity with their fill color and clipPaths don't.
### clipWith()
```javascript
var ellipse = draw.ellipse(80, 40).move(10, 10)
rect.clipWith(ellipse)
```
### clip()
Clip multiple elements:
```javascript
var ellipse = draw.ellipse(80, 40).move(10, 10)
var text = draw.text('SVG.JS').move(10, 10).font({ size: 36 })
var clip = draw.clip().add(text).add(ellipse)
rect.clipWith(clip)
```
### unclip()
Unclipping the elements can be done with the `unclip()` method:
```javascript
rect.unclip()
```
### remove()
Removing the clip alltogether will also `unclip()` all clipped elements as well:
```javascript
clip.remove()
```
Or:
```javascript
rect.clipper.remove()
```
### clipper
For your convenience, the clipping element is also referenced in the clipped element. This can be useful in case you want to change the clipPath:
```javascript
rect.clipper.move(10, 10)
```
_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.
### front()
Move element to the front:
```javascript
rect.front()
```
### back()
Move element to the back:
```javascript
rect.back()
```
### forward()
Move element one step forward:
```javascript
rect.forward()
```
### backward()
Move element one step backward:
```javascript
rect.backward()
```
### siblings()
The arrange.js module brings some additional methods. To get all siblings of rect, including rect itself:
```javascript
rect.siblings()
```
### position()
Get the position (a number) of rect between its siblings:
```javascript
rect.position()
```
### next()
Get the next sibling:
```javascript
rect.next()
```
### previous()
Get the previous sibling:
```javascript
rect.previous()
```
### before()
Insert an element before another:
```javascript
// inserts circle before rect
rect.before(circle)
```
### after()
Insert an element after another:
```javascript
// inserts circle after rect
rect.after(circle)
```
_This functionality requires the arrange.js module which is included in the default distribution._
## Sets
Sets are very useful if you want to modify or animate multiple elements at once. A set will accept all the same methods accessible on individual elements, even the ones that you add with your own plugins! Creating a set is exactly as you would expect:
```javascript
// create some elements
var rect = draw.rect(100,100)
var circle = draw.circle(100).move(100,100).fill('#f09')
// create a set and add the elements
var set = draw.set()
set.add(rect).add(circle)
// change the fill of all elements in the set at once
set.fill('#ff0')
```
A single element can be a member of many sets. Sets also don't have a structural representation, in fact they are just fancy array's.
### add()
Add an element to a set:
```javascript
set.add(rect)
```
Quite a useful feature of sets is the ability to accept multiple elements at once:
```javascript
set.add(rect, circle)
```
### each()
Iterating over all members in a set is the same as with svg containers:
```javascript
set.each(function(i) {
this.attr('id', 'shiny_new_id_' + i)
})
```
Note that `this` refers to the current child element.
### has()
Determine if an element is member of the set:
```javascript
set.has(rect) //-> returns true of false
```
### get()
Get the element at a given index:
```javascript
set.get(1)
```
### bbox()
Get the bounding box of all elements in the set:
```javascript
set.bbox()
```
### remove()
To remove an element from a set:
```javascript
set.remove(rect)
```
### clear()
Or to remove all elements from a set:
```javascript
set.clear()
```
Sets work with animations as well:
```javascript
set.animate(3000).fill('#ff0')
```
## Gradients
### gradient()
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: 1, color: '#fff', opacity: 1 })
})
```
### at()
The `offset` and `color` parameters are required for stops, `opacity` is optional. Offset is float between 0 and 1, or a percentage value (e.g. `33%`).
```javascript
stop.at({ offset: 0, color: '#333', opacity: 1 })
```
### from() and to()
To define the direction you can set from `x`, `y` and to `x`, `y`:
```javascript
gradient.from(0, 0).to(0, 1)
```
The from and to values are also expressed in percent.
### radius()
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: 1, color: '#fff', opacity: 1 })
})
gradient.from(0.5, 0.5).to(0.5, 0.5).radius(0.5)
```
### update()
A gradient can also be updated afterwards:
```javascript
gradient.update(function(stop) {
stop.at({ offset: 0.1, color: '#333', opacity: 0.2 })
stop.at({ offset: 0.9, 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: 0.5, color: '#f03', opacity: 1 })
s3 = stop.at({ offset: 1, color: '#066', opacity: 1 })
})
s1.update({ offset: 0.1, color: '#0f0', opacity: 1 })
```
### get()
The `get()` method makes it even easier to get a stop from an existing gradient:
```javascript
var gradient = draw.gradient('radial', function(stop) {
stop.at({ offset: 0, color: '#000', opacity: 1 }) // -> first
stop.at({ offset: 0.5, color: '#f03', opacity: 1 }) // -> second
stop.at({ offset: 1, color: '#066', opacity: 1 }) // -> third
})
var s1 = gradient.get(0) // -> returns "first" stop
```
### fill()
Finally, to use the gradient on an element:
```javascript
rect.attr({ fill: gradient })
```
By passing the gradient instance as the fill on any element, the `fill()` method will be called:
```javascript
gradient.fill() //-> returns 'url(#SvgjsGradient1234)'
```
[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._
## Data
### Setting
The `data()` method allows you to bind arbitrary objects, strings and numbers to SVG elements:
```javascript
rect.data('key', { value: { data: 0.3 }})
```
Or set multiple values at once:
```javascript
rect.data({
forbidden: 'fruit'
, multiple: {
values: 'in'
, an: 'object'
}
})
```
### Getting
Fetching the values is similar to the `attr()` method:
```javascript
rect.data('key')
```
### Removing
Removing the data altogether:
```javascript
rect.data('key', null)
```
### Sustaining data types
Your values will always be stored as JSON and in some cases this might not be desirable. If you want to store the value as-is, just pass true as the third argument:
```javascript
rect.data('key', 'value', true)
```
## Memory
### remember()
Storing data in-memory is very much like setting attributes:
```javascript
rect.remember('oldBBox', rect.bbox())
```
Multiple values can also be remembered at once:
```javascript
rect.remember({
oldFill: rect.attr('fill')
, oldStroke: rect.attr('stroke')
})
```
To retrieve a memory
```javascript
rect.remember('oldBBox')
```
### forget()
Erasing a single memory:
```javascript
rect.forget('oldBBox')
```
Or erasing multiple memories at once:
```javascript
rect.forget('oldFill', 'oldStroke')
```
And finally, just erasing the whole memory:
```javascript
rect.forget()
```
## Events
### Basic events
Events can be bound to elements as follows:
```javascript
rect.click(function() {
this.fill({ color: '#f06' })
})
```
Removing it is quite as easy:
```javascript
rect.click(null)
```
All available evenets are: `click`, `dblclick`, `mousedown`, `mouseup`, `mouseover`, `mouseout`, `mousemove`, `mouseenter` and `mouseleave`.
### Event listeners
You can also bind event listeners to elements:
```javascript
var click = function() {
rect.fill({ color: '#f06' })
}
rect.on('click', click)
```
Note that the context of event listeners is not the same as events, which are applied directly to the element. Therefore `this` will not refer to the element when using event listeners.
Unbinding events is just as easy:
```javascript
rect.off('click', click)
```
But there is more to event listeners. You can bind events to html elements as well:
```javascript
SVG.on(window, 'click', click)
```
Obviously unbinding is practically the same:
```javascript
SVG.off(window, 'click', click)
```
## Colors
Svg.js has a dedicated color module handling different types of colors. Accepted values are:
- hex string; three based (e.g. #f06) or six based (e.g. #ff0066) `new SVG.Color('#f06')`
- rgb string; e.g. rgb(255, 0, 102) `new SVG.Color('rgb(255, 0, 102)')`
- rgb object; e.g. { r: 255, g: 0, b: 102 } `new SVG.Color({ r: 255, g: 0, b: 102 })`
Note that when working with objects is important to provide all three values every time.
The `SVG.Color` instance has a few methods of its own.
### toHex()
Get hex value:
```javascript
color.toHex() //-> returns '#ff0066'
```
### toRgb()
Get rgb string value:
```javascript
color.toRgb() //-> returns 'rgb(255,0,102)'
```
### brightness()
Get the brightness of a color:
```javascript
color.brightness() //-> returns 0.344
```
### morph()
Make a color morphable:
```javascript
color.morph('#000')
```
### at()
Get morphable color at given position:
```javascript
var color = new SVG.Color('#ff0066').morph('#000')
color.at(0.5).toHex() //-> '#7f0033'
```
This is the perceived brighness where `0` is black and `1` is white.
## Arrays
In svg.js every value list string can be cast and passed as an array. This makes writing them more convenient but also adds a lot of key functionality to them.
### SVG.Array
Is for simple, whitespace separated value strings:
```javascript
'0.343 0.669 0.119 0 0 0.249 -0.626 0.13 0 0 0.172 0.334 0.111 0 0 0 0 0 1 0'
```
Can also be passed like this in a more manageable format:
```javascript
new SVG.Array([ .343, .669, .119, 0, 0
, .249, -.626, .130, 0, 0
, .172, .334, .111, 0, 0
, .000, .000, .000, 1, -0 ])
```
### SVG.PointArray
Is a bit more complex and is used for polyline and polygon elements. This is a poly-point string:
```javascript
'0,0 100,100'
```
The dynamic representation:
```javascript
new SVG.PointArray([[0, 0], [100, 100]])
```
Note that every instance of `SVG.Polyline` and `SVG.Polygon` carries a reference to the `SVG.PointArray` instance:
```javascript
polygon.array //-> returns the SVG.PointArray instance
```
_Javascript inheritance stack: `SVG.PointArray` < `SVG.Array`_
### SVG.PathArray
Path arrays carry objects representing every point in a path string:
```javascript
'M 0 0 L 100 100 z'
```
The dynamic representation:
```javascript
new SVG.PathArray([
{ type: 'M', x: 0, y: 0 }
, { type: 'L', x: 100, y: 100 }
, { type: 'z' }
])
```
Note that every instance of `SVG.Path` carries a reference to the `SVG.PathArray` instance:
```javascript
polygon.array //-> returns the SVG.PointArray instance
```
_Javascript inheritance stack: `SVG.PathArray` < `SVG.Array`_
### morph()
In order to animate array values the `morph()` method lets you pass a destination value. This can be either the string value, a plain array or an instance of the same type of svg.js array:
```javascript
var array = new SVG.PointArray([[0, 0], [100, 100]])
array.morph('100,0 0,100 200,200')
```
This method will prepare the array ensuring both the source and destination arrays have the same length.
Note that this method is currently not available on `SVG.PathArray` but will be soon.
### at()
This method will morph the array to a given position between `0` and `1`. Continuing with the previous example:
```javascript
array.at(0.27).toString() //-> returns '27,0 73,100 127,127'
```
Note that this method is currently not available on `SVG.PathArray` but will be soon.
### settle()
When morphing is done the `settle()` method will eliminate any transitional points like duplicates:
```javascript
array.settle()
```
Note that this method is currently not available on `SVG.PathArray` but will be soon.
### move()
Moves geometry of the array with the given `x` and `y` values:
```javascript
var array = new SVG.PointArray([[0, 0], [100, 100]])
array.move(33,75)
array.toString() //-> returns '33,75 133,175'
```
Note that this method is only available on `SVG.PointArray` and `SVG.PathArray`
### size()
Resizes geometry of the array by the given `width` and `height` values:
```javascript
var array = new SVG.PointArray([[0, 0], [100, 100]])
array.move(100,100).size(222,333)
array.toString() //-> returns '100,100 322,433'
```
Note that this method is only available on `SVG.PointArray` and `SVG.PathArray`
### bbox()
Gets the bounding box of the geometry of the array:
```javascript
array.bbox()
```
Note that this method is only available on `SVG.PointArray` and `SVG.PathArray`
## Extending functionality
### SVG.extend
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() {
for (var i = 0, l = this.children.length; i < l; i++) {
this.children[i].fill({ color: 'pink' })
}
return this
}
})
```
You can also extend multiple elements at once:
```javascript
SVG.extend(SVG.Ellipse, SVG.Path, SVG.Polygon, {
paintRed: function() {
return this.fill({ color: 'orangered' })
}
})
```
## Plugins
Here are a few nice plugins that are available for svg.js:
### draggable
[svg.draggable.js](https://github.com/wout/svg.draggable.js) to make elements draggable.
### easing
[svg.easing.js](https://github.com/wout/svg.easing.js) for more easing methods on animations.
### export
[svg.export.js](https://github.com/wout/svg.export.js) export raw SVG.
### filter
[svg.filter.js](https://github.com/wout/svg.filter.js) adding svg filters to elements.
### foreignobject
[svg.foreignobject.js](https://github.com/john-memloom/svg.foreignobject.js) foreignObject implementation (by john-memloom).
### import
[svg.import.js](https://github.com/wout/svg.import.js) import raw SVG data.
### math
[svg.math.js](https://github.com/otm/svg.math.js) a math extension (by Nils Lagerkvist).
### path
[svg.path.js](https://github.com/otm/svg.path.js) for manually drawing paths (by Nils Lagerkvist).
### pattern
[svg.pattern.js](https://github.com/wout/svg.pattern.js) add fill patterns.
### shapes
[svg.shapes.js](https://github.com/wout/svg.shapes.js) for more polygon based shapes.
### topath
[svg.topath.js](https://github.com/wout/svg.topath.js) to convert any other shape to a path.
## 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 svg.js by running `rake`:
``` sh
$ rake
Original version: 32.165k
Minified: 14.757k
Minified and gzipped: 4.413k, compression factor 7.289
```
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:
``` sh
$ rake concat[-clip:group:arrange] dist
```
To build the base library only including shapes:
``` sh
rake concat[-fx:-event:-group:-arrange:-mask:-gradient:-nested:-sugar] dist
```
## 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 beta, therefore the API might be subject to change in the course of development.
|