# 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
```
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
```
### 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')
```
__`returns`: `SVG.Doc`__
_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)
```
__`returns`: `SVG.Nested`__
_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)
```
__`returns`: `SVG.G`__
_Javascript inheritance stack: `SVG.G` < `SVG.Container` < `SVG.Parent`_
### Hyperlink
A hyperlink or `` 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')
})
```
__`returns`: `SVG.A`__
_Javascript inheritance stack: `SVG.A` < `SVG.Container` < `SVG.Parent`_
### Defs
The `` element is a container element for referenced elements. Elements that are descendants of a ‘defs’ are not rendered directly. The `` node lives in the main `