From: wout Date: Thu, 27 Dec 2012 12:40:58 +0000 (+0100) Subject: Reworked transform() X-Git-Tag: 0.2~64 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=819c6e5e8d5c0484364e3b937130851471d38965;p=svg.js.git Reworked transform() --- diff --git a/README.md b/README.md index 4f48b6c..60bb727 100644 --- a/README.md +++ b/README.md @@ -129,16 +129,30 @@ rect.attr('x', 50, 'http://www.w3.org/2000/svg'); ### Transform -With the transform attribute elements can be scaled, rotated, translated, skewed... : +With the transform attribute elements can be scaled, rotated, translated and skewed: ```javascript -rect.transform('rotate(45, 100, 100)'); +rect.transform({ + rotation: 45, + cx: 100, + cy: 100 +}); ``` -These operations are always absolute. If every transformation needs remembered, so multiple rotate operations will be stacked together making them relative to previous operations, a boolean value can be passed as a second argument: -```javascript -rect.transform('rotate(45, 100, 100)', true); +All available translations are: +```javascript +rect.transform({ + x: _[translation on x-axis]_, + y: _[translation on y-axis]_, + rotation: _[degrees]_, + cx: _[x rotation point]_, + cy: _[y rotation point]_, + scaleX: _[scaling on x-axis]_, + scaleX: _[scaling on y-axis]_, + skewX: _[skewing on x-axis]_, + skewY: _[skewing on y-axis]_ +}); ``` -More details on available transformations can be found here: -http://www.w3.org/TR/SVG/coords.html#TransformAttribute + +Important: matrix transformations are not yet supported. ### Move @@ -173,12 +187,14 @@ rect.remove(); ```javascript path.bbox(); ``` -This will return a SVGRect element as a js object: +This will return an object with the following values: ```javascript { height: 20, width: 20, y: 20, x: 10, cx: 30, cy: 20 } ``` +As opposed to the built-in `getBBox()` method any translations used with the `transform()` method will be taken into account. + ## Syntax sugar Fill and stroke are used quite often. Therefore two convenience methods are provided: @@ -198,15 +214,13 @@ rect.stroke({ color: '#f06', opacity: 0.6, width: 5 }); ### Rotate The 'rotate()' method will automatically rotate elements according to the centre of the element: ```javascript +// rotate(degrees) rect.rotate(45); ``` Unless you also define a rotation point: ```javascript -rect.rotate({ deg: 45, x: 100, y: 100 }); -``` -To make the operation relative: -```javascript -rect.rotate({ deg: 45, x: 100, y: 100, relative: true }); +// rotate(degrees, cx, cy) +rect.rotate(45, 100, 100); ``` _This functionality requires the sugar.js module which is included in the default distribution._ diff --git a/dist/svg.js b/dist/svg.js index c810fa0..b7b4adf 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js v0.1-25-ga2f2323 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ +/* svg.js v0.1-26-gf301d23 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ (function() { this.SVG = { @@ -147,6 +147,15 @@ SVG.Element = function Element(n) { this.node = n; this.attrs = {}; + this.trans = { + x: 0, + y: 0, + scaleX: 1, + scaleY: 1, + rotation: 0, + skewX: 0, + skewY: 0 + }; this._s = ('size family weight stretch variant style').split(' '); }; @@ -186,7 +195,7 @@ a == 'leading' ? this[a] : this.style[a]; - + else return this.attrs[a]; @@ -214,32 +223,60 @@ return this; }, - // transformations - transform: function(t, r) { - var n = [], + transform: function(o) { + // act as a getter if the first argument is a string + if (typeof o === 'string') + return this.trans[o]; + + // ... otherwise continue as a setter + var k, + t = [], + b = this.bbox(), s = this.attr('transform') || '', - l = s.match(/([a-z]+\([^\)]+\))/g) || []; + l = s.match(/[a-z]+\([^\)]+\)/g) || []; - if (r !== true) { - var v = t.match(/^([A-Za-z\-]+)/)[1], - r = new RegExp('^' + v); - - for (var i = 0, s = l.length; i < s; i++) - if (!r.test(l[i])) - n.push(l[i]); - - } else - n = l; + // merge values + for (k in this.trans) + if (o[k] != null) + this.trans[k] = o[k]; + + // alias current transformations + o = this.trans; + + // add rotate + if (o.rotation != 0) + t.push('rotate(' + o.rotation + ',' + (o.cx != null ? o.cx : b.cx) + ',' + (o.cy != null ? o.cy : b.cy) + ')'); + + // add scale + if (o.scaleX != 1 && o.scaleY != 1) + t.push('scale(' + o.sx + ',' + o.sy + ')'); + + // add skew on x axis + if (o.skewX != 0) + t.push('skewX(' + x.skewX + ')'); - n.push(t); + // add skew on y axis + if (o.skewY != 0) + t.push('skewY(' + x.skewY + ')') - return this.attr('transform', n.join(' ')); + // add translate + if (o.x != 0 && o.y != 0) + t.push('translate(' + o.x + ',' + o.y + ')'); + + // add only te required transformations + return this.attr('transform', t.join(' ')); }, - + // get bounding box bbox: function() { + // actual bounding box var b = this.node.getBBox(); + // include translations on x an y + b.x += this.trans.x; + b.y += this.trans.y; + + // add the center b.cx = b.x + b.width / 2; b.cy = b.y + b.height / 2; @@ -623,12 +660,12 @@ // set path data plot: function(d) { - return this.attr('d', d); + return this.attr('d', d || 'M0,0L0,0'); }, // move path using translate move: function(x, y) { - return this.transform('translate(' + x + ',' + y + ')'); + return this.transform({ x: x, y: y }); } }); @@ -758,18 +795,14 @@ SVG.extend(SVG.Element, { // rotation - rotate: function(o) { + rotate: function(d, x, y) { var b = this.bbox(); - if (typeof o == 'number') - o = { deg: o }; - - return this.transform( - 'rotate(' + - (o.deg || 0) + ' ' + - (o.x == null ? b.cx : o.x) + ' ' + - (o.y == null ? b.cx : o.y) + ')', - o.relative); + return this.transform({ + rotation: d || 0, + cx: x == null ? b.cx : x, + cy: y == null ? b.cx : y + }); } }); @@ -779,7 +812,7 @@ // move using translate move: function(x, y) { - return this.transform('translate(' + x + ' ' + y + ')'); + return this.transform({ x: x, y: y }); } }); @@ -789,9 +822,9 @@ // set font font: function(o) { - var a = {}; + var k, a = {}; - for (var k in o) + for (k in o) k == 'leading' ? a[k] = o[k] : k == 'anchor' ? @@ -801,7 +834,7 @@ void 0; return this.attr(a).text(this.content); - }, + } }); diff --git a/dist/svg.min.js b/dist/svg.min.js index 640492c..43a57dc 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js v0.1-25-ga2f2323 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ -function svg(e){return new SVG.Doc(e)}(function(){function n(){this.constructor.call(this,SVG.create("tspan"))}this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",create:function(e){return document.createElementNS(this.ns,e)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]||null),e.parent=this),this},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e,t){var n=[],r=this.attr("transform")||"",i=r.match(/([a-z]+\([^\)]+\))/g)||[];if(t!==!0){var s=e.match(/^([A-Za-z\-]+)/)[1],t=new RegExp("^"+s);for(var o=0,r=i.length;o1&&t.remove(this).add(this,e-1),this},front:function(){return this.parent.remove(this).add(this),this},back:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}}),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container);var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_clip_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.parent.defs().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){var e=new SVG.Clip;return this.add(e),e}});var t=0;SVG.Gradient=function(n){this.constructor.call(this,SVG.create(n+"Gradient")),this.id="svgjs_grad_"+t++,this.type=n,this.attr("id",this.id)},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){var t=new SVG.Stop(e);return this.add(t),t},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=new SVG.Gradient(e);return this.add(n),t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t="",n=["opacity","color"];for(var r=n.length-1;r>=0;r--)e[n[r]]!=null&&(t+="stop-"+n[r]+":"+e[n[r]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attr("offset")||0)+"%",style:t})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg"));var n=document.createElement("div");n.style.cssText="position:relative;width:100%;height:100%;",typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",width:"100%",height:"100%"}).attr("xlink",SVG.xlink,SVG.ns).defs(),t.appendChild(n),n.appendChild(this.node),this.stage()},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Circle=function(){this.constructor.call(this,SVG.create("circle"))},SVG.Circle.prototype=new SVG.Shape,SVG.extend(SVG.Circle,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e){return this.attr("r",e/2).center()},center:function(e,t){var n=this.attrs.r||0;return this.attr({cx:e||(this.attrs.x||0)+n,cy:t||(this.attrs.y||0)+n})}}),SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:t/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{plot:function(e){return this.attr("d",e)},move:function(e,t){return this.transform("translate("+e+","+t+")")}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("xlink:href",e,SVG.xlink)}}),SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica","text-anchor":"start"},this.leading=1.2,this.lines=[]},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text",this.lines=[];var t,r,i=this._style(),s=this.parentDoc(),o=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");return t+="text-anchor:"+this.style["text-anchor"]+";",t}}),n.prototype=new SVG.Shape,SVG.extend(n,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}}),SVG.extend(SVG.Shape,{fill:function(e){return e.color!=null&&this.attr("fill",e.color),e.opacity!=null&&this.attr("fill-opacity",e.opacity),this},stroke:function(e){e.color&&this.attr("stroke",e.color);var t="width opacity linecap linejoin miterlimit dasharray dashoffset".split(" ");for(var n=t.length-1;n>=0;n--)e[t[n]]!=null&&this.attr("stroke-"+t[n],e[t[n]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e){var t=this.bbox();return typeof e=="number"&&(e={deg:e}),this.transform("rotate("+(e.deg||0)+" "+(e.x==null?t.cx:e.x)+" "+(e.y==null?t.cx:e.y)+")",e.relative)}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform("translate("+e+" "+t+")")}}),SVG.extend(SVG.Text,{font:function(e){var t={};for(var n in e)n=="leading"?t[n]=e[n]:n=="anchor"?t["text-anchor"]=e[n]:this._s.indexOf(n)>-1?t["font-"+n]=e[n]:void 0;return this.attr(t).text(this.content)}})}).call(this); \ No newline at end of file +/* svg.js v0.1-26-gf301d23 - svg container element group arrange defs clip gradient doc shape rect circle ellipse path image text sugar - svgjs.com/license */ +function svg(e){return new SVG.Doc(e)}(function(){function n(){this.constructor.call(this,SVG.create("tspan"))}this.SVG={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",create:function(e){return document.createElementNS(this.ns,e)},extend:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e,t){return this.has(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t]||null),e.parent=this),this},has:function(e){return this.children().indexOf(e)>=0},children:function(){return this._children||(this._children=[])},remove:function(e){return this.removeAt(this.children().indexOf(e))},removeAt:function(e){if(0<=e&&e=0;r--)this.lines[r].attr(e,t);else n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);this._isStyle(e)&&(e=="text"?this.text(t):e=="leading"?this[e]=t:this.style[e]=t,this.text(this.content))}return this},transform:function(e){if(typeof e=="string")return this.trans[e];var t,n=[],r=this.bbox(),i=this.attr("transform")||"",s=i.match(/[a-z]+\([^\)]+\)/g)||[];for(t in this.trans)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)+")"),e.scaleX!=1&&e.scaleY!=1&&n.push("scale("+e.sx+","+e.sy+")"),e.skewX!=0&&n.push("skewX("+x.skewX+")"),e.skewY!=0&&n.push("skewY("+x.skewY+")"),e.x!=0&&e.y!=0&&n.push("translate("+e.x+","+e.y+")"),this.attr("transform",n.join(" "))},bbox:function(){var e=this.node.getBBox();return e.x+=this.trans.x,e.y+=this.trans.y,e.cx=e.x+e.width/2,e.cy=e.y+e.height/2,e},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t},_isStyle:function(e){return typeof e=="string"&&this._isText()?/^font|text|leading/.test(e):!1},_isText:function(){return this instanceof SVG.Text}}),SVG.G=function(){this.constructor.call(this,SVG.create("g"))},SVG.G.prototype=new SVG.Element,SVG.extend(SVG.G,SVG.Container),SVG.extend(SVG.Element,{siblings:function(){return this.parent.children()},forward:function(){var e=this.siblings().indexOf(this);return this.parent.remove(this).add(this,e+1),this},backward:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,e-1),this},front:function(){return this.parent.remove(this).add(this),this},back:function(){var e,t=this.parent.level();return e=this.siblings().indexOf(this),e>1&&t.remove(this).add(this,0),this}}),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container);var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="svgjs_clip_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.extend(SVG.Element,{clip:function(e){var t=this.parent.defs().clip();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")}}),SVG.extend(SVG.Defs,{clip:function(){var e=new SVG.Clip;return this.add(e),e}});var t=0;SVG.Gradient=function(n){this.constructor.call(this,SVG.create(n+"Gradient")),this.id="svgjs_grad_"+t++,this.type=n,this.attr("id",this.id)},SVG.Gradient.prototype=new SVG.Element,SVG.extend(SVG.Gradient,SVG.Container),SVG.extend(SVG.Gradient,{from:function(e,t){return this.type=="radial"?this.attr({fx:e+"%",fy:t+"%"}):this.attr({x1:e+"%",y1:t+"%"})},to:function(e,t){return this.type=="radial"?this.attr({cx:e+"%",cy:t+"%"}):this.attr({x2:e+"%",y2:t+"%"})},radius:function(e){return this.type=="radial"?this.attr({r:e+"%"}):this},at:function(e){var t=new SVG.Stop(e);return this.add(t),t},update:function(e){while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);return e(this),this},fill:function(){return"url(#"+this.id+")"}}),SVG.extend(SVG.Defs,{gradient:function(e,t){var n=new SVG.Gradient(e);return this.add(n),t(n),n}}),SVG.Stop=function(t){this.constructor.call(this,SVG.create("stop")),this.update(t)},SVG.Stop.prototype=new SVG.Element,SVG.extend(SVG.Stop,{update:function(e){var t="",n=["opacity","color"];for(var r=n.length-1;r>=0;r--)e[n[r]]!=null&&(t+="stop-"+n[r]+":"+e[n[r]]+";");return this.attr({offset:(e.offset!=null?e.offset:this.attr("offset")||0)+"%",style:t})}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg"));var n=document.createElement("div");n.style.cssText="position:relative;width:100%;height:100%;",typeof t=="string"&&(t=document.getElementById(t)),this.attr({xmlns:SVG.ns,version:"1.1",width:"100%",height:"100%"}).attr("xlink",SVG.xlink,SVG.ns).defs(),t.appendChild(n),n.appendChild(this.node),this.stage()},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Rect=function(){this.constructor.call(this,SVG.create("rect"))},SVG.Rect.prototype=new SVG.Shape,SVG.Circle=function(){this.constructor.call(this,SVG.create("circle"))},SVG.Circle.prototype=new SVG.Shape,SVG.extend(SVG.Circle,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e){return this.attr("r",e/2).center()},center:function(e,t){var n=this.attrs.r||0;return this.attr({cx:e||(this.attrs.x||0)+n,cy:t||(this.attrs.y||0)+n})}}),SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.extend(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center()},size:function(e,t){return this.attr({rx:e/2,ry:t/2}).center()},center:function(e,t){return this.attr({cx:e||(this.attrs.x||0)+(this.attrs.rx||0),cy:t||(this.attrs.y||0)+(this.attrs.ry||0)})}}),SVG.Path=function(){this.constructor.call(this,SVG.create("path"))},SVG.Path.prototype=new SVG.Shape,SVG.extend(SVG.Path,{plot:function(e){return this.attr("d",e||"M0,0L0,0")},move:function(e,t){return this.transform({x:e,y:t})}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Shape,SVG.extend(SVG.Image,{load:function(e){return this.attr("xlink:href",e,SVG.xlink)}}),SVG.Text=function(){this.constructor.call(this,SVG.create("text")),this.style={"font-size":16,"font-family":"Helvetica","text-anchor":"start"},this.leading=1.2,this.lines=[]},SVG.Text.prototype=new SVG.Shape,SVG.extend(SVG.Text,{text:function(e){this.content=e=e||"text",this.lines=[];var t,r,i=this._style(),s=this.parentDoc(),o=e.split("\n");while(this.node.hasChildNodes())this.node.removeChild(this.node.lastChild);for(t=0,l=o.length;t=0;e--)this.style["font-"+n[e]]!=null&&(t+="font-"+n[e]+":"+this.style["font-"+n[e]]+";");return t+="text-anchor:"+this.style["text-anchor"]+";",t}}),n.prototype=new SVG.Shape,SVG.extend(n,{text:function(e){return this.node.appendChild(document.createTextNode(e)),this}}),SVG.extend(SVG.Shape,{fill:function(e){return e.color!=null&&this.attr("fill",e.color),e.opacity!=null&&this.attr("fill-opacity",e.opacity),this},stroke:function(e){e.color&&this.attr("stroke",e.color);var t="width opacity linecap linejoin miterlimit dasharray dashoffset".split(" ");for(var n=t.length-1;n>=0;n--)e[t[n]]!=null&&this.attr("stroke-"+t[n],e[t[n]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e,t,n){var r=this.bbox();return this.transform({rotation:e||0,cx:t==null?r.cx:t,cy:n==null?r.cx:n})}}),SVG.extend(SVG.G,{move:function(e,t){return this.transform({x:e,y:t})}}),SVG.extend(SVG.Text,{font:function(e){var t,n={};for(t in e)t=="leading"?n[t]=e[t]:t=="anchor"?n["text-anchor"]=e[t]:this._s.indexOf(t)>-1?n["font-"+t]=e[t]:void 0;return this.attr(n).text(this.content)}})}).call(this); \ No newline at end of file diff --git a/src/element.js b/src/element.js index ff3ad37..03963f1 100644 --- a/src/element.js +++ b/src/element.js @@ -2,6 +2,15 @@ SVG.Element = function Element(n) { this.node = n; this.attrs = {}; + this.trans = { + x: 0, + y: 0, + scaleX: 1, + scaleY: 1, + rotation: 0, + skewX: 0, + skewY: 0 + }; this._s = ('size family weight stretch variant style').split(' '); }; @@ -41,7 +50,7 @@ SVG.extend(SVG.Element, { a == 'leading' ? this[a] : this.style[a]; - + else return this.attrs[a]; @@ -69,32 +78,60 @@ SVG.extend(SVG.Element, { return this; }, - // transformations - transform: function(t, r) { - var n = [], + transform: function(o) { + // act as a getter if the first argument is a string + if (typeof o === 'string') + return this.trans[o]; + + // ... otherwise continue as a setter + var k, + t = [], + b = this.bbox(), s = this.attr('transform') || '', - l = s.match(/([a-z]+\([^\)]+\))/g) || []; + l = s.match(/[a-z]+\([^\)]+\)/g) || []; - if (r !== true) { - var v = t.match(/^([A-Za-z\-]+)/)[1], - r = new RegExp('^' + v); - - for (var i = 0, s = l.length; i < s; i++) - if (!r.test(l[i])) - n.push(l[i]); - - } else - n = l; + // merge values + for (k in this.trans) + if (o[k] != null) + this.trans[k] = o[k]; + + // alias current transformations + o = this.trans; + + // add rotate + if (o.rotation != 0) + t.push('rotate(' + o.rotation + ',' + (o.cx != null ? o.cx : b.cx) + ',' + (o.cy != null ? o.cy : b.cy) + ')'); - n.push(t); + // add scale + if (o.scaleX != 1 && o.scaleY != 1) + t.push('scale(' + o.sx + ',' + o.sy + ')'); - return this.attr('transform', n.join(' ')); + // add skew on x axis + if (o.skewX != 0) + t.push('skewX(' + x.skewX + ')'); + + // add skew on y axis + if (o.skewY != 0) + t.push('skewY(' + x.skewY + ')') + + // add translate + if (o.x != 0 && o.y != 0) + t.push('translate(' + o.x + ',' + o.y + ')'); + + // add only te required transformations + return this.attr('transform', t.join(' ')); }, - + // get bounding box bbox: function() { + // actual bounding box var b = this.node.getBBox(); + // include translations on x an y + b.x += this.trans.x; + b.y += this.trans.y; + + // add the center b.cx = b.x + b.width / 2; b.cy = b.y + b.height / 2; diff --git a/src/path.js b/src/path.js index 4faf174..27c28b3 100644 --- a/src/path.js +++ b/src/path.js @@ -11,12 +11,12 @@ SVG.extend(SVG.Path, { // set path data plot: function(d) { - return this.attr('d', d); + return this.attr('d', d || 'M0,0L0,0'); }, // move path using translate move: function(x, y) { - return this.transform('translate(' + x + ',' + y + ')'); + return this.transform({ x: x, y: y }); } }); \ No newline at end of file diff --git a/src/sugar.js b/src/sugar.js index eba897d..bd8454d 100644 --- a/src/sugar.js +++ b/src/sugar.js @@ -33,18 +33,14 @@ SVG.extend(SVG.Shape, { SVG.extend(SVG.Element, { // rotation - rotate: function(o) { + rotate: function(d, x, y) { var b = this.bbox(); - if (typeof o == 'number') - o = { deg: o }; - - return this.transform( - 'rotate(' + - (o.deg || 0) + ' ' + - (o.x == null ? b.cx : o.x) + ' ' + - (o.y == null ? b.cx : o.y) + ')', - o.relative); + return this.transform({ + rotation: d || 0, + cx: x == null ? b.cx : x, + cy: y == null ? b.cx : y + }); } }); @@ -54,7 +50,7 @@ SVG.extend(SVG.G, { // move using translate move: function(x, y) { - return this.transform('translate(' + x + ' ' + y + ')'); + return this.transform({ x: x, y: y }); } }); @@ -64,9 +60,9 @@ SVG.extend(SVG.Text, { // set font font: function(o) { - var a = {}; + var k, a = {}; - for (var k in o) + for (k in o) k == 'leading' ? a[k] = o[k] : k == 'anchor' ? @@ -76,7 +72,7 @@ SVG.extend(SVG.Text, { void 0; return this.attr(a).text(this.content); - }, + } });