summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dist/svg.js175
-rw-r--r--dist/svg.min.js4
-rw-r--r--src/arrange.js9
-rw-r--r--src/clip.js9
-rw-r--r--src/container.js30
-rw-r--r--src/doc.js3
-rw-r--r--src/element.js23
-rw-r--r--src/ellipse.js4
-rw-r--r--src/gradient.js37
-rw-r--r--src/path.js2
-rw-r--r--src/sugar.js32
-rw-r--r--src/svg.js3
-rw-r--r--src/text.js23
13 files changed, 231 insertions, 123 deletions
diff --git a/dist/svg.js b/dist/svg.js
index 61f9898..737df90 100644
--- a/dist/svg.js
+++ b/dist/svg.js
@@ -1,14 +1,17 @@
-/* svg.js v0.1-40-g5fc94f8 - svg container element group arrange defs clip gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */
+/* svg.js v0.1-43-g52ed3ba - svg container element group arrange defs clip gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */
(function() {
this.SVG = {
+ // define default namespaces
ns: 'http://www.w3.org/2000/svg',
xlink: 'http://www.w3.org/1999/xlink',
+ // method for element creation
create: function(e) {
return document.createElementNS(this.ns, e);
},
+ // method for extending objects
extend: function(o, m) {
for (var k in m)
o.prototype[k] = m[k];
@@ -18,6 +21,7 @@
SVG.Container = {
+ // add given element at goven position
add: function(e, i) {
if (!this.has(e)) {
i = i == null ? this.children().length : i;
@@ -29,23 +33,28 @@
return this;
},
+ // basically does the same as add() but returns the added element rather than 'this'
put: function(e, i) {
this.add(e, i);
return e;
},
+ // chacks if the given element is a child
has: function(e) {
return this.children().indexOf(e) >= 0;
},
+ // returns all child elements and initializes store array if non existant
children: function() {
return this._children || (this._children = []);
},
+ // remove a given child element
remove: function(e) {
return this.removeAt(this.children().indexOf(e));
},
+ // remove child element at a given position
removeAt: function(i) {
if (0 <= i && i < this.children().length) {
var e = this.children()[i];
@@ -57,58 +66,63 @@
return this;
},
+ // returns defs element and initializes store array if non existant
defs: function() {
- if (this._defs == null) {
- this._defs = new SVG.Defs();
- this.add(this._defs, 0);
- }
-
- return this._defs;
+ return this._defs || (this._defs = this.put(new SVG.Defs(), 0));
},
+ // re-level defs to first positon in element stack
level: function() {
- var d = this.defs();
-
- return this.remove(d).add(d, 0);
+ return this.remove(d).put(this.defs(), 0);
},
+ // create a group element
group: function() {
return this.put(new SVG.G());
},
+ // create a rect element
rect: function(w, h) {
return this.put(new SVG.Rect().size(w, h));
},
+ // create circle element, based on ellipse
circle: function(d) {
return this.ellipse(d);
},
+ // create and ellipse
ellipse: function(w, h) {
return this.put(new SVG.Ellipse().size(w, h));
},
+ // create a polyline element
polyline: function(p) {
return this.put(new SVG.Polyline().plot(p));
},
+ // create a polygon element
polygon: function(p) {
return this.put(new SVG.Polygon().plot(p));
},
+ // create a path element
path: function(d) {
return this.put(new SVG.Path().plot(d));
},
+ // create image element, load image and set its size
image: function(s, w, h) {
w = w != null ? w : 100;
return this.put(new SVG.Image().load(s).size(w, h != null ? h : w));
},
+ // create text element
text: function(t) {
return this.put(new SVG.Text().text(t));
},
+ // create element in defs
gradient: function(t, b) {
return this.defs().gradient(t, b);
},
@@ -136,8 +150,13 @@
};
SVG.Element = function Element(n) {
+ // keep reference to the element node
this.node = n;
+
+ // initialize attribute store
this.attrs = {};
+
+ // initialize transformations store
this.trans = {
x: 0,
y: 0,
@@ -147,8 +166,6 @@
skewX: 0,
skewY: 0
};
-
- this._s = ('size family weight stretch variant style').split(' ');
};
// Add element-specific functions
@@ -184,9 +201,11 @@
// set svg element attribute
attr: function(a, v, n) {
if (arguments.length < 2) {
+ // apply every attribute individually if an object is passed
if (typeof a == 'object')
for (v in a) this.attr(v, a[v]);
-
+
+ // act as a getter for style attributes
else if (this._isStyle(a))
return a == 'text' ?
this.content :
@@ -194,19 +213,26 @@
this[a] :
this.style[a];
+ // act as a getter if the first and only argument is not an object
else
return this.attrs[a];
} else {
+ // store value
this.attrs[a] = v;
+
+ // treat x differently on text elements
if (a == 'x' && this._isText())
for (var i = this.lines.length - 1; i >= 0; i--)
this.lines[i].attr(a, v);
+
+ // set the actual attribute
else
n != null ?
this.node.setAttributeNS(n, a, v) :
this.node.setAttribute(a, v);
+ // if the passed argument belongs to the style as well, add it there
if (this._isStyle(a)) {
a == 'text' ?
this.text(v) :
@@ -286,14 +312,15 @@
// private: find svg parent
_parent: function(pt) {
var e = this;
-
+
+ // find ancestor with given type
while (e != null && !(e instanceof pt))
e = e.parent;
return e;
},
- // private: is this text style
+ // private: tester method for style detection
_isStyle: function(a) {
return typeof a == 'string' && this._isText() ? (/^font|text|leading/).test(a) : false;
},
@@ -318,7 +345,7 @@
SVG.extend(SVG.Element, {
- // get all siblings, including me
+ // get all siblings, including myself
siblings: function() {
return this.parent.children();
},
@@ -326,9 +353,8 @@
// send given element one step forwards
forward: function() {
var i = this.siblings().indexOf(this);
- this.parent.remove(this).add(this, i + 1);
- return this;
+ return this.parent.remove(this).put(this, i + 1);
},
// send given element one step backwards
@@ -345,9 +371,7 @@
// send given element all the way to the front
front: function() {
- this.parent.remove(this).add(this);
-
- return this;
+ return this.parent.remove(this).put(this);
},
// send given element all the way to the back
@@ -378,6 +402,8 @@
SVG.Clip = function Clip() {
this.constructor.call(this, SVG.create('clipPath'));
+
+ // set unique id
this.id = 'svgjs_clip_' + (clipID++);
this.attr('id', this.id);
};
@@ -409,12 +435,9 @@
// add def-specific functions
SVG.extend(SVG.Defs, {
- // define clippath
+ // create clippath
clip: function() {
- var e = new SVG.Clip();
- this.add(e);
-
- return e;
+ return this.put(new SVG.Clip());
}
});
@@ -423,10 +446,13 @@
SVG.Gradient = function Gradient(t) {
this.constructor.call(this, SVG.create(t + 'Gradient'));
- this.id = 'svgjs_grad_' + (gradID++);
- this.type = t;
+ // set unique id
+ this.id = 'svgjs_grad_' + (gradID++);
this.attr('id', this.id);
+
+ // store type
+ this.type = t;
};
// inherit from SVG.Element
@@ -459,21 +485,20 @@
this;
},
- // add color stops
+ // add a color stop
at: function(o) {
- var m = new SVG.Stop(o);
- this.add(m);
-
- return m;
+ return this.put(new SVG.Stop(o));
},
// update gradient
update: function(b) {
+ // remove all stops
while (this.node.hasChildNodes())
this.node.removeChild(this.node.lastChild);
+ // invoke passed block
b(this);
-
+
return this;
},
@@ -489,8 +514,9 @@
// define clippath
gradient: function(t, b) {
- var e = new SVG.Gradient(t);
- this.add(e);
+ var e = this.put(new SVG.Gradient(t));
+
+ // invoke passed block
b(e);
return e;
@@ -502,6 +528,7 @@
SVG.Stop = function Stop(o) {
this.constructor.call(this, SVG.create('stop'));
+ // immediatelly build stop
this.update(o);
};
@@ -513,15 +540,18 @@
// add color stops
update: function(o) {
- var s = '',
+ var i,
+ s = '',
a = ['opacity', 'color'];
-
- for (var i = a.length - 1; i >= 0; i--)
+
+ // build style attribute
+ for (i = a.length - 1; i >= 0; i--)
if (o[a[i]] != null)
s += 'stop-' + a[i] + ':' + o[a[i]] + ';';
-
+
+ // set attributes
return this.attr({
- offset: (o.offset != null ? o.offset : this.attr('offset') || 0) + '%',
+ offset: (o.offset != null ? o.offset : this.attrs.offset || 0) + '%',
style: s
});
}
@@ -547,10 +577,11 @@
attr('xlink', SVG.xlink, SVG.ns).
defs();
+ // add elements
e.appendChild(w);
w.appendChild(this.node);
- // ensure
+ // ensure correct rendering for safari
this.stage();
};
@@ -605,8 +636,8 @@
// position element by its center
center: function(x, y) {
return this.attr({
- cx: x || ((this.attrs.x || 0) + (this.attrs.rx || 0)),
- cy: y || ((this.attrs.y || 0) + (this.attrs.ry || 0))
+ cx: x || (this.attrs.x || 0) + (this.attrs.rx || 0),
+ cy: y || (this.attrs.y || 0) + (this.attrs.ry || 0)
});
}
@@ -666,7 +697,7 @@
return this.attr('d', d || 'M0,0');
},
- // move path using translate
+ // move path using translate, path's don't take x and y
move: function(x, y) {
return this.transform({ x: x, y: y });
}
@@ -690,12 +721,15 @@
});
+ var _styleAttr = ['size', 'family', 'weight', 'stretch', 'variant', 'style'];
+
+
SVG.Text = function Text() {
this.constructor.call(this, SVG.create('text'));
+ // define default style
this.style = { 'font-size': 16, 'font-family': 'Helvetica', 'text-anchor': 'start' };
this.leading = 1.2;
- this.lines = [];
};
// inherit from SVG.Element
@@ -705,6 +739,7 @@
SVG.extend(SVG.Text, {
text: function(t) {
+ // update the content
this.content = t = t || 'text';
this.lines = [];
@@ -714,31 +749,37 @@
a = t.split("\n"),
f = this.style['font-size'];
+ // remove existing child nodes
while (this.node.hasChildNodes())
this.node.removeChild(this.node.lastChild);
+ // build new lines
for (i = 0, l = a.length; i < l; i++) {
+ // create new tspan and set attributes
n = new TSpan().
text(a[i]).
attr({
dy: f * this.leading - (i == 0 ? f * 0.3 : 0),
- x: (this.attr('x') || 0),
+ x: (this.attrs.x || 0),
style: s
});
+ // add new tspan
this.node.appendChild(n.node);
this.lines.push(n);
};
+ // set style
return this.attr('style', s);
},
+ // build style based on _styleAttr
_style: function() {
- var i, o = '', s = this._s;
+ var i, o = '';
- for (i = s.length - 1; i >= 0; i--)
- if (this.style['font-' + s[i]] != null)
- o += 'font-' + s[i] + ':' + this.style['font-' + s[i]] + ';';
+ for (i = _styleAttr.length - 1; i >= 0; i--)
+ if (this.style['font-' + _styleAttr[i]] != null)
+ o += 'font-' + _styleAttr[i] + ':' + this.style['font-' + _styleAttr[i]] + ';';
o += 'text-anchor:' + this.style['text-anchor'] + ';';
@@ -766,29 +807,41 @@
});
+ var _strokeAttr = ['width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset'],
+ _fillAttr = ['opacity', 'rule'];
+
+
+ // Add shape-specific functions
SVG.extend(SVG.Shape, {
// set fill color and opacity
fill: function(f) {
+ var i;
+
+ // set fill color if not null
if (f.color != null)
this.attr('fill', f.color);
-
- if (f.opacity != null)
- this.attr('fill-opacity', f.opacity);
-
+
+ // set all attributes from _fillAttr list with prependes 'fill-' if not null
+ for (i = _fillAttr.length - 1; i >= 0; i--)
+ if (f[_fillAttr[i]] != null)
+ this.attr('fill-' + _fillAttr[i], f[_fillAttr[i]]);
+
return this;
},
// set stroke color and opacity
stroke: function(s) {
+ var i;
+
+ // set stroke color if not null
if (s.color)
this.attr('stroke', s.color);
- var a = ('width opacity linecap linejoin miterlimit dasharray dashoffset').split(' ');
-
- for (var i = a.length - 1; i >= 0; i--)
- if (s[a[i]] != null)
- this.attr('stroke-' + a[i], s[a[i]]);
+ // set all attributes from _strokeAttr list with prependes 'stroke-' if not null
+ for (i = _strokeAttr.length - 1; i >= 0; i--)
+ if (s[_strokeAttr[i]] != null)
+ this.attr('stroke-' + _strokeAttr[i], s[_strokeAttr[i]]);
return this;
}
@@ -841,7 +894,7 @@
a[k] = o[k] :
k == 'anchor' ?
a['text-anchor'] = o[k] :
- this._s.indexOf(k) > -1 ?
+ _styleAttr.indexOf(k) > -1 ?
a['font-'+ k] = o[k] :
void 0;
diff --git a/dist/svg.min.js b/dist/svg.min.js
index 999ff9e..29c5990 100644
--- a/dist/svg.min.js
+++ b/dist/svg.min.js
@@ -1,2 +1,2 @@
-/* svg.js v0.1-40-g5fc94f8 - svg container element group arrange defs clip gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */
-function svg(e){return new SVG.Doc(e)}(function(){function n(){this.constructor.call(this,SVG.create("tspan"))}this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",create:function(e){return document.createElementNS(this.ns,e)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]||null),e.parent=this),this},put:function(e,t){return this.add(e,t),e},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e<this.children().length){var t=this.children()[e];this.children().splice(e,1),this.node.removeChild(t.node),t.parent=null}return this},defs:function(){return this._defs==null&&(this._defs=new SVG.Defs,this.add(this._defs,0)),this._defs},level:function(){var e=this.defs();return this.remove(e).add(e,0)},group:function(){return this.put(new SVG.G)},rect:function(e,t){return this.put((new SVG.Rect).size(e,t))},circle:function(e){return this.ellipse(e)},ellipse:function(e,t){return this.put((new SVG.Ellipse).size(e,t))},polyline:function(e){return this.put((new SVG.Polyline).plot(e))},polygon:function(e){return this.put((new SVG.Polygon).plot(e))},path:function(e){return this.put((new SVG.Path).plot(e))},image:function(e,t,n){return t=t!=null?t:100,this.put((new SVG.Image).load(e).size(t,n!=null?n:t))},text:function(e){return this.put((new SVG.Text).text(e))},gradient:function(e,t){return this.defs().gradient(e,t)},stage:function(){var e,t=this;return e=function(){document.readyState==="complete"?(t.attr("style","position:absolute;"),setTimeout(function(){t.attr("style","position:relative;")},5)):setTimeout(e,10)},e(),this}},SVG.Element=function(t){this.node=t,this.attrs={},this.trans={x:0,y:0,scaleX:1,scaleY:1,rotation:0,skewX:0,skewY:0},this._s="size family weight stretch variant style".split(" ")},SVG.extend(SVG.Element,{move:function(e,t){return this.attr({x:e,y:t})},size:function(e,t){return this.attr({width:e,height:t})},center:function(e,t){var n=this.bbox();return this.move(e-n.width/2,t-n.height/2)},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},attr:function(e,t,n){if(arguments.length<2){if(typeof e!="object")return this._isStyle(e)?e=="text"?this.content:e=="leading"?this[e]:this.style[e]:this.attrs[e];for(t in e)this.attr(t,e[t])}else{this.attrs[e]=t;if(e=="x"&&this._isText())for(var r=this.lines.length-1;r>=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e){if(typeof e=="string")return this.trans[e];var t,n=[],r=this.bbox(),i=this.attr("transform")||"",s=i.match(/[a-z]+\([^\)]+\)/g)||[];for(t in e)e[t]!=null&&(this.trans[t]=e[t]);return e=this.trans,e.rotation!=0&&n.push("rotate("+e.rotation+","+(e.cx!=null?e.cx:r.cx)+","+(e.cy!=null?e.cy:r.cy)+")"),n.push("scale("+e.scaleX+","+e.scaleY+")"),e.skewX!=0&&n.push("skewX("+e.skewX+")"),e.skewY!=0&&n.push("skewY("+e.skewY+")"),n.push("translate("+e.x+","+e.y+")"),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return{x:e.x+this.trans.x,y:e.y+this.trans.y,cx:e.x+this.trans.x+e.width/2,cy:e.y+this.trans.y+e.height/2,width:e.width,height:e.height}},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t},_isStyle:function(e){return typeof e=="string"&&this._isText()?/^font|text|leading/.test(e):!1},_isText:function(){return this instanceof SVG.Text}}),SVG.G=function(){this.constructor.call(this,SVG.create("g"))},SVG.G.prototype=new SVG.Element,SVG.extend(SVG.G,SVG.Container),SVG.extend(SVG.Element,{siblings:function(){return this.parent.children()},forward:function(){var e=this.siblings().indexOf(this);return this.parent.remove(this).add(this,e+1),this},backward:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,e-1),this},front:function(){return this.parent.remove(this).add(this),this},back:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}}),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container);var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_clip_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.parent.defs().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){var e=new SVG.Clip;return this.add(e),e}});var t=0;SVG.Gradient=function(n){this.constructor.call(this,SVG.create(n+"Gradient")),this.id="svgjs_grad_"+t++,this.type=n,this.attr("id",this.id)},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){var t=new SVG.Stop(e);return this.add(t),t},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=new SVG.Gradient(e);return this.add(n),t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t="",n=["opacity","color"];for(var r=n.length-1;r>=0;r--)e[n[r]]!=null&&(t+="stop-"+n[r]+":"+e[n[r]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attr("offset")||0)+"%",style:t})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg"));var n=document.createElement("div");n.style.cssText="position:relative;width:100%;height:100%;",typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",width:"100%",height:"100%"}).attr("xlink",SVG.xlink,SVG.ns).defs(),t.appendChild(n),n.appendChild(this.node),this.stage()},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:(t!=null?t:e)/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Poly={plot:function(e){return this.attr("points",e||"0,0")},move:function(e,t){return this.transform({x:e,y:t})}},SVG.Polyline=function(){this.constructor.call(this,SVG.create("polyline"))},SVG.Polyline.prototype=new SVG.Shape,SVG.extend(SVG.Polyline,SVG.Poly),SVG.Polygon=function(){this.constructor.call(this,SVG.create("polygon"))},SVG.Polygon.prototype=new SVG.Shape,SVG.extend(SVG.Polygon,SVG.Poly),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{plot:function(e){return this.attr("d",e||"M0,0")},move:function(e,t){return this.transform({x:e,y:t})}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("xlink:href",e,SVG.xlink)}}),SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica","text-anchor":"start"},this.leading=1.2,this.lines=[]},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text",this.lines=[];var t,r,i=this._style(),s=this.parentDoc(),o=e.split("\n"),u=this.style["font-size"];while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t<l;t++)r=(new n).text(o[t]).attr({dy:u*this.leading-(t==0?u*.3:0),x:this.attr("x")||0,style:i}),this.node.appendChild(r.node),this.lines.push(r);return this.attr("style",i)},_style:function(){var e,t="",n=this._s;for(e=n.length-1;e>=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");return t+="text-anchor:"+this.style["text-anchor"]+";",t}}),n.prototype=new SVG.Shape,SVG.extend(n,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}}),SVG.extend(SVG.Shape,{fill:function(e){return e.color!=null&&this.attr("fill",e.color),e.opacity!=null&&this.attr("fill-opacity",e.opacity),this},stroke:function(e){e.color&&this.attr("stroke",e.color);var t="width opacity linecap linejoin miterlimit dasharray dashoffset".split(" ");for(var n=t.length-1;n>=0;n--)e[t[n]]!=null&&this.attr("stroke-"+t[n],e[t[n]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e,t,n){var r=this.bbox();return this.transform({rotation:e||0,cx:t==null?r.cx:t,cy:n==null?r.cx:n})},skew:function(e,t){return this.transform({skewX:e||0,skewY:t||0})}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform({x:e,y:t})}}),SVG.extend(SVG.Text,{font:function(e){var t,n={};for(t in e)t=="leading"?n[t]=e[t]:t=="anchor"?n["text-anchor"]=e[t]:this._s.indexOf(t)>-1?n["font-"+t]=e[t]:void 0;return this.attr(n).text(this.content)}})}).call(this); \ No newline at end of file
+/* svg.js v0.1-43-g52ed3ba - svg container element group arrange defs clip gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */
+function svg(e){return new SVG.Doc(e)}(function(){function r(){this.constructor.call(this,SVG.create("tspan"))}this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",create:function(e){return document.createElementNS(this.ns,e)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]||null),e.parent=this),this},put:function(e,t){return this.add(e,t),e},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e<this.children().length){var t=this.children()[e];this.children().splice(e,1),this.node.removeChild(t.node),t.parent=null}return this},defs:function(){return this._defs||(this._defs=this.put(new SVG.Defs,0))},level:function(){return this.remove(d).put(this.defs(),0)},group:function(){return this.put(new SVG.G)},rect:function(e,t){return this.put((new SVG.Rect).size(e,t))},circle:function(e){return this.ellipse(e)},ellipse:function(e,t){return this.put((new SVG.Ellipse).size(e,t))},polyline:function(e){return this.put((new SVG.Polyline).plot(e))},polygon:function(e){return this.put((new SVG.Polygon).plot(e))},path:function(e){return this.put((new SVG.Path).plot(e))},image:function(e,t,n){return t=t!=null?t:100,this.put((new SVG.Image).load(e).size(t,n!=null?n:t))},text:function(e){return this.put((new SVG.Text).text(e))},gradient:function(e,t){return this.defs().gradient(e,t)},stage:function(){var e,t=this;return e=function(){document.readyState==="complete"?(t.attr("style","position:absolute;"),setTimeout(function(){t.attr("style","position:relative;")},5)):setTimeout(e,10)},e(),this}},SVG.Element=function(t){this.node=t,this.attrs={},this.trans={x:0,y:0,scaleX:1,scaleY:1,rotation:0,skewX:0,skewY:0}},SVG.extend(SVG.Element,{move:function(e,t){return this.attr({x:e,y:t})},size:function(e,t){return this.attr({width:e,height:t})},center:function(e,t){var n=this.bbox();return this.move(e-n.width/2,t-n.height/2)},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},attr:function(e,t,n){if(arguments.length<2){if(typeof e!="object")return this._isStyle(e)?e=="text"?this.content:e=="leading"?this[e]:this.style[e]:this.attrs[e];for(t in e)this.attr(t,e[t])}else{this.attrs[e]=t;if(e=="x"&&this._isText())for(var r=this.lines.length-1;r>=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e){if(typeof e=="string")return this.trans[e];var t,n=[],r=this.bbox(),i=this.attr("transform")||"",s=i.match(/[a-z]+\([^\)]+\)/g)||[];for(t in e)e[t]!=null&&(this.trans[t]=e[t]);return e=this.trans,e.rotation!=0&&n.push("rotate("+e.rotation+","+(e.cx!=null?e.cx:r.cx)+","+(e.cy!=null?e.cy:r.cy)+")"),n.push("scale("+e.scaleX+","+e.scaleY+")"),e.skewX!=0&&n.push("skewX("+e.skewX+")"),e.skewY!=0&&n.push("skewY("+e.skewY+")"),n.push("translate("+e.x+","+e.y+")"),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return{x:e.x+this.trans.x,y:e.y+this.trans.y,cx:e.x+this.trans.x+e.width/2,cy:e.y+this.trans.y+e.height/2,width:e.width,height:e.height}},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t},_isStyle:function(e){return typeof e=="string"&&this._isText()?/^font|text|leading/.test(e):!1},_isText:function(){return this instanceof SVG.Text}}),SVG.G=function(){this.constructor.call(this,SVG.create("g"))},SVG.G.prototype=new SVG.Element,SVG.extend(SVG.G,SVG.Container),SVG.extend(SVG.Element,{siblings:function(){return this.parent.children()},forward:function(){var e=this.siblings().indexOf(this);return this.parent.remove(this).put(this,e+1)},backward:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,e-1),this},front:function(){return this.parent.remove(this).put(this)},back:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}}),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container);var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_clip_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.parent.defs().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){return this.put(new SVG.Clip)}});var t=0;SVG.Gradient=function(n){this.constructor.call(this,SVG.create(n+"Gradient")),this.id="svgjs_grad_"+t++,this.attr("id",this.id),this.type=n},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){return this.put(new SVG.Stop(e))},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=this.put(new SVG.Gradient(e));return t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t,n="",r=["opacity","color"];for(t=r.length-1;t>=0;t--)e[r[t]]!=null&&(n+="stop-"+r[t]+":"+e[r[t]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attrs.offset||0)+"%",style:n})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg"));var n=document.createElement("div");n.style.cssText="position:relative;width:100%;height:100%;",typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",width:"100%",height:"100%"}).attr("xlink",SVG.xlink,SVG.ns).defs(),t.appendChild(n),n.appendChild(this.node),this.stage()},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:(t!=null?t:e)/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Poly={plot:function(e){return this.attr("points",e||"0,0")},move:function(e,t){return this.transform({x:e,y:t})}},SVG.Polyline=function(){this.constructor.call(this,SVG.create("polyline"))},SVG.Polyline.prototype=new SVG.Shape,SVG.extend(SVG.Polyline,SVG.Poly),SVG.Polygon=function(){this.constructor.call(this,SVG.create("polygon"))},SVG.Polygon.prototype=new SVG.Shape,SVG.extend(SVG.Polygon,SVG.Poly),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{plot:function(e){return this.attr("d",e||"M0,0")},move:function(e,t){return this.transform({x:e,y:t})}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("xlink:href",e,SVG.xlink)}});var n=["size","family","weight","stretch","variant","style"];SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica","text-anchor":"start"},this.leading=1.2},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text",this.lines=[];var t,n,i=this._style(),s=this.parentDoc(),o=e.split("\n"),u=this.style["font-size"];while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t<l;t++)n=(new r).text(o[t]).attr({dy:u*this.leading-(t==0?u*.3:0),x:this.attrs.x||0,style:i}),this.node.appendChild(n.node),this.lines.push(n);return this.attr("style",i)},_style:function(){var e,t="";for(e=n.length-1;e>=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");return t+="text-anchor:"+this.style["text-anchor"]+";",t}}),r.prototype=new SVG.Shape,SVG.extend(r,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}});var i=["width","opacity","linecap","linejoin","miterlimit","dasharray","dashoffset"],s=["opacity","rule"];SVG.extend(SVG.Shape,{fill:function(e){var t;e.color!=null&&this.attr("fill",e.color);for(t=s.length-1;t>=0;t--)e[s[t]]!=null&&this.attr("fill-"+s[t],e[s[t]]);return this},stroke:function(e){var t;e.color&&this.attr("stroke",e.color);for(t=i.length-1;t>=0;t--)e[i[t]]!=null&&this.attr("stroke-"+i[t],e[i[t]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e,t,n){var r=this.bbox();return this.transform({rotation:e||0,cx:t==null?r.cx:t,cy:n==null?r.cx:n})},skew:function(e,t){return this.transform({skewX:e||0,skewY:t||0})}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform({x:e,y:t})}}),SVG.extend(SVG.Text,{font:function(e){var t,r={};for(t in e)t=="leading"?r[t]=e[t]:t=="anchor"?r["text-anchor"]=e[t]:n.indexOf(t)>-1?r["font-"+t]=e[t]:void 0;return this.attr(r).text(this.content)}})}).call(this); \ No newline at end of file
diff --git a/src/arrange.js b/src/arrange.js
index 001da75..ac53e43 100644
--- a/src/arrange.js
+++ b/src/arrange.js
@@ -2,7 +2,7 @@
// add backward / forward functionality to elements
SVG.extend(SVG.Element, {
- // get all siblings, including me
+ // get all siblings, including myself
siblings: function() {
return this.parent.children();
},
@@ -10,9 +10,8 @@ SVG.extend(SVG.Element, {
// send given element one step forwards
forward: function() {
var i = this.siblings().indexOf(this);
- this.parent.remove(this).add(this, i + 1);
- return this;
+ return this.parent.remove(this).put(this, i + 1);
},
// send given element one step backwards
@@ -29,9 +28,7 @@ SVG.extend(SVG.Element, {
// send given element all the way to the front
front: function() {
- this.parent.remove(this).add(this);
-
- return this;
+ return this.parent.remove(this).put(this);
},
// send given element all the way to the back
diff --git a/src/clip.js b/src/clip.js
index 9b106b4..6d820df 100644
--- a/src/clip.js
+++ b/src/clip.js
@@ -4,6 +4,8 @@ var clipID = 0;
SVG.Clip = function Clip() {
this.constructor.call(this, SVG.create('clipPath'));
+
+ // set unique id
this.id = 'svgjs_clip_' + (clipID++);
this.attr('id', this.id);
};
@@ -35,12 +37,9 @@ SVG.extend(SVG.Element, {
// add def-specific functions
SVG.extend(SVG.Defs, {
- // define clippath
+ // create clippath
clip: function() {
- var e = new SVG.Clip();
- this.add(e);
-
- return e;
+ return this.put(new SVG.Clip());
}
}); \ No newline at end of file
diff --git a/src/container.js b/src/container.js
index f436d4d..6482450 100644
--- a/src/container.js
+++ b/src/container.js
@@ -1,6 +1,8 @@
+// methods for container elements like SVG.Doc, SVG.Group, SVG.Defs, ...
SVG.Container = {
+ // add given element at goven position
add: function(e, i) {
if (!this.has(e)) {
i = i == null ? this.children().length : i;
@@ -12,23 +14,28 @@ SVG.Container = {
return this;
},
+ // basically does the same as add() but returns the added element rather than 'this'
put: function(e, i) {
this.add(e, i);
return e;
},
+ // chacks if the given element is a child
has: function(e) {
return this.children().indexOf(e) >= 0;
},
+ // returns all child elements and initializes store array if non existant
children: function() {
return this._children || (this._children = []);
},
+ // remove a given child element
remove: function(e) {
return this.removeAt(this.children().indexOf(e));
},
+ // remove child element at a given position
removeAt: function(i) {
if (0 <= i && i < this.children().length) {
var e = this.children()[i];
@@ -40,58 +47,63 @@ SVG.Container = {
return this;
},
+ // returns defs element and initializes store array if non existant
defs: function() {
- if (this._defs == null) {
- this._defs = new SVG.Defs();
- this.add(this._defs, 0);
- }
-
- return this._defs;
+ return this._defs || (this._defs = this.put(new SVG.Defs(), 0));
},
+ // re-level defs to first positon in element stack
level: function() {
- var d = this.defs();
-
- return this.remove(d).add(d, 0);
+ return this.remove(d).put(this.defs(), 0);
},
+ // create a group element
group: function() {
return this.put(new SVG.G());
},
+ // create a rect element
rect: function(w, h) {
return this.put(new SVG.Rect().size(w, h));
},
+ // create circle element, based on ellipse
circle: function(d) {
return this.ellipse(d);
},
+ // create and ellipse
ellipse: function(w, h) {
return this.put(new SVG.Ellipse().size(w, h));
},
+ // create a polyline element
polyline: function(p) {
return this.put(new SVG.Polyline().plot(p));
},
+ // create a polygon element
polygon: function(p) {
return this.put(new SVG.Polygon().plot(p));
},
+ // create a path element
path: function(d) {
return this.put(new SVG.Path().plot(d));
},
+ // create image element, load image and set its size
image: function(s, w, h) {
w = w != null ? w : 100;
return this.put(new SVG.Image().load(s).size(w, h != null ? h : w));
},
+ // create text element
text: function(t) {
return this.put(new SVG.Text().text(t));
},
+ // create element in defs
gradient: function(t, b) {
return this.defs().gradient(t, b);
},
diff --git a/src/doc.js b/src/doc.js
index 2457815..3d563a3 100644
--- a/src/doc.js
+++ b/src/doc.js
@@ -16,10 +16,11 @@ SVG.Doc = function Doc(e) {
attr('xlink', SVG.xlink, SVG.ns).
defs();
+ // add elements
e.appendChild(w);
w.appendChild(this.node);
- // ensure
+ // ensure correct rendering for safari
this.stage();
};
diff --git a/src/element.js b/src/element.js
index e849867..e7701ce 100644
--- a/src/element.js
+++ b/src/element.js
@@ -1,7 +1,12 @@
SVG.Element = function Element(n) {
+ // keep reference to the element node
this.node = n;
+
+ // initialize attribute store
this.attrs = {};
+
+ // initialize transformations store
this.trans = {
x: 0,
y: 0,
@@ -11,8 +16,6 @@ SVG.Element = function Element(n) {
skewX: 0,
skewY: 0
};
-
- this._s = ('size family weight stretch variant style').split(' ');
};
// Add element-specific functions
@@ -48,9 +51,11 @@ SVG.extend(SVG.Element, {
// set svg element attribute
attr: function(a, v, n) {
if (arguments.length < 2) {
+ // apply every attribute individually if an object is passed
if (typeof a == 'object')
for (v in a) this.attr(v, a[v]);
-
+
+ // act as a getter for style attributes
else if (this._isStyle(a))
return a == 'text' ?
this.content :
@@ -58,19 +63,26 @@ SVG.extend(SVG.Element, {
this[a] :
this.style[a];
+ // act as a getter if the first and only argument is not an object
else
return this.attrs[a];
} else {
+ // store value
this.attrs[a] = v;
+
+ // treat x differently on text elements
if (a == 'x' && this._isText())
for (var i = this.lines.length - 1; i >= 0; i--)
this.lines[i].attr(a, v);
+
+ // set the actual attribute
else
n != null ?
this.node.setAttributeNS(n, a, v) :
this.node.setAttribute(a, v);
+ // if the passed argument belongs to the style as well, add it there
if (this._isStyle(a)) {
a == 'text' ?
this.text(v) :
@@ -150,14 +162,15 @@ SVG.extend(SVG.Element, {
// private: find svg parent
_parent: function(pt) {
var e = this;
-
+
+ // find ancestor with given type
while (e != null && !(e instanceof pt))
e = e.parent;
return e;
},
- // private: is this text style
+ // private: tester method for style detection
_isStyle: function(a) {
return typeof a == 'string' && this._isText() ? (/^font|text|leading/).test(a) : false;
},
diff --git a/src/ellipse.js b/src/ellipse.js
index 6280792..018cd4f 100644
--- a/src/ellipse.js
+++ b/src/ellipse.js
@@ -27,8 +27,8 @@ SVG.extend(SVG.Ellipse, {
// position element by its center
center: function(x, y) {
return this.attr({
- cx: x || ((this.attrs.x || 0) + (this.attrs.rx || 0)),
- cy: y || ((this.attrs.y || 0) + (this.attrs.ry || 0))
+ cx: x || (this.attrs.x || 0) + (this.attrs.rx || 0),
+ cy: y || (this.attrs.y || 0) + (this.attrs.ry || 0)
});
}
diff --git a/src/gradient.js b/src/gradient.js
index 0239b15..b9007ef 100644
--- a/src/gradient.js
+++ b/src/gradient.js
@@ -4,10 +4,13 @@ var gradID = 0;
SVG.Gradient = function Gradient(t) {
this.constructor.call(this, SVG.create(t + 'Gradient'));
- this.id = 'svgjs_grad_' + (gradID++);
- this.type = t;
+ // set unique id
+ this.id = 'svgjs_grad_' + (gradID++);
this.attr('id', this.id);
+
+ // store type
+ this.type = t;
};
// inherit from SVG.Element
@@ -40,21 +43,20 @@ SVG.extend(SVG.Gradient, {
this;
},
- // add color stops
+ // add a color stop
at: function(o) {
- var m = new SVG.Stop(o);
- this.add(m);
-
- return m;
+ return this.put(new SVG.Stop(o));
},
// update gradient
update: function(b) {
+ // remove all stops
while (this.node.hasChildNodes())
this.node.removeChild(this.node.lastChild);
+ // invoke passed block
b(this);
-
+
return this;
},
@@ -70,8 +72,9 @@ SVG.extend(SVG.Defs, {
// define clippath
gradient: function(t, b) {
- var e = new SVG.Gradient(t);
- this.add(e);
+ var e = this.put(new SVG.Gradient(t));
+
+ // invoke passed block
b(e);
return e;
@@ -83,6 +86,7 @@ SVG.extend(SVG.Defs, {
SVG.Stop = function Stop(o) {
this.constructor.call(this, SVG.create('stop'));
+ // immediatelly build stop
this.update(o);
};
@@ -94,15 +98,18 @@ SVG.extend(SVG.Stop, {
// add color stops
update: function(o) {
- var s = '',
+ var i,
+ s = '',
a = ['opacity', 'color'];
-
- for (var i = a.length - 1; i >= 0; i--)
+
+ // build style attribute
+ for (i = a.length - 1; i >= 0; i--)
if (o[a[i]] != null)
s += 'stop-' + a[i] + ':' + o[a[i]] + ';';
-
+
+ // set attributes
return this.attr({
- offset: (o.offset != null ? o.offset : this.attr('offset') || 0) + '%',
+ offset: (o.offset != null ? o.offset : this.attrs.offset || 0) + '%',
style: s
});
}
diff --git a/src/path.js b/src/path.js
index fef5809..5b71e50 100644
--- a/src/path.js
+++ b/src/path.js
@@ -14,7 +14,7 @@ SVG.extend(SVG.Path, {
return this.attr('d', d || 'M0,0');
},
- // move path using translate
+ // move path using translate, path's don't take x and y
move: function(x, y) {
return this.transform({ x: x, y: y });
}
diff --git a/src/sugar.js b/src/sugar.js
index 10a8716..47ab35a 100644
--- a/src/sugar.js
+++ b/src/sugar.js
@@ -1,28 +1,40 @@
+// define list of available attributes for stroke and fill
+var _strokeAttr = ['width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset'],
+ _fillAttr = ['opacity', 'rule'];
+
+
// Add shape-specific functions
SVG.extend(SVG.Shape, {
// set fill color and opacity
fill: function(f) {
+ var i;
+
+ // set fill color if not null
if (f.color != null)
this.attr('fill', f.color);
-
- if (f.opacity != null)
- this.attr('fill-opacity', f.opacity);
-
+
+ // set all attributes from _fillAttr list with prependes 'fill-' if not null
+ for (i = _fillAttr.length - 1; i >= 0; i--)
+ if (f[_fillAttr[i]] != null)
+ this.attr('fill-' + _fillAttr[i], f[_fillAttr[i]]);
+
return this;
},
// set stroke color and opacity
stroke: function(s) {
+ var i;
+
+ // set stroke color if not null
if (s.color)
this.attr('stroke', s.color);
- var a = ('width opacity linecap linejoin miterlimit dasharray dashoffset').split(' ');
-
- for (var i = a.length - 1; i >= 0; i--)
- if (s[a[i]] != null)
- this.attr('stroke-' + a[i], s[a[i]]);
+ // set all attributes from _strokeAttr list with prependes 'stroke-' if not null
+ for (i = _strokeAttr.length - 1; i >= 0; i--)
+ if (s[_strokeAttr[i]] != null)
+ this.attr('stroke-' + _strokeAttr[i], s[_strokeAttr[i]]);
return this;
}
@@ -75,7 +87,7 @@ SVG.extend(SVG.Text, {
a[k] = o[k] :
k == 'anchor' ?
a['text-anchor'] = o[k] :
- this._s.indexOf(k) > -1 ?
+ _styleAttr.indexOf(k) > -1 ?
a['font-'+ k] = o[k] :
void 0;
diff --git a/src/svg.js b/src/svg.js
index 876b9aa..fd5f967 100644
--- a/src/svg.js
+++ b/src/svg.js
@@ -1,12 +1,15 @@
this.SVG = {
+ // define default namespaces
ns: 'http://www.w3.org/2000/svg',
xlink: 'http://www.w3.org/1999/xlink',
+ // method for element creation
create: function(e) {
return document.createElementNS(this.ns, e);
},
+ // method for extending objects
extend: function(o, m) {
for (var k in m)
o.prototype[k] = m[k];
diff --git a/src/text.js b/src/text.js
index 301373c..3fe3f0b 100644
--- a/src/text.js
+++ b/src/text.js
@@ -1,10 +1,14 @@
+// list font style attributes as they should be applied to style
+var _styleAttr = ['size', 'family', 'weight', 'stretch', 'variant', 'style'];
+
+
SVG.Text = function Text() {
this.constructor.call(this, SVG.create('text'));
+ // define default style
this.style = { 'font-size': 16, 'font-family': 'Helvetica', 'text-anchor': 'start' };
this.leading = 1.2;
- this.lines = [];
};
// inherit from SVG.Element
@@ -14,6 +18,7 @@ SVG.Text.prototype = new SVG.Shape();
SVG.extend(SVG.Text, {
text: function(t) {
+ // update the content
this.content = t = t || 'text';
this.lines = [];
@@ -23,31 +28,37 @@ SVG.extend(SVG.Text, {
a = t.split("\n"),
f = this.style['font-size'];
+ // remove existing child nodes
while (this.node.hasChildNodes())
this.node.removeChild(this.node.lastChild);
+ // build new lines
for (i = 0, l = a.length; i < l; i++) {
+ // create new tspan and set attributes
n = new TSpan().
text(a[i]).
attr({
dy: f * this.leading - (i == 0 ? f * 0.3 : 0),
- x: (this.attr('x') || 0),
+ x: (this.attrs.x || 0),
style: s
});
+ // add new tspan
this.node.appendChild(n.node);
this.lines.push(n);
};
+ // set style
return this.attr('style', s);
},
+ // build style based on _styleAttr
_style: function() {
- var i, o = '', s = this._s;
+ var i, o = '';
- for (i = s.length - 1; i >= 0; i--)
- if (this.style['font-' + s[i]] != null)
- o += 'font-' + s[i] + ':' + this.style['font-' + s[i]] + ';';
+ for (i = _styleAttr.length - 1; i >= 0; i--)
+ if (this.style['font-' + _styleAttr[i]] != null)
+ o += 'font-' + _styleAttr[i] + ':' + this.style['font-' + _styleAttr[i]] + ';';
o += 'text-anchor:' + this.style['text-anchor'] + ';';