diff options
author | wout <wout@impinc.co.uk> | 2013-01-01 21:39:25 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2013-01-01 21:39:25 +0100 |
commit | 88987d60c456f1e686edd226f4ccb45e35142cd7 (patch) | |
tree | 719bd1c6ea1e18bae81ea9a8083742184eb422e9 | |
parent | 5e7c26e9423f3c543e04bc9a11656125ec7bf8ca (diff) | |
download | svg.js-88987d60c456f1e686edd226f4ccb45e35142cd7.tar.gz svg.js-88987d60c456f1e686edd226f4ccb45e35142cd7.zip |
Replaced clip() in favor of mask()
Some browsers had issues with clipping, masking was a better option.
-rw-r--r-- | README.md | 45 | ||||
-rw-r--r-- | Rakefile | 2 | ||||
-rw-r--r-- | dist/svg.js | 221 | ||||
-rw-r--r-- | dist/svg.min.js | 4 | ||||
-rw-r--r-- | src/clip.js | 42 | ||||
-rw-r--r-- | src/container.js | 15 | ||||
-rw-r--r-- | src/element.js | 44 | ||||
-rw-r--r-- | src/mask.js | 22 | ||||
-rw-r--r-- | src/path.js | 10 | ||||
-rw-r--r-- | src/text.js | 2 | ||||
-rw-r--r-- | src/wrap.js | 84 |
11 files changed, 283 insertions, 208 deletions
@@ -309,49 +309,38 @@ rect.skew(0, 45); _This functionality requires the sugar.js module which is included in the default distribution._ -## Clipping elements -Clipping elements can be done with either `clip()` or `clipTo()`. -Using `clip()` creates a clip path in the parents 'defs' node, and passes it to a block: +## Masking elements +The easiest way to mask is to use a single element: ```javascript -rect.clip(function(clipPath) { - clipPath.ellipse(80, 40).move(10, 10); -}); -``` +var ellipse = draw.ellipse(80, 40).move(10, 10).fill({ color: '#fff' }); -You can also reuse clip paths for multiple elements using `clipTo()`. -```javascript -var clipPath = draw.defs().clip(); -var ellipse = clipPath.ellipse(80, 40).move(10, 10); -rect.clipTo(clipPath); +rect.maskWith(ellipse); ``` -_This functionality requires the clip.js module which is included in the default distribution._ +But you can also use multiple elements: +```javascript +var ellipse = draw.ellipse(80, 40).move(10, 10).fill({ color: '#fff' }); +var text = draw.text('SVG.JS').move(10, 10).font({ size: 36 }).fill({ color: '#fff' }); -## Masking elements -Masking elements works very much the same as clipping with either `mask()` or `maskTo()`. +var mask = draw.mask().add(text).add(ellipse); -Using `mask()` creates a mask in the parents 'defs' node, and passes it to a block: +rect.maskWith(mask); +``` + +If you want the masked object to be rendered at 100% you need to set the fill color of the masking object to white. But you might also want to use a gradient: ```javascript -var gradient = draw.gradient('linear', function(stop) { +var gradient = image.parent.gradient('linear', function(stop) { stop.at({ offset: 0, color: '#000' }); - stop.at({ offset: 90, color: '#fff' }); -}); - -rect.mask(function(mask) { - mask.ellipse(80, 40).move(10, 10).fill({ color: gradient.fill() }); + stop.at({ offset: 100, color: '#fff' }); }); -``` -You can also reuse masks for multiple elements using `maskTo()`. +var ellipse = draw.ellipse(80, 40).move(10, 10).fill({ color: gradient.fill() }); -```javascript -var mask = draw.defs().mask(); -var ellipse = mask.ellipse(80, 40).move(10, 10).fill({ color: gradient.fill() }); -rect.maskTo(mask); +rect.maskWith(ellipse); ``` _This functionality requires the mask.js module which is included in the default distribution._ @@ -1,7 +1,7 @@ SVGJS_VERSION = '0.1a' # all available modules in the correct loading order -MODULES = %w[ svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar ] +MODULES = %w[ svg container element event group arrange defs mask gradient doc shape wrap 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 288751f..f8b3366 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* 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 */ +/* svg.js v0.1-53-g5e7c26e - svg container element event group arrange defs mask gradient doc shape wrap rect ellipse poly path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -58,7 +58,7 @@ c = this.children(); // iteralte all shapes - for (i = 1, l = c.length; i < l; i++) + for (i = 0, l = c.length; i < l; i++) if (c[i] instanceof SVG.Shape) b.apply(c[i], [i, c]); @@ -114,17 +114,17 @@ // create a polyline element polyline: function(p) { - return this.put(new SVG.Polyline().plot(p)); + return this.put(new Wrap(new SVG.Polyline())).plot(p); }, // create a polygon element polygon: function(p) { - return this.put(new SVG.Polygon().plot(p)); + return this.put(new Wrap(new SVG.Polygon())).plot(p); }, - // create a path element + // create a wrapped path element path: function(d) { - return this.put(new SVG.Path().plot(d)); + return this.put(new Wrap(new SVG.Path())).plot(d); }, // create image element, load image and set its size @@ -143,6 +143,11 @@ return this.defs().gradient(t, b); }, + // create masking element + mask: function() { + return this.defs().put(new SVG.Mask()); + }, + // get first child, skipping the defs node first: function() { return this.children()[1]; @@ -226,21 +231,35 @@ // clone element clone: function() { - 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.attrs.rx * 2, this.attrs.ry * 2) : - n == 'image' ? - this.parent[n](this.src) : - n == 'text' ? - this.parent[n](this.content) : - this.parent[n](); - - // copy translations + var c; + + // if this is a wrapped shape + if (this instanceof Wrap) { + // build new wrapped shape + c = this.parent[this.child.node.nodeName](); + + // copy child attributes and transformations + c.child.trans = this.child.trans; + c.child.attr(this.child.attrs).transform({}); + + } else { + var 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.attrs.rx * 2, this.attrs.ry * 2) : + n == 'image' ? + this.parent[n](this.src) : + n == 'text' ? + this.parent[n](this.content) : + n == 'g' ? + this.parent.group() : + this.parent[n](); + } + + // copy transformations c.trans = this.trans; // apply attributes and translations @@ -527,48 +546,6 @@ // include the container object SVG.extend(SVG.Defs, SVG.Container); - SVG.Clip = function Clip() { - this.constructor.call(this, SVG.create('clipPath')); - - // set unique id - this.id = 'svgjs_' + (SVG.did++); - this.attr('id', this.id); - }; - - // inherit from SVG.Element - SVG.Clip.prototype = new SVG.Element(); - - // include the container object - SVG.extend(SVG.Clip, SVG.Container); - - // add clipping functionality to element - SVG.extend(SVG.Element, { - - // clip element using another element - clip: function(b) { - var p = this.parent.defs().clip(); - b(p); - - return this.clipTo(p); - }, - - // distribute clipping path to svg element - clipTo: function(p) { - return this.attr('clip-path', 'url(#' + p.id + ')'); - } - - }); - - // add def-specific functions - SVG.extend(SVG.Defs, { - - // create clippath - clip: function() { - return this.put(new SVG.Clip()); - } - - }); - SVG.Mask = function Mask() { this.constructor.call(this, SVG.create('mask')); @@ -586,27 +563,9 @@ // add clipping functionality to element SVG.extend(SVG.Element, { - // mask element using another element - mask: function(b) { - var m = this.parent.defs().mask(); - b(m); - - return this.maskTo(m); - }, - // distribute mask to svg element - maskTo: function(m) { - return this.attr('mask', 'url(#' + m.id + ')'); - } - - }); - - // add def-specific functions - SVG.extend(SVG.Defs, { - - // create clippath - mask: function() { - return this.put(new SVG.Mask()); + maskWith: function(e) { + return this.attr('mask', 'url(#' + (e instanceof SVG.Mask ? e : this.parent.mask().add(e)).id + ')'); } }); @@ -768,6 +727,90 @@ // inherit from SVG.Element SVG.Shape.prototype = new SVG.Element(); + function Wrap(e) { + this.constructor.call(this, SVG.create('g')); + + // insert and store child + this.node.insertBefore(e.node, null); + this.child = e; + }; + + // inherit from SVG.Shape + Wrap.prototype = new SVG.Shape(); + + // include the container object + SVG.extend(Wrap, { + + // move wrapper around + move: function(x, y) { + return this.center( + x + (this._b.width * this.child.trans.scaleX) / 2, + y + (this._b.height * this.child.trans.scaleY) / 2 + ); + }, + + // set the actual size in pixels + size: function(w, h) { + var s = w / this._b.width; + + this.child.transform({ + scaleX: s, + scaleY: h != null ? h / this._b.height : s + }); + + return this; + }, + + // move by center + center: function(x, y) { + return this.transform({ x: x, y: y }); + }, + + // create distributed attr + attr: function(a, v, n) { + // call individual attributes if an object is given + if (typeof a == 'object') { + for (v in a) this.attr(v, a[v]); + + // act as a getter if only one argument is given + } else if (arguments.length < 2) { + return a == 'transform' ? this.attrs[a] : this.child.attrs[a]; + + // apply locally for certain attributes + } else if (a == 'transform') { + this.attrs[a] = v; + + n != null ? + this.node.setAttributeNS(n, a, v) : + this.node.setAttribute(a, v); + + // apply attributes to child + } else { + this.child.attr(a, v, n); + } + + return this; + }, + + // distribute plot method to child + plot: function(d) { + // plot new shape + this.child.plot(d); + + // get new bbox and store size + this._b = this.child.bbox(); + + // reposition element withing wrapper + this.child.transform({ + x: -this._b.cx, + y: -this._b.cy + }); + + return this; + } + + }); + SVG.Rect = function Rect() { this.constructor.call(this, SVG.create('rect')); }; @@ -859,14 +902,14 @@ // Add path-specific functions SVG.extend(SVG.Path, { + // move using transform + move: function(x, y) { + this.transform({ x: x, y: y }); + }, + // set path data plot: function(d) { return this.attr('d', d || 'M0,0'); - }, - - // move path using translate, path's don't take x and y - move: function(x, y) { - return this.transform({ x: x, y: y }); } }); @@ -961,7 +1004,7 @@ this.constructor.call(this, SVG.create('tspan')); }; - // inherit from SVG.Element + // inherit from SVG.Shape TSpan.prototype = new SVG.Shape(); // include the container object diff --git a/dist/svg.min.js b/dist/svg.min.js index 136519f..2944a86 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* 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 +/* svg.js v0.1-53-g5e7c26e - svg container element event group arrange defs mask gradient doc shape wrap rect ellipse poly path image text sugar - svgjs.com/license */ +function svg(e){return new SVG.Doc(e)}(function(){function e(e){this.constructor.call(this,SVG.create("g")),this.node.insertBefore(e.node,null),this.child=e}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",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=0,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(t){return this.put(new e(new SVG.Polyline)).plot(t)},polygon:function(t){return this.put(new e(new SVG.Polygon)).plot(t)},path:function(t){return this.put(new e(new SVG.Path)).plot(t)},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)},mask:function(){return this.defs().put(new SVG.Mask)},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 t;if(this instanceof e)t=this.parent[this.child.node.nodeName](),t.child.trans=this.child.trans,t.child.attr(this.child.attrs).transform({});else{var n=this.node.nodeName;t=n=="rect"?this.parent[n](this.attrs.width,this.attrs.height):n=="ellipse"?this.parent[n](this.attrs.rx*2,this.attrs.ry*2):n=="image"?this.parent[n](this.src):n=="text"?this.parent[n](this.content):n=="g"?this.parent.group():this.parent[n]()}return t.trans=this.trans,t.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.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,{maskWith:function(e){return this.attr("mask","url(#"+(e instanceof SVG.Mask?e:this.parent.mask().add(e)).id+")")}}),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,e.prototype=new SVG.Shape,SVG.extend(e,{move:function(e,t){return this.center(e+this._b.width*this.child.trans.scaleX/2,t+this._b.height*this.child.trans.scaleY/2)},size:function(e,t){var n=e/this._b.width;return this.child.transform({scaleX:n,scaleY:t!=null?t/this._b.height:n}),this},center:function(e,t){return this.transform({x:e,y:t})},attr:function(e,t,n){if(typeof e=="object")for(t in e)this.attr(t,e[t]);else{if(arguments.length<2)return e=="transform"?this.attrs[e]:this.child.attrs[e];e=="transform"?(this.attrs[e]=t,n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t)):this.child.attr(e,t,n)}return this},plot:function(e){return this.child.plot(e),this._b=this.child.bbox(),this.child.transform({x:-this._b.cx,y:-this._b.cy}),this}}),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,{move:function(e,t){this.transform({x:e,y:t})},plot:function(e){return this.attr("d",e||"M0,0")}}),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 t=["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 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<l;t++)r=(new n).text(o[t]).attr({dy:u*this.leading-(t==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 e,n="";for(e=t.length-1;e>=0;e--)this.style["font-"+t[e]]!=null&&(n+="font-"+t[e]+":"+this.style["font-"+t[e]]+";");return n+="text-anchor:"+this.style["text-anchor"]+";",n}}),n.prototype=new SVG.Shape,SVG.extend(n,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}});var r=["width","opacity","linecap","linejoin","miterlimit","dasharray","dashoffset"],i=["opacity","rule"];SVG.extend(SVG.Shape,{fill:function(e){var t;e.color!=null&&this.attr("fill",e.color);for(t=i.length-1;t>=0;t--)e[i[t]]!=null&&this.attr("fill-"+i[t],e[i[t]]);return this},stroke:function(e){var t;e.color&&this.attr("stroke",e.color);for(t=r.length-1;t>=0;t--)e[r[t]]!=null&&this.attr("stroke-"+r[t],e[r[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(e){var n,r={};for(n in e)n=="leading"?r[n]=e[n]:n=="anchor"?r["text-anchor"]=e[n]:t.indexOf(n)>-1?r["font-"+n]=e[n]:void 0;return this.attr(r).text(this.content)}})}).call(this);
\ No newline at end of file diff --git a/src/clip.js b/src/clip.js deleted file mode 100644 index ab7bebf..0000000 --- a/src/clip.js +++ /dev/null @@ -1,42 +0,0 @@ - -SVG.Clip = function Clip() { - this.constructor.call(this, SVG.create('clipPath')); - - // set unique id - this.id = 'svgjs_' + (SVG.did++); - this.attr('id', this.id); -}; - -// inherit from SVG.Element -SVG.Clip.prototype = new SVG.Element(); - -// include the container object -SVG.extend(SVG.Clip, SVG.Container); - -// add clipping functionality to element -SVG.extend(SVG.Element, { - - // clip element using another element - clip: function(b) { - var p = this.parent.defs().clip(); - b(p); - - return this.clipTo(p); - }, - - // distribute clipping path to svg element - clipTo: function(p) { - return this.attr('clip-path', 'url(#' + p.id + ')'); - } - -}); - -// add def-specific functions -SVG.extend(SVG.Defs, { - - // create clippath - clip: function() { - return this.put(new SVG.Clip()); - } - -});
\ No newline at end of file diff --git a/src/container.js b/src/container.js index 3656892..86c2827 100644 --- a/src/container.js +++ b/src/container.js @@ -36,7 +36,7 @@ SVG.Container = { c = this.children(); // iteralte all shapes - for (i = 1, l = c.length; i < l; i++) + for (i = 0, l = c.length; i < l; i++) if (c[i] instanceof SVG.Shape) b.apply(c[i], [i, c]); @@ -92,17 +92,17 @@ SVG.Container = { // create a polyline element polyline: function(p) { - return this.put(new SVG.Polyline().plot(p)); + return this.put(new Wrap(new SVG.Polyline())).plot(p); }, // create a polygon element polygon: function(p) { - return this.put(new SVG.Polygon().plot(p)); + return this.put(new Wrap(new SVG.Polygon())).plot(p); }, - // create a path element + // create a wrapped path element path: function(d) { - return this.put(new SVG.Path().plot(d)); + return this.put(new Wrap(new SVG.Path())).plot(d); }, // create image element, load image and set its size @@ -121,6 +121,11 @@ SVG.Container = { return this.defs().gradient(t, b); }, + // create masking element + mask: function() { + return this.defs().put(new SVG.Mask()); + }, + // get first child, skipping the defs node first: function() { return this.children()[1]; diff --git a/src/element.js b/src/element.js index 5db42b3..aaa6010 100644 --- a/src/element.js +++ b/src/element.js @@ -40,21 +40,35 @@ SVG.extend(SVG.Element, { // clone element clone: function() { - 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.attrs.rx * 2, this.attrs.ry * 2) : - n == 'image' ? - this.parent[n](this.src) : - n == 'text' ? - this.parent[n](this.content) : - this.parent[n](); - - // copy translations + var c; + + // if this is a wrapped shape + if (this instanceof Wrap) { + // build new wrapped shape + c = this.parent[this.child.node.nodeName](); + + // copy child attributes and transformations + c.child.trans = this.child.trans; + c.child.attr(this.child.attrs).transform({}); + + } else { + var 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.attrs.rx * 2, this.attrs.ry * 2) : + n == 'image' ? + this.parent[n](this.src) : + n == 'text' ? + this.parent[n](this.content) : + n == 'g' ? + this.parent.group() : + this.parent[n](); + } + + // copy transformations c.trans = this.trans; // apply attributes and translations diff --git a/src/mask.js b/src/mask.js index a9b457e..19f5562 100644 --- a/src/mask.js +++ b/src/mask.js @@ -16,27 +16,9 @@ SVG.extend(SVG.Mask, SVG.Container); // add clipping functionality to element SVG.extend(SVG.Element, { - // mask element using another element - mask: function(b) { - var m = this.parent.defs().mask(); - b(m); - - return this.maskTo(m); - }, - // distribute mask to svg element - maskTo: function(m) { - return this.attr('mask', 'url(#' + m.id + ')'); - } - -}); - -// add def-specific functions -SVG.extend(SVG.Defs, { - - // create clippath - mask: function() { - return this.put(new SVG.Mask()); + maskWith: function(e) { + return this.attr('mask', 'url(#' + (e instanceof SVG.Mask ? e : this.parent.mask().add(e)).id + ')'); } });
\ No newline at end of file diff --git a/src/path.js b/src/path.js index 5b71e50..6a6ee49 100644 --- a/src/path.js +++ b/src/path.js @@ -9,14 +9,14 @@ SVG.Path.prototype = new SVG.Shape(); // Add path-specific functions SVG.extend(SVG.Path, { + // move using transform + move: function(x, y) { + this.transform({ x: x, y: y }); + }, + // set path data plot: function(d) { return this.attr('d', d || 'M0,0'); - }, - - // move path using translate, path's don't take x and y - move: function(x, y) { - return this.transform({ x: x, y: y }); } });
\ No newline at end of file diff --git a/src/text.js b/src/text.js index 3fe3f0b..50a05b7 100644 --- a/src/text.js +++ b/src/text.js @@ -72,7 +72,7 @@ function TSpan() { this.constructor.call(this, SVG.create('tspan')); }; -// inherit from SVG.Element +// inherit from SVG.Shape TSpan.prototype = new SVG.Shape(); // include the container object diff --git a/src/wrap.js b/src/wrap.js new file mode 100644 index 0000000..4f785cd --- /dev/null +++ b/src/wrap.js @@ -0,0 +1,84 @@ + +function Wrap(e) { + this.constructor.call(this, SVG.create('g')); + + // insert and store child + this.node.insertBefore(e.node, null); + this.child = e; +}; + +// inherit from SVG.Shape +Wrap.prototype = new SVG.Shape(); + +// include the container object +SVG.extend(Wrap, { + + // move wrapper around + move: function(x, y) { + return this.center( + x + (this._b.width * this.child.trans.scaleX) / 2, + y + (this._b.height * this.child.trans.scaleY) / 2 + ); + }, + + // set the actual size in pixels + size: function(w, h) { + var s = w / this._b.width; + + this.child.transform({ + scaleX: s, + scaleY: h != null ? h / this._b.height : s + }); + + return this; + }, + + // move by center + center: function(x, y) { + return this.transform({ x: x, y: y }); + }, + + // create distributed attr + attr: function(a, v, n) { + // call individual attributes if an object is given + if (typeof a == 'object') { + for (v in a) this.attr(v, a[v]); + + // act as a getter if only one argument is given + } else if (arguments.length < 2) { + return a == 'transform' ? this.attrs[a] : this.child.attrs[a]; + + // apply locally for certain attributes + } else if (a == 'transform') { + this.attrs[a] = v; + + n != null ? + this.node.setAttributeNS(n, a, v) : + this.node.setAttribute(a, v); + + // apply attributes to child + } else { + this.child.attr(a, v, n); + } + + return this; + }, + + // distribute plot method to child + plot: function(d) { + // plot new shape + this.child.plot(d); + + // get new bbox and store size + this._b = this.child.bbox(); + + // reposition element withing wrapper + this.child.transform({ + x: -this._b.cx, + y: -this._b.cy + }); + + return this; + } + +});
\ No newline at end of file |