From: wout Date: Mon, 31 Dec 2012 15:16:05 +0000 (+0100) Subject: Added each(), next(), previous() and more X-Git-Tag: 0.2~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5e7c26e9423f3c543e04bc9a11656125ec7bf8ca;p=svg.js.git Added each(), next(), previous() and more --- diff --git a/README.md b/README.md index 554303e..fc9a54d 100644 --- a/README.md +++ b/README.md @@ -230,12 +230,19 @@ rect.show(); ### Removing elements Pretty straightforward: + ```javascript rect.remove(); ``` +To remove all elements in the svg document: + +```javascript +draw.clear(); +``` + -## Bounding box +### Bounding box ```javascript path.bbox(); @@ -249,6 +256,17 @@ This will return an object with the following values: As opposed to the native `getBBox()` method any translations used with the `transform()` method will be taken into account. +### Iterating over all children + +If you would iterate over all the `children()` of the svg document, you might notice also the `` and `` elements will be included. To iterate the shapes only, you can use the `each()` method: + +```javascript +draw.each(function(i, children) { + this.fill({ color: '#f06' }); +}); +``` + + ## Syntax sugar Fill and stroke are used quite often. Therefore two convenience methods are provided: @@ -340,29 +358,58 @@ _This functionality requires the mask.js module which is included in the default ## Arranging elements -You can arrange elements within their parent SVG document using the following methods: +You can arrange elements within their parent SVG document using the following methods. + +Move element to the front: ```javascript -// move element to the front rect.front(); +``` + +Move element to the back: -// move element to the back +```javascript rect.back(); +``` -// move element one step forward +Note that `back()` will move the element to position 1, not 0, because the `` node is already located at position 0. + +Move element one step forward: + +```javascript rect.forward(); +``` + +Move element one step backward: -// move element one step backward +```javascript rect.backward(); ``` -The arrange.js module brings some additional methods: +The arrange.js module brings some additional methods. To get all siblings of rect, including rect itself: + +```javascript +rect.siblings(); +``` + +Get the position (a number) of rect between its siblings: ```javascript -// returns the position (a number) of the element between its siblings rect.position(); ``` +Get the next sibling: + +```javascript +rect.next(); +``` + +Get the previous sibling: + +```javascript +rect.previous(); +``` + _This functionality requires the arrange.js module which is included in the default distribution._ diff --git a/dist/svg.js b/dist/svg.js index 95464c6..288751f 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js v0.1-50-g9f622f7 - svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ +/* svg.js v0.1-52-g8dbe359 - svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -52,6 +52,19 @@ return this._children || (this._children = []); }, + // iterates over all children + each: function(b) { + var i, + c = this.children(); + + // iteralte all shapes + for (i = 1, l = c.length; i < l; i++) + if (c[i] instanceof SVG.Shape) + b.apply(c[i], [i, c]); + + return this; + }, + // remove a given child element remove: function(e) { return this.removeAt(this.children().indexOf(e)); @@ -130,6 +143,26 @@ return this.defs().gradient(t, b); }, + // get first child, skipping the defs node + first: function() { + return this.children()[1]; + }, + + // let the last child + last: function() { + return this.children()[this.children().length - 1]; + }, + + // clears all elements of this container + clear: function() { + this._children = []; + + while (this.node.hasChildNodes()) + this.node.removeChild(this.node.lastChild); + + return this; + }, + // hack for safari preventing text to be rendered in one line, // basically it sets the position of the svg node to absolute // when the dom is loaded, and resets it to relative a few ms later. @@ -196,6 +229,7 @@ var c, n = this.node.nodeName; + // invoke shape method with shape-specific arguments c = n == 'rect' ? this.parent[n](this.attrs.width, this.attrs.height) : n == 'ellipse' ? @@ -206,7 +240,11 @@ this.parent[n](this.content) : this.parent[n](); - return c.attr(this.attrs); + // copy translations + c.trans = this.trans; + + // apply attributes and translations + return c.attr(this.attrs).transform({}); }, // remove element @@ -330,6 +368,16 @@ }; }, + // is a given point inside the bounding box of the element + inside: function(x, y) { + var b = this.bbox(); + + return x > b.x && + y > b.y && + x < b.x + b.width && + y < b.y + b.height; + }, + // show element show: function() { this.node.style.display = ''; @@ -404,6 +452,14 @@ // include the container object SVG.extend(SVG.G, SVG.Container); + + SVG.extend(SVG.G, { + + defs: function() { + return this.parentDoc().defs(); + } + + }); SVG.extend(SVG.Element, { @@ -412,18 +468,31 @@ return this.parent.children(); }, + // get the curent position siblings + position: function() { + return this.siblings().indexOf(this); + }, + + // get the next element + next: function() { + return this.siblings()[this.position() + 1]; + }, + + // get the next element + previous: function() { + return this.siblings()[this.position() - 1]; + }, + // send given element one step forwards forward: function() { - var i = this.siblings().indexOf(this); - - return this.parent.remove(this).put(this, i + 1); + return this.parent.remove(this).put(this, this.position() + 1); }, // send given element one step backwards backward: function() { var i, p = this.parent.level(); - i = this.siblings().indexOf(this); + i = this.position(); if (i > 1) p.remove(this).add(this, i - 1); @@ -438,11 +507,9 @@ // send given element all the way to the back back: function() { - var i, p = this.parent.level(); + var p = this.parent.level(); - i = this.siblings().indexOf(this); - - if (i > 1) + if (this.position() > 1) p.remove(this).add(this, 0); return this; diff --git a/dist/svg.min.js b/dist/svg.min.js index 614e54e..136519f 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js v0.1-50-g9f622f7 - svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ -function svg(e){return new SVG.Doc(e)}(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",did:0,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}},show:function(){return this.node.style.display="",this},hide:function(){return this.node.style.display="none",this},_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}}),["click","dblclick","mousedown","mouseup","mouseover","mouseout","mousemove","touchstart","touchend","touchmove","touchcancel"].forEach(function(e){SVG.Element.prototype[e]=function(t){var n=this;return this.node["on"+e]=function(){return t.apply(n,arguments)},this}}),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),SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_"+SVG.did++,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)}}),SVG.Mask=function(){this.constructor.call(this,SVG.create("mask")),this.id="svgjs_"+SVG.did++,this.attr("id",this.id)},SVG.Mask.prototype=new SVG.Element,SVG.extend(SVG.Mask,SVG.Container),SVG.extend(SVG.Element,{mask:function(e){var t=this.parent.defs().mask();return e(t),this.maskTo(t)},maskTo:function(e){return this.attr("mask","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{mask:function(){return this.put(new SVG.Mask)}}),SVG.Gradient=function(t){this.constructor.call(this,SVG.create(t+"Gradient")),this.id="svgjs_"+SVG.did++,this.attr("id",this.id),this.type=t},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.src=e,e?this.attr("xlink:href",e,SVG.xlink):this}});var e=["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 n,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(n=0,l=o.length;n=0;t--)this.style["font-"+e[t]]!=null&&(n+="font-"+e[t]+":"+this.style["font-"+e[t]]+";");return n+="text-anchor:"+this.style["text-anchor"]+";",n}}),t.prototype=new SVG.Shape,SVG.extend(t,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}});var n=["width","opacity","linecap","linejoin","miterlimit","dasharray","dashoffset"],r=["opacity","rule"];SVG.extend(SVG.Shape,{fill:function(e){var t;e.color!=null&&this.attr("fill",e.color);for(t=r.length-1;t>=0;t--)e[r[t]]!=null&&this.attr("fill-"+r[t],e[r[t]]);return this},stroke:function(e){var t;e.color&&this.attr("stroke",e.color);for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&this.attr("stroke-"+n[t],e[n[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(t){var n,r={};for(n in t)n=="leading"?r[n]=t[n]:n=="anchor"?r["text-anchor"]=t[n]:e.indexOf(n)>-1?r["font-"+n]=t[n]:void 0;return this.attr(r).text(this.content)}})}).call(this); \ No newline at end of file +/* svg.js v0.1-52-g8dbe359 - svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */ +function svg(e){return new SVG.Doc(e)}(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",did:0,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=[])},each:function(e){var t,n=this.children();for(t=1,l=n.length;t=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}},inside:function(e,t){var n=this.bbox();return e>n.x&&t>n.y&&e1&&t.remove(this).add(this,e-1),this},front:function(){return this.parent.remove(this).put(this)},back:function(){var e=this.parent.level();return this.position()>1&&e.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),SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_"+SVG.did++,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)}}),SVG.Mask=function(){this.constructor.call(this,SVG.create("mask")),this.id="svgjs_"+SVG.did++,this.attr("id",this.id)},SVG.Mask.prototype=new SVG.Element,SVG.extend(SVG.Mask,SVG.Container),SVG.extend(SVG.Element,{mask:function(e){var t=this.parent.defs().mask();return e(t),this.maskTo(t)},maskTo:function(e){return this.attr("mask","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{mask:function(){return this.put(new SVG.Mask)}}),SVG.Gradient=function(t){this.constructor.call(this,SVG.create(t+"Gradient")),this.id="svgjs_"+SVG.did++,this.attr("id",this.id),this.type=t},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.src=e,e?this.attr("xlink:href",e,SVG.xlink):this}});var e=["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 n,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(n=0,l=o.length;n=0;t--)this.style["font-"+e[t]]!=null&&(n+="font-"+e[t]+":"+this.style["font-"+e[t]]+";");return n+="text-anchor:"+this.style["text-anchor"]+";",n}}),t.prototype=new SVG.Shape,SVG.extend(t,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}});var n=["width","opacity","linecap","linejoin","miterlimit","dasharray","dashoffset"],r=["opacity","rule"];SVG.extend(SVG.Shape,{fill:function(e){var t;e.color!=null&&this.attr("fill",e.color);for(t=r.length-1;t>=0;t--)e[r[t]]!=null&&this.attr("fill-"+r[t],e[r[t]]);return this},stroke:function(e){var t;e.color&&this.attr("stroke",e.color);for(t=n.length-1;t>=0;t--)e[n[t]]!=null&&this.attr("stroke-"+n[t],e[n[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(t){var n,r={};for(n in t)n=="leading"?r[n]=t[n]:n=="anchor"?r["text-anchor"]=t[n]:e.indexOf(n)>-1?r["font-"+n]=t[n]: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 ff255be..ec06591 100644 --- a/src/arrange.js +++ b/src/arrange.js @@ -12,11 +12,19 @@ SVG.extend(SVG.Element, { return this.siblings().indexOf(this); }, + // get the next element + next: function() { + return this.siblings()[this.position() + 1]; + }, + + // get the next element + previous: function() { + return this.siblings()[this.position() - 1]; + }, + // send given element one step forwards forward: function() { - var i = this.siblings().indexOf(this); - - return this.parent.remove(this).put(this, i + 1); + return this.parent.remove(this).put(this, this.position() + 1); }, // send given element one step backwards diff --git a/src/container.js b/src/container.js index 6482450..3656892 100644 --- a/src/container.js +++ b/src/container.js @@ -30,6 +30,19 @@ SVG.Container = { return this._children || (this._children = []); }, + // iterates over all children + each: function(b) { + var i, + c = this.children(); + + // iteralte all shapes + for (i = 1, l = c.length; i < l; i++) + if (c[i] instanceof SVG.Shape) + b.apply(c[i], [i, c]); + + return this; + }, + // remove a given child element remove: function(e) { return this.removeAt(this.children().indexOf(e)); @@ -108,6 +121,26 @@ SVG.Container = { return this.defs().gradient(t, b); }, + // get first child, skipping the defs node + first: function() { + return this.children()[1]; + }, + + // let the last child + last: function() { + return this.children()[this.children().length - 1]; + }, + + // clears all elements of this container + clear: function() { + this._children = []; + + while (this.node.hasChildNodes()) + this.node.removeChild(this.node.lastChild); + + return this; + }, + // hack for safari preventing text to be rendered in one line, // basically it sets the position of the svg node to absolute // when the dom is loaded, and resets it to relative a few ms later. diff --git a/src/element.js b/src/element.js index a9cd7cd..5db42b3 100644 --- a/src/element.js +++ b/src/element.js @@ -182,6 +182,16 @@ SVG.extend(SVG.Element, { }; }, + // is a given point inside the bounding box of the element + inside: function(x, y) { + var b = this.bbox(); + + return x > b.x && + y > b.y && + x < b.x + b.width && + y < b.y + b.height; + }, + // show element show: function() { this.node.style.display = ''; diff --git a/src/group.js b/src/group.js index e3cb6a8..1007a32 100644 --- a/src/group.js +++ b/src/group.js @@ -7,4 +7,12 @@ SVG.G = function G() { SVG.G.prototype = new SVG.Element(); // include the container object -SVG.extend(SVG.G, SVG.Container); \ No newline at end of file +SVG.extend(SVG.G, SVG.Container); + +SVG.extend(SVG.G, { + + defs: function() { + return this.parentDoc().defs(); + } + +}); \ No newline at end of file