]> source.dussan.org Git - svg.js.git/commitdiff
Added anchor option for text element
authorwout <wout@impinc.co.uk>
Thu, 20 Dec 2012 21:19:45 +0000 (22:19 +0100)
committerwout <wout@impinc.co.uk>
Thu, 20 Dec 2012 21:19:45 +0000 (22:19 +0100)
README.md
dist/svg.js
dist/svg.min.js
src/text.js

index e6ee3aba942b50966e4f95278be96a5b3eb6beb6..dc9225e9da02dfdebf852f2b10c6d0847a73a11d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # Svg.js
 
-Svg.js is a lightweight (2k gzipped) library for manipulating SVG.
+Svg.js is a lightweight (2.5k gzipped) library for manipulating SVG.
 
 Svg.js is licensed under the terms of the MIT License.
 
@@ -93,6 +93,26 @@ rect.remove();
 ```
 
 
+### Text element
+Text elements can be created with a single style applied.
+```javascript
+var text = draw.text({
+  text: "svg\nto\nthe\npoint.",
+  x:    300,
+  y:    0
+});
+```
+Styling text can be done using the 'font()' method:
+```javascript
+text.font({
+  family: 'Helvetica',
+  size: 36,
+  anchor: 'middle',
+  leading: 1.5
+});
+```
+
+
 ### Image element
 When creating images the width and height values should be defined:
 ```javascript
index b338b901a79bd00d08dc8aa87ef352d3fe8375da..db34602c63170890a528178ab8aeaf3b3d4c2ce6 100644 (file)
     
     this.style = { 'font-size':  16 };
     this.leading = 1.2;
+    this.anchor = 'start';
   };
   
   // inherit from SVG.Element
         if (o[a[i]] != null)
           this.style['font-' + a[i]] = o[a[i]];
      
-      a = ('leading kerning anchor').split(' ');
+      a = ('leading anchor').split(' ');
       
       for (i = a.length - 1; i >= 0; i--)
         if (o[a[i]] != null)
         if (this.style['font-' + a[i]] != null)
           s += 'font-' + a[i] + ':' + this.style['font-' + a[i]] + ';';
       
-      a = ('leading kerning anchor').split(' ');
-      
-      for (i = a.length - 1; i >= 0; i--)
-        if (this[a[i]] != null)
-          s += a[i] + ':' + this[a[i]] + ';';
-      
+      if (this.anchor != null)
+        s += 'text-anchor:' + this.anchor + ';';
+        
       return s;
     }
     
index ee726d94095c9161139d98675defc47f4fdfd893..6a0e45d10a9bd68b24ccd89921582c16815febe7 100644 (file)
@@ -1,2 +1,2 @@
 /* svg.js 0.1a - svg container element group arrange clip doc defs shape rect circle ellipse path image text sugar - svgjs.com/license */
