From 331068a979be8b735e92f074ce0d2438f5f2127b Mon Sep 17 00:00:00 2001 From: wout Date: Sat, 29 Dec 2012 15:20:50 +0100 Subject: [PATCH] Added polyline and polygon, removed cicle --- README.md | 73 +++++++++++++++++-------- Rakefile | 2 +- dist/svg.js | 138 +++++++++++++++++++++++------------------------ dist/svg.min.js | 4 +- src/circle.js | 35 ------------ src/container.js | 54 ++++++++----------- src/ellipse.js | 2 +- src/path.js | 2 +- src/poly.js | 38 +++++++++++++ src/text.js | 5 +- 10 files changed, 186 insertions(+), 167 deletions(-) delete mode 100644 src/circle.js create mode 100644 src/poly.js diff --git a/README.md b/README.md index 7cc5ce0..c5130e5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Svg.js -Svg.js is a lightweight (less than 3k gzipped) library for manipulating SVG. +Svg.js is a lightweight (3k gzipped) library for manipulating SVG. Svg.js is licensed under the terms of the MIT License. @@ -35,6 +35,7 @@ var draw = svg('paper').size('100%', '100%'); ### Rect Rects have two arguments, their `width` and `height`: + ```javascript var text = draw.rect(100, 100); ``` @@ -42,16 +43,66 @@ var text = draw.rect(100, 100); ### Ellipse Ellipses, like rects, have two arguments, their `width` and `height`: + ```javascript var ellipse = draw.ellipse(100, 100); ``` ### Circle The only argument necessary for a circle is the diameter: + ```javascript var circle = draw.circle(100); ``` +_Note that this generates an `` element rather than a ``. This choice has been made to keep the size of the library down._ + + +### Polyline +The polyline element defines a set of connected straight line segments. Typically, polyline elements define open shapes: + +```javascript +// polyline(x1, y1, x2, y2) +var polyline = draw.polyline('10,20 30,40 50,60'); +``` + +Polyline strings consist of a list of points separated by spaces: `x,y x,y x,y`. + +_Note that the svg `` element is not implemented. Therefore you might want to use the `polyline()` method to create a line._ + + +### Polygon +The polygon element, unlike the polyline element, defines a closed shape consisting of a set of connected straight line segments: + +```javascript +// polyline('x,y x,y x,y') +var polygon = draw.polygon('10,20 30,40 50,60'); +``` + +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 automatically connected. + + +### Path +The path string is similar to the polygon string but much more complex in order to support curves: + +```javascript +// polyline('path data') +var path = draw.path('M10,20L30,40'); +``` + +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); +``` + + ### Text The first argument of a text element is the actual text content: ```javascript @@ -76,26 +127,6 @@ text.font({ ``` -### 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); -``` - - -### Path -Pass the path string as the first argument: - -```javascript -var path = draw.path('M10,20L30,40').attr({ fill: '#9dffd3' }); -``` - -For more details on path data strings, please read the SVG documentation: -http://www.w3.org/TR/SVG/paths.html#PathData - - ## Manipulating elements ### Attributes diff --git a/Rakefile b/Rakefile index fe9905b..6051334 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ SVGJS_VERSION = '0.1a' # all available modules in the correct loading order -MODULES = %w[ svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar ] +MODULES = %w[ svg container element group arrange defs clip gradient doc shape rect ellipse poly path image text sugar ] # how many bytes in a "kilobyte" KILO = 1024 diff --git a/dist/svg.js b/dist/svg.js index d8c2b48..5d3a670 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js v0.1-37-gc8be3da - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ +/* svg.js v0.1-38-ga20c0b1 - svg container element group arrange defs clip gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -29,6 +29,11 @@ return this; }, + put: function(e, i) { + this.add(e, i); + return e; + }, + has: function(e) { return this.children().indexOf(e) >= 0; }, @@ -64,59 +69,44 @@ level: function() { var d = this.defs(); - this.remove(d).add(d, 0); - - return this; + return this.remove(d).add(d, 0); }, group: function() { - var e = new SVG.G(); - this.add(e); - - return e; + return this.put(new SVG.G()); }, rect: function(w, h) { - var e = new SVG.Rect().size(w, h); - this.add(e); - - return e; + return this.put(new SVG.Rect().size(w, h)); }, - circle: function(r) { - var e = new SVG.Circle().size(r); - this.add(e); - - return e; + circle: function(d) { + return this.ellipse(d); }, ellipse: function(w, h) { - var e = new SVG.Ellipse().size(w, h); - this.add(e); - - return e; + return this.put(new SVG.Ellipse().size(w, h)); + }, + + polyline: function(p) { + return this.put(new SVG.Polyline().plot(p)); + }, + + polygon: function(p) { + return this.put(new SVG.Polygon().plot(p)); }, path: function(d) { - var e = new SVG.Path().plot(d); - this.add(e); - - return e; + return this.put(new SVG.Path().plot(d)); }, image: function(s, w, h) { w = w != null ? w : 100; - var e = new SVG.Image().load(s).size(w, h != null ? h : w); - this.add(e); - - return e; + return this.put(new SVG.Image().load(s).size(w, h != null ? h : w)); }, text: function(t) { - var e = new SVG.Text().text(t); - this.add(e); - - return e; + return this.put(new SVG.Text().text(t)); }, gradient: function(t, b) { @@ -587,41 +577,6 @@ // inherit from SVG.Shape SVG.Rect.prototype = new SVG.Shape(); - SVG.Circle = function Circle() { - this.constructor.call(this, SVG.create('circle')); - }; - - // inherit from SVG.Shape - SVG.Circle.prototype = new SVG.Shape(); - - // Add circle-specific functions - SVG.extend(SVG.Circle, { - - // custom move function - move: function(x, y) { - this.attrs.x = x; - this.attrs.y = y; - - return this.center(); - }, - - // custom size function (no height value here!) - size: function(w) { - return this.attr('r', w / 2).center(); - }, - - // position element by its center - center: function(x, y) { - var r = this.attrs.r || 0; - - return this.attr({ - cx: x || (this.attrs.x || 0) + r, - cy: y || (this.attrs.y || 0) + r - }); - } - - }); - SVG.Ellipse = function Ellipse() { this.constructor.call(this, SVG.create('ellipse')); }; @@ -643,7 +598,7 @@ // custom size function size: function(w, h) { return this. - attr({ rx: w / 2, ry: h / 2 }). + attr({ rx: w / 2, ry: (h != null ? h : w) / 2 }). center(); }, @@ -658,6 +613,44 @@ }); + SVG.Poly = { + + // set polygon data with default zero point if no data is passed + plot: function(p) { + return this.attr('points', p || '0,0'); + }, + + // move path using translate + move: function(x, y) { + return this.transform({ x: x, y: y }); + } + + }; + + + + SVG.Polyline = function Polyline() { + this.constructor.call(this, SVG.create('polyline')); + }; + + // inherit from SVG.Shape + SVG.Polyline.prototype = new SVG.Shape(); + + // Add polygon-specific functions + SVG.extend(SVG.Polyline, SVG.Poly); + + + + SVG.Polygon = function Polygon() { + this.constructor.call(this, SVG.create('polygon')); + }; + + // inherit from SVG.Shape + SVG.Polygon.prototype = new SVG.Shape(); + + // Add polygon-specific functions + SVG.extend(SVG.Polygon, SVG.Poly); + SVG.Path = function Path() { this.constructor.call(this, SVG.create('path')); }; @@ -670,7 +663,7 @@ // set path data plot: function(d) { - return this.attr('d', d || 'M0,0L0,0'); + return this.attr('d', d || 'M0,0'); }, // move path using translate @@ -718,7 +711,8 @@ var i, n, s = this._style(), p = this.parentDoc(), - a = t.split("\n"); + a = t.split("\n"), + f = this.style['font-size']; while (this.node.hasChildNodes()) this.node.removeChild(this.node.lastChild); @@ -727,7 +721,7 @@ n = new TSpan(). text(a[i]). attr({ - dy: this.style['font-size'] * this.leading, + dy: f * this.leading - (i == 0 ? f * 0.3 : 0), x: (this.attr('x') || 0), style: s }); diff --git a/dist/svg.min.js b/dist/svg.min.js index 508e0cc..3a4529e 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js v0.1-37-gc8be3da - svg container element group arrange defs clip gradient doc shape rect circle ellipse 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},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=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.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,{plot:function(e){return this.attr("d",e||"M0,0L0,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");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t=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-38-ga20c0b1 - 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=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=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 diff --git a/src/circle.js b/src/circle.js deleted file mode 100644 index 059813e..0000000 --- a/src/circle.js +++ /dev/null @@ -1,35 +0,0 @@ - -SVG.Circle = function Circle() { - this.constructor.call(this, SVG.create('circle')); -}; - -// inherit from SVG.Shape -SVG.Circle.prototype = new SVG.Shape(); - -// Add circle-specific functions -SVG.extend(SVG.Circle, { - - // custom move function - move: function(x, y) { - this.attrs.x = x; - this.attrs.y = y; - - return this.center(); - }, - - // custom size function (no height value here!) - size: function(w) { - return this.attr('r', w / 2).center(); - }, - - // position element by its center - center: function(x, y) { - var r = this.attrs.r || 0; - - return this.attr({ - cx: x || (this.attrs.x || 0) + r, - cy: y || (this.attrs.y || 0) + r - }); - } - -}); \ No newline at end of file diff --git a/src/container.js b/src/container.js index 9e73758..36988ea 100644 --- a/src/container.js +++ b/src/container.js @@ -12,6 +12,11 @@ SVG.Container = { return this; }, + put: function(e, i) { + this.add(e, i); + return e; + }, + has: function(e) { return this.children().indexOf(e) >= 0; }, @@ -47,59 +52,44 @@ SVG.Container = { level: function() { var d = this.defs(); - this.remove(d).add(d, 0); - - return this; + return this.remove(d).add(d, 0); }, group: function() { - var e = new SVG.G(); - this.add(e); - - return e; + return this.put(new SVG.G()); }, rect: function(w, h) { - var e = new SVG.Rect().size(w, h); - this.add(e); - - return e; + return this.put(new SVG.Rect().size(w, h)); }, - circle: function(r) { - var e = new SVG.Circle().size(r); - this.add(e); - - return e; + circle: function(d) { + return this.ellipse(d); }, ellipse: function(w, h) { - var e = new SVG.Ellipse().size(w, h); - this.add(e); - - return e; + return this.put(new SVG.Ellipse().size(w, h)); + }, + + polyline: function(p) { + return this.put(new SVG.Polyline().plot(p)); + }, + + polygon: function(p) { + return this.put(new SVG.Polygon().plot(p)); }, path: function(d) { - var e = new SVG.Path().plot(d); - this.add(e); - - return e; + return this.put(new SVG.Path().plot(d)); }, image: function(s, w, h) { w = w != null ? w : 100; - var e = new SVG.Image().load(s).size(w, h != null ? h : w); - this.add(e); - - return e; + return this.put(new SVG.Image().load(s).size(w, h != null ? h : w)); }, text: function(t) { - var e = new SVG.Text().text(t); - this.add(e); - - return e; + return this.put(new SVG.Text().text(t)); }, gradient: function(t, b) { diff --git a/src/ellipse.js b/src/ellipse.js index d897969..006f89f 100644 --- a/src/ellipse.js +++ b/src/ellipse.js @@ -20,7 +20,7 @@ SVG.extend(SVG.Ellipse, { // custom size function size: function(w, h) { return this. - attr({ rx: w / 2, ry: h / 2 }). + attr({ rx: w / 2, ry: (h != null ? h : w) / 2 }). center(); }, diff --git a/src/path.js b/src/path.js index 27c28b3..fef5809 100644 --- a/src/path.js +++ b/src/path.js @@ -11,7 +11,7 @@ SVG.extend(SVG.Path, { // set path data plot: function(d) { - return this.attr('d', d || 'M0,0L0,0'); + return this.attr('d', d || 'M0,0'); }, // move path using translate diff --git a/src/poly.js b/src/poly.js new file mode 100644 index 0000000..e504b4c --- /dev/null +++ b/src/poly.js @@ -0,0 +1,38 @@ + +SVG.Poly = { + + // set polygon data with default zero point if no data is passed + plot: function(p) { + return this.attr('points', p || '0,0'); + }, + + // move path using translate + move: function(x, y) { + return this.transform({ x: x, y: y }); + } + +}; + + + +SVG.Polyline = function Polyline() { + this.constructor.call(this, SVG.create('polyline')); +}; + +// inherit from SVG.Shape +SVG.Polyline.prototype = new SVG.Shape(); + +// Add polygon-specific functions +SVG.extend(SVG.Polyline, SVG.Poly); + + + +SVG.Polygon = function Polygon() { + this.constructor.call(this, SVG.create('polygon')); +}; + +// inherit from SVG.Shape +SVG.Polygon.prototype = new SVG.Shape(); + +// Add polygon-specific functions +SVG.extend(SVG.Polygon, SVG.Poly); \ No newline at end of file diff --git a/src/text.js b/src/text.js index 7528c27..301373c 100644 --- a/src/text.js +++ b/src/text.js @@ -20,7 +20,8 @@ SVG.extend(SVG.Text, { var i, n, s = this._style(), p = this.parentDoc(), - a = t.split("\n"); + a = t.split("\n"), + f = this.style['font-size']; while (this.node.hasChildNodes()) this.node.removeChild(this.node.lastChild); @@ -29,7 +30,7 @@ SVG.extend(SVG.Text, { n = new TSpan(). text(a[i]). attr({ - dy: this.style['font-size'] * this.leading, + dy: f * this.leading - (i == 0 ? f * 0.3 : 0), x: (this.attr('x') || 0), style: s }); -- 2.39.5