]> source.dussan.org Git - svg.js.git/commitdiff
Added each(), next(), previous() and more
authorwout <wout@impinc.co.uk>
Mon, 31 Dec 2012 15:16:05 +0000 (16:16 +0100)
committerwout <wout@impinc.co.uk>
Mon, 31 Dec 2012 15:16:05 +0000 (16:16 +0100)
README.md
dist/svg.js
dist/svg.min.js
src/arrange.js
src/container.js
src/element.js
src/group.js

index 554303ec208e38cb308540c47a2e9b8747dc8832..fc9a54d0ef8870132426e6bf00ad4f801434a6d8 100644 (file)
--- 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 `<defs>` and `<g>` 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 `<defs>` 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._
 
index 95464c635591aa8492dda870ba99065666c8a4ea..288751fd78559a656753e5f11324c8f3b5b94895 100644 (file)
@@ -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 = {
       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));
       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.
       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' ?
         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
       };
     },
     
+    // 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 = '';
   
   // include the container object
   SVG.extend(SVG.G, SVG.Container);
+  
+  SVG.extend(SVG.G, {
+    
+    defs: function() {
+      return this.parentDoc().defs();
+    }
+    
+  });
 
   SVG.extend(SVG.Element, {
     
       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);
     
     // 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;
index 614e54eb53c4236faf5802c3d51dfa5ea76420b9..136519f9bb3668e1db92795371170f40bf75fe2c 100644 (file)
@@ -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<this.children().length){var t=this.children()[e];this.children().splice(e,1),this.node.removeChild(t.node),t.parent=null}return this},defs:function(){return this._defs||(this._defs=this.put(new SVG.Defs,0))},level:function(){return this.remove(d).put(this.defs(),0)},group:function(){return this.put(new SVG.G)},rect:function(e,t){return this.put((new SVG.Rect).size(e,t))},circle:function(e){return this.ellipse(e)},ellipse:function(e,t){return this.put((new SVG.Ellipse).size(e,t))},polyline:function(e){return this.put((new SVG.Polyline).plot(e))},polygon:function(e){return this.put((new SVG.Polygon).plot(e))},path:function(e){return this.put((new SVG.Path).plot(e))},image:function(e,t,n){return t=t!=null?t:100,this.put((new SVG.Image).load(e).size(t,n!=null?n:t))},text:function(e){return this.put((new SVG.Text).text(e))},gradient:function(e,t){return this.defs().gradient(e,t)},stage:function(){var e,t=this;return e=function(){document.readyState==="complete"?(t.attr("style","position:absolute;"),setTimeout(function(){t.attr("style","position:relative;")},5)):setTimeout(e,10)},e(),this}},SVG.Element=function(t){this.node=t,this.attrs={},this.trans={x:0,y:0,scaleX:1,scaleY:1,rotation:0,skewX:0,skewY:0}},SVG.extend(SVG.Element,{move:function(e,t){return this.attr({x:e,y:t})},size:function(e,t){return this.attr({width:e,height:t})},center:function(e,t){var n=this.bbox();return this.move(e-n.width/2,t-n.height/2)},clone:function(){var e,t=this.node.nodeName;return e=t=="rect"?this.parent[t](this.attrs.width,this.attrs.height):t=="ellipse"?this.parent[t](this.attrs.rx*2,this.attrs.ry*2):t=="image"?this.parent[t](this.src):t=="text"?this.parent[t](this.content):this.parent[t](),e.attr(this.attrs)},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},attr:function(e,t,n){if(arguments.length<2){if(typeof e!="object")return this._isStyle(e)?e=="text"?this.content:e=="leading"?this[e]:this.style[e]:this.attrs[e];for(t in e)this.attr(t,e[t])}else{this.attrs[e]=t;if(e=="x"&&this._isText())for(var r=this.lines.length-1;r>=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e){if(typeof e=="string")return this.trans[e];var t,n=[],r=this.bbox(),i=this.attr("transform")||"",s=i.match(/[a-z]+\([^\)]+\)/g)||[];for(t in e)e[t]!=null&&(this.trans[t]=e[t]);return e=this.trans,e.rotation!=0&&n.push("rotate("+e.rotation+","+(e.cx!=null?e.cx:r.cx)+","+(e.cy!=null?e.cy:r.cy)+")"),n.push("scale("+e.scaleX+","+e.scaleY+")"),e.skewX!=0&&n.push("skewX("+e.skewX+")"),e.skewY!=0&&n.push("skewY("+e.skewY+")"),n.push("translate("+e.x+","+e.y+")"),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return{x:e.x+this.trans.x,y:e.y+this.trans.y,cx:e.x+this.trans.x+e.width/2,cy:e.y+this.trans.y+e.height/2,width:e.width,height:e.height}},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<l;n++)r=(new t).text(o[n]).attr({dy:u*this.leading-(n==0?u*.3:0),x:this.attrs.x||0,style:i}),this.node.appendChild(r.node),this.lines.push(r);return this.attr("style",i)},_style:function(){var t,n="";for(t=e.length-1;t>=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<l;t++)n[t]instanceof SVG.Shape&&e.apply(n[t],[t,n]);return this},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e<this.children().length){var t=this.children()[e];this.children().splice(e,1),this.node.removeChild(t.node),t.parent=null}return this},defs:function(){return this._defs||(this._defs=this.put(new SVG.Defs,0))},level:function(){return this.remove(d).put(this.defs(),0)},group:function(){return this.put(new SVG.G)},rect:function(e,t){return this.put((new SVG.Rect).size(e,t))},circle:function(e){return this.ellipse(e)},ellipse:function(e,t){return this.put((new SVG.Ellipse).size(e,t))},polyline:function(e){return this.put((new SVG.Polyline).plot(e))},polygon:function(e){return this.put((new SVG.Polygon).plot(e))},path:function(e){return this.put((new SVG.Path).plot(e))},image:function(e,t,n){return t=t!=null?t:100,this.put((new SVG.Image).load(e).size(t,n!=null?n:t))},text:function(e){return this.put((new SVG.Text).text(e))},gradient:function(e,t){return this.defs().gradient(e,t)},first:function(){return this.children()[1]},last:function(){return this.children()[this.children().length-1]},clear:function(){this._children=[];while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return this},stage:function(){var e,t=this;return e=function(){document.readyState==="complete"?(t.attr("style","position:absolute;"),setTimeout(function(){t.attr("style","position:relative;")},5)):setTimeout(e,10)},e(),this}},SVG.Element=function(t){this.node=t,this.attrs={},this.trans={x:0,y:0,scaleX:1,scaleY:1,rotation:0,skewX:0,skewY:0}},SVG.extend(SVG.Element,{move:function(e,t){return this.attr({x:e,y:t})},size:function(e,t){return this.attr({width:e,height:t})},center:function(e,t){var n=this.bbox();return this.move(e-n.width/2,t-n.height/2)},clone:function(){var e,t=this.node.nodeName;return e=t=="rect"?this.parent[t](this.attrs.width,this.attrs.height):t=="ellipse"?this.parent[t](this.attrs.rx*2,this.attrs.ry*2):t=="image"?this.parent[t](this.src):t=="text"?this.parent[t](this.content):this.parent[t](),e.trans=this.trans,e.attr(this.attrs).transform({})},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},attr:function(e,t,n){if(arguments.length<2){if(typeof e!="object")return this._isStyle(e)?e=="text"?this.content:e=="leading"?this[e]:this.style[e]:this.attrs[e];for(t in e)this.attr(t,e[t])}else{this.attrs[e]=t;if(e=="x"&&this._isText())for(var r=this.lines.length-1;r>=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e){if(typeof e=="string")return this.trans[e];var t,n=[],r=this.bbox(),i=this.attr("transform")||"",s=i.match(/[a-z]+\([^\)]+\)/g)||[];for(t in e)e[t]!=null&&(this.trans[t]=e[t]);return e=this.trans,e.rotation!=0&&n.push("rotate("+e.rotation+","+(e.cx!=null?e.cx:r.cx)+","+(e.cy!=null?e.cy:r.cy)+")"),n.push("scale("+e.scaleX+","+e.scaleY+")"),e.skewX!=0&&n.push("skewX("+e.skewX+")"),e.skewY!=0&&n.push("skewY("+e.skewY+")"),n.push("translate("+e.x+","+e.y+")"),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return{x:e.x+this.trans.x,y:e.y+this.trans.y,cx:e.x+this.trans.x+e.width/2,cy:e.y+this.trans.y+e.height/2,width:e.width,height:e.height}},inside:function(e,t){var n=this.bbox();return e>n.x&&t>n.y&&e<n.x+n.width&&t<n.y+n.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.G,{defs:function(){return this.parentDoc().defs()}}),SVG.extend(SVG.Element,{siblings:function(){return this.parent.children()},position:function(){return this.siblings().indexOf(this)},next:function(){return this.siblings()[this.position()+1]},previous:function(){return this.siblings()[this.position()-1]},forward:function(){return this.parent.remove(this).put(this,this.position()+1)},backward:function(){var e,t=this.parent.level();return e=this.position(),e>1&&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<l;n++)r=(new t).text(o[n]).attr({dy:u*this.leading-(n==0?u*.3:0),x:this.attrs.x||0,style:i}),this.node.appendChild(r.node),this.lines.push(r);return this.attr("style",i)},_style:function(){var t,n="";for(t=e.length-1;t>=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
index ff255be33ae1077d114fdda83c55668ae2674ff8..ec06591a6921b775fd35de855b5bfa2170dbc6e5 100644 (file)
@@ -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
index 64824504b8cd4ed8817aa485fbd06dd0e84efc16..3656892a8bbf625f579e56d29e3ee3d84f85f4f6 100644 (file)
@@ -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.
index a9cd7cd1a7bfca992feb7aa8fa223f6b377d01a3..5db42b3bde635426c32bf3f654cdfadad4b2ff63 100644 (file)
@@ -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 = '';
index e3cb6a8df7854e0c85f73117a80f10f28d514a27..1007a329ba3f0aa2052e6f6928bd3355733614be 100644 (file)
@@ -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