-(function(){function t(){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]}},this.svg=function(e){return new SVG.Doc(e)},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]),e.parent=this),this},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},levelDefs:function(){var e=this.defs();return this.remove(e).add(e,0),this},group:function(){var e=new SVG.G;return this.add(e),e},rect:function(e){return this.place(new SVG.Rect,e)},circle:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.r||e.radius?t.width=t.height=(e.r||e.radius)*2:t.width=t.height=e.width||e.height),this.place(new SVG.Circle,t)},ellipse:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.width&&(t.width=e.width),e.height&&(t.height=e.height),e.rx&&(t.width=e.rx*2),e.ry&&(t.height=e.ry*2)),this.place(new SVG.Ellipse,t)},path:function(e){return this.place(new SVG.Path,e)},image:function(e){return this.place(new SVG.Image,e)},text:function(e){return this.place(new SVG.Text,e)},place:function(e,t){return t!=null&&(t.x!=null&&t.y!=null&&e.move(t.x,t.y),t.width!=null&&t.height!=null&&e.size(t.width,t.height),t.data!=null?e.data(t.data):t.src!=null?e.load(t.src):t.text!=null?e.text(t.text):void 0),this.add(e),e}},SVG.Element=function(t){this.node=t,this.attrs={}},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})},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},mother:function(){return this.parentDoc()},attr:function(e,t,n){if(arguments.length<2){if(typeof e!="object")return this.attrs[e];for(t in e)this.attr(t,e[t])}else this.attrs[e]=t,n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);return this},transform:function(e,t){var n=[],r=this.attr("transform")||"",i=r.match(/([a-z]+\([^\)]+\))/g)||[];if(t!==!0){var s=e.match(/^([A-Za-z\-]+)/)[1],t=new RegExp("^"+s);for(var o=0,r=i.length;o<r;o++)t.test(i[o])||n.push(i[o])}else n=i;return n.push(e),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return e.cx=e.x+e.width/2,e.cy=e.y+e.height/2,e},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t}}),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.mother().children()},forward:function(){var e=this.siblings().indexOf(this);return this.mother().remove(this).add(this,e+1),this},backward:function(){var e,t=this.mother();return t.levelDefs(),e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,e-1),this},front:function(){return this.mother().remove(this).add(this),this},back:function(){var e,t=this.mother();return t.levelDefs(),e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}});var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="_"+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.mother().defs().clipPath();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg")),this.attr("xmlns",SVG.ns),this.attr("version","1.1"),this.attr("xlink",SVG.xlink,SVG.ns),this.defs(),typeof t=="string"&&(t=document.getElementById(t)),t.appendChild(this.node)},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container),SVG.extend(SVG.Defs,{clipPath:function(){var e=new SVG.Clip;return this.add(e),e}}),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.Circle=function(){this.constructor.call(this,SVG.create("circle"))},SVG.Circle.prototype=new SVG.Shape,SVG.extend(SVG.Circle,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e){return this.attr("r",e/2).center()},center:function(e,t){var n=this.attrs.r||0;return this.attr({cx:e||(this.attrs.x||0)+n,cy:t||(this.attrs.y||0)+n})}}),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/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.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{data:function(e){return this.attr("d",e)}}),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("href",e,SVG.xlink)}}),SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16},this.leading=1.2},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text";var n,r=this.parentDoc(),i=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(n=0,l=i.length;n<l;n++)this.node.appendChild((new t).text(i[n]).attr("style",this._style()).attr({dy:this.style["font-size"]*this.leading,x:this.attr("x")||0}).node);return this},font:function(e){var t,n="size family weight stretch variant style".split(" ");for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&(this.style["font-"+n[t]]=e[n[t]]);n="leading kerning anchor".split(" ");for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&(this[n[t]]=e[n[t]]);return this.text(this.content)},_style:function(){var e,t="",n="size family weight stretch variant style".split(" ");for(e=n.length-1;e>=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");n="leading kerning anchor".split(" ");for(e=n.length-1;e>=0;e--)this[n[e]]!=null&&(t+=n[e]+":"+this[n[e]]+";");return t}}),t.prototype=new SVG.Shape,SVG.extend(t,{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){var t=this.bbox();return e.x==null&&(e.x=t.cx),e.y==null&&(e.y=t.cy),this.transform("rotate("+(e.deg||0)+" "+e.x+" "+e.y+")",e.relative)}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform("translate("+e+" "+t+")")}})}).call(this);
\ No newline at end of file
+(function(){function t(){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]}},this.svg=function(e){return new SVG.Doc(e)},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]),e.parent=this),this},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},levelDefs:function(){var e=this.defs();return this.remove(e).add(e,0),this},group:function(){var e=new SVG.G;return this.add(e),e},rect:function(e){return this.place(new SVG.Rect,e)},circle:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.r||e.radius?t.width=t.height=(e.r||e.radius)*2:t.width=t.height=e.width||e.height),this.place(new SVG.Circle,t)},ellipse:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.width&&(t.width=e.width),e.height&&(t.height=e.height),e.rx&&(t.width=e.rx*2),e.ry&&(t.height=e.ry*2)),this.place(new SVG.Ellipse,t)},path:function(e){return this.place(new SVG.Path,e)},image:function(e){return this.place(new SVG.Image,e)},text:function(e){return this.place(new SVG.Text,e)},place:function(e,t){return t!=null&&(t.x!=null&&t.y!=null&&e.move(t.x,t.y),t.width!=null&&t.height!=null&&e.size(t.width,t.height),t.data!=null?e.data(t.data):t.src!=null?e.load(t.src):t.text!=null?e.text(t.text):void 0),this.add(e),e}},SVG.Element=function(t){this.node=t,this.attrs={}},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})},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},mother:function(){return this.parentDoc()},attr:function(e,t,n){if(arguments.length<2){if(typeof e!="object")return this.attrs[e];for(t in e)this.attr(t,e[t])}else this.attrs[e]=t,n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);return this},transform:function(e,t){var n=[],r=this.attr("transform")||"",i=r.match(/([a-z]+\([^\)]+\))/g)||[];if(t!==!0){var s=e.match(/^([A-Za-z\-]+)/)[1],t=new RegExp("^"+s);for(var o=0,r=i.length;o<r;o++)t.test(i[o])||n.push(i[o])}else n=i;return n.push(e),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return e.cx=e.x+e.width/2,e.cy=e.y+e.height/2,e},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t}}),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.mother().children()},forward:function(){var e=this.siblings().indexOf(this);return this.mother().remove(this).add(this,e+1),this},backward:function(){var e,t=this.mother();return t.levelDefs(),e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,e-1),this},front:function(){return this.mother().remove(this).add(this),this},back:function(){var e,t=this.mother();return t.levelDefs(),e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}});var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="_"+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.mother().defs().clipPath();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg")),this.attr("xmlns",SVG.ns),this.attr("version","1.1"),this.attr("xlink",SVG.xlink,SVG.ns),this.defs(),typeof t=="string"&&(t=document.getElementById(t)),t.appendChild(this.node)},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container),SVG.extend(SVG.Defs,{clipPath:function(){var e=new SVG.Clip;return this.add(e),e}}),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.Circle=function(){this.constructor.call(this,SVG.create("circle"))},SVG.Circle.prototype=new SVG.Shape,SVG.extend(SVG.Circle,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e){return this.attr("r",e/2).center()},center:function(e,t){var n=this.attrs.r||0;return this.attr({cx:e||(this.attrs.x||0)+n,cy:t||(this.attrs.y||0)+n})}}),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/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.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{data:function(e){return this.attr("d",e)}}),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("href",e,SVG.xlink)}}),SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16},this.leading=1.2,this.anchor="start"},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text";var n,r=this.parentDoc(),i=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(n=0,l=i.length;n<l;n++)this.node.appendChild((new t).text(i[n]).attr("style",this._style()).attr({dy:this.style["font-size"]*this.leading,x:this.attr("x")||0}).node);return this},font:function(e){var t,n="size family weight stretch variant style".split(" ");for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&(this.style["font-"+n[t]]=e[n[t]]);n="leading anchor".split(" ");for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&(this[n[t]]=e[n[t]]);return this.text(this.content)},_style:function(){var e,t="",n="size family weight stretch variant style".split(" ");for(e=n.length-1;e>=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");return this.anchor!=null&&(t+="text-anchor:"+this.anchor+";"),t}}),t.prototype=new SVG.Shape,SVG.extend(t,{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){var t=this.bbox();return e.x==null&&(e.x=t.cx),e.y==null&&(e.y=t.cy),this.transform("rotate("+(e.deg||0)+" "+e.x+" "+e.y+")",e.relative)}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform("translate("+e+" "+t+")")}})}).call(this);
\ No newline at end of file
index 3b502160cbed5886af584c6526c8f0697dc93d12..655815d797eef3e2179cef98fcdb5faf6a5cb126 100644 (file)
@@ -4,6 +4,7 @@ SVG.Text = function Text() {
   
   this.style = { 'font-size':  16 };
   this.leading = 1.2;
+  this.anchor = 'start';
 };
 
 // inherit from SVG.Element
@@ -38,7 +39,7 @@ SVG.extend(SVG.Text, {
       if (o[a[i]] != null)
         this.style['font-' + a[i]] = o[a[i]];
    
-    a = ('leading kerning anchor').split(' ');
+    a = ('leading anchor').split(' ');
     
     for (i = a.length - 1; i >= 0; i--)
       if (o[a[i]] != null)
@@ -54,12 +55,9 @@ SVG.extend(SVG.Text, {
       if (this.style['font-' + a[i]] != null)
         s += 'font-' + a[i] + ':' + this.style['font-' + a[i]] + ';';
     
-    a = ('leading kerning anchor').split(' ');
-    
-    for (i = a.length - 1; i >= 0; i--)
-      if (this[a[i]] != null)
-        s += a[i] + ':' + this[a[i]] + ';';
-    
+    if (this.anchor != null)
+      s += 'text-anchor:' + this.anchor + ';';
+      
     return s;
   }