From: wout Date: Sat, 22 Dec 2012 12:33:50 +0000 (+0100) Subject: Change lement creation syntax X-Git-Tag: 0.2~83 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a96c2890913e7c6714d2558d15490226e82119bf;p=svg.js.git Change lement creation syntax --- diff --git a/README.md b/README.md index 717f377..fbc6278 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Use the 'svg()' function to create a SVG document within a given html element: ```javascript var draw = svg('paper').size(300, 300); -var rect = draw.rect({ width:100, height:100 }).attr({ fill: '#f06' }); +var rect = draw.rect(100, 100).attr({ fill: '#f06' }); ``` The first argument can either be an id of the element or the selected element itself. This will generate the following output: @@ -96,11 +96,7 @@ rect.remove(); ### Text element Text elements can be created with an object as the first argument defining the 'text' content, 'x' and 'y': ```javascript -var text = draw.text({ - text: "svg\nto\nthe\npoint.", - x: 300, - y: 0 -}); +var text = draw.text("svg\nto\nthe\npoint.").move(300, 0); ``` Styling text can be done using the 'attr()': ```javascript @@ -133,19 +129,13 @@ text.font({ ### Image element When creating images the width and height values should be defined: ```javascript -var image = draw.image({ - width: 200, - height: 200, - x: 100, - y: 100, - src: '/path/to/image.jpg' -}); +var image = draw.image('/path/to/image.jpg').move(100, 100).size(200, 200); ``` ### Path element ```javascript -var path = draw.path({ data: "M10,20L30,40" }).attr({ fill: '#9dffd3' }); +var path = draw.path('M10,20L30,40').attr({ fill: '#9dffd3' }); ``` For more details on path data strings, please read the SVG documentation: @@ -203,14 +193,14 @@ Using 'clip()' creates a clip path in the parents 'defs' node, and passes it to ```javascript rect.clip(function(clipPath) { - clipPath.rect({ x:10, y:10, width:80, height:80 }); + clipPath.rect(80, 80).move(10, 10); }); ``` You can also reuse clip paths for multiple elements using 'clipTo()'. ```javascript var clipPath = doc.defs().clipPath(); -clipRect = clipPath.rect({ x:10, y:10, width:80, height:80 }); +clipRect = clipPath.rect(80, 80).move(10, 10); rect.clipTo(clipPath); ``` @@ -241,7 +231,7 @@ _This functionality requires the arrange.js module which is included in the defa Grouping elements is useful if you want to transform a set of elements as if it were one. All element within a group maintain their position relative to the group they belong to. A group has all the same element methods as the root svg document: ```javascript var group = draw.group(); -group.path({ data: "M10,20L30,40" }); +group.path('M10,20L30,40'); ``` Existing elements from the svg document can also be added to a group: ```javascript diff --git a/dist/svg.js b/dist/svg.js index c906761..57f8038 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js v0.1-4-g740392e - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ +/* svg.js v0.1-7-g0c4be8f - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -75,76 +75,50 @@ return e; }, - rect: function(v) { - return this.place(new SVG.Rect(), v); + rect: function(w, h) { + var e = new SVG.Rect().size(w, h); + this.add(e); + + return e; }, - circle: function(v) { - var g; - - if (v != null) { - g = { x: v.x, y: v.y }; - - if (v.r || v.radius) - g.width = g.height = (v.r || v.radius) * 2; - else - g.width = g.height = v.width || v.height; - } + circle: function(r) { + var e = new SVG.Circle().size(r); + this.add(e); - return this.place(new SVG.Circle(), g); + return e; }, - ellipse: function(v) { - var g; - - if (v != null) { - g = { x: v.x, y: v.y }; - - if (v.width) g.width = v.width; - if (v.height) g.height = v.height; - if (v.rx) g.width = v.rx * 2; - if (v.ry) g.height = v.ry * 2; - } + ellipse: function(w, h) { + var e = new SVG.Ellipse().size(w, h); + this.add(e); - return this.place(new SVG.Ellipse(), g); + return e; }, - path: function(v) { - return this.place(new SVG.Path(), v); + path: function(d) { + var e = new SVG.Path().plot(d); + this.add(e); + + return e; }, - image: function(v) { - return this.place(new SVG.Image(), v); + image: function(s) { + var e = new SVG.Image().load(s); + this.add(e); + + return e; }, - text: function(v) { - return this.place(new SVG.Text(), v); + text: function(t) { + var e = new SVG.Text().text(t); + this.add(e); + + return e; }, gradient: function(t, b) { return this.defs().gradient(t, b); - }, - - place: function(e, v) { - if (v != null) { - e.move(v.x || 0, v.y || 0); - - - if (v.width != null && v.height != null) - e.size(v.width, v.height); - - v.data != null ? - e.plot(v.data) : - v.src != null ? - e.load(v.src) : - v.text != null ? - e.text(v.text) : - void 0; - } - - this.add(e); - - return e; } }; diff --git a/dist/svg.min.js b/dist/svg.min.js index 612e746..77890e9 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js v0.1-4-g740392e - 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]),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,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;o1&&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().levelDefs();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.mother().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")),typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1"}).attr("xlink",SVG.xlink,SVG.ns).size(t.offsetWidth,t.offsetHeight).defs(),t.appendChild(this.node)},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)}}),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){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+")")}}),SVG.extend(SVG.Text,{font:function(e){var t={};for(var n in e)n=="leading"?t[n]=e[n]:n=="anchor"?t["text-anchor"]=e[n]:this._s.indexOf(n)>-1?t["font-"+n]=e[n]:void 0;return this.attr(t).text(this.content)}})}).call(this); \ No newline at end of file +/* svg.js v0.1-7-g0c4be8f - 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]),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,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;o1&&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().levelDefs();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.mother().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")),typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1"}).attr("xlink",SVG.xlink,SVG.ns).size(t.offsetWidth,t.offsetHeight).defs(),t.appendChild(this.node)},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)}}),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){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+")")}}),SVG.extend(SVG.Text,{font:function(e){var t={};for(var n in e)n=="leading"?t[n]=e[n]:n=="anchor"?t["text-anchor"]=e[n]:this._s.indexOf(n)>-1?t["font-"+n]=e[n]:void 0;return this.attr(t).text(this.content)}})}).call(this); \ No newline at end of file diff --git a/src/container.js b/src/container.js index 35d80a4..f45bd6c 100644 --- a/src/container.js +++ b/src/container.js @@ -59,76 +59,50 @@ SVG.Container = { return e; }, - rect: function(v) { - return this.place(new SVG.Rect(), v); + rect: function(w, h) { + var e = new SVG.Rect().size(w, h); + this.add(e); + + return e; }, - circle: function(v) { - var g; - - if (v != null) { - g = { x: v.x, y: v.y }; - - if (v.r || v.radius) - g.width = g.height = (v.r || v.radius) * 2; - else - g.width = g.height = v.width || v.height; - } + circle: function(r) { + var e = new SVG.Circle().size(r); + this.add(e); - return this.place(new SVG.Circle(), g); + return e; }, - ellipse: function(v) { - var g; - - if (v != null) { - g = { x: v.x, y: v.y }; - - if (v.width) g.width = v.width; - if (v.height) g.height = v.height; - if (v.rx) g.width = v.rx * 2; - if (v.ry) g.height = v.ry * 2; - } + ellipse: function(w, h) { + var e = new SVG.Ellipse().size(w, h); + this.add(e); - return this.place(new SVG.Ellipse(), g); + return e; }, - path: function(v) { - return this.place(new SVG.Path(), v); + path: function(d) { + var e = new SVG.Path().plot(d); + this.add(e); + + return e; }, - image: function(v) { - return this.place(new SVG.Image(), v); + image: function(s) { + var e = new SVG.Image().load(s); + this.add(e); + + return e; }, - text: function(v) { - return this.place(new SVG.Text(), v); + text: function(t) { + var e = new SVG.Text().text(t); + this.add(e); + + return e; }, gradient: function(t, b) { return this.defs().gradient(t, b); - }, - - place: function(e, v) { - if (v != null) { - e.move(v.x || 0, v.y || 0); - - - if (v.width != null && v.height != null) - e.size(v.width, v.height); - - v.data != null ? - e.plot(v.data) : - v.src != null ? - e.load(v.src) : - v.text != null ? - e.text(v.text) : - void 0; - } - - this.add(e); - - return e; } }; \ No newline at end of file