forked from jonkirkman/ringchart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lilsvg.js
41 lines (35 loc) · 1.1 KB
/
lilsvg.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
/*
* LilSVG - A micro library for easily creating SVG elements
*
* @usage:
* var mySVG = new LilSVG();
* --> <svg version="1.2"></svg>
*
* var yourSVG = new LilSVG('svg', { viewPort: '0 0 500 500' });
* --> <svg version="1.2" viewPort="0 0 500 500"></svg>
*
* var myCircle = new LilSVG('circle', { cx: 250, cy: 250, r: 100 });
* yourSVG.appendChild( myCirle );
* --> <svg version="1.2" viewPort="0 0 500 500"> <circle cx="250" cy="250" r="100"/> </svg>
*/
var LilSVG = function( type, attrs ) {
// default to a root svg element no other type is supplied
type = type || 'svg';
// create the element
var svg_el = document.createElementNS('http://www.w3.org/2000/svg', type);
// if this is a root svg element we should set some defaults
if (type == 'svg') {
this.set( svg_el, { version: '1.2', baseProfile: 'tiny' });
}
// apply the supplied attributes
this.set( svg_el, attrs );
// return the new element
return svg_el;
};
LilSVG.prototype.set = function( svg_el, attrs ) {
if (typeof attrs == 'object') {
for (var key in attrs ) {
svg_el.setAttribute( key, attrs[key] );
}
}
};