diff options
author | wout <wout@impinc.co.uk> | 2012-12-18 13:16:25 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2012-12-18 13:16:25 +0100 |
commit | ecc7c2b21cc484a0a57cd7df02a533c65f7bde97 (patch) | |
tree | ccf3cce51e73e7dfd454d44ce366901a1242635d | |
parent | 1d8f6a6bfda2c09a1fafe330ac2d91e9d38e2019 (diff) | |
download | svg.js-ecc7c2b21cc484a0a57cd7df02a533c65f7bde97.tar.gz svg.js-ecc7c2b21cc484a0a57cd7df02a533c65f7bde97.zip |
Added transform()
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | Rakefile | 10 | ||||
-rw-r--r-- | dist/svg.js | 234 | ||||
-rw-r--r-- | dist/svg.min.js | 4 | ||||
-rw-r--r-- | src/circle.js | 2 | ||||
-rw-r--r-- | src/clip.js | 3 | ||||
-rw-r--r-- | src/defs.js | 4 | ||||
-rw-r--r-- | src/doc.js | 2 | ||||
-rw-r--r-- | src/element.js | 94 | ||||
-rw-r--r-- | src/ellipse.js | 2 | ||||
-rw-r--r-- | src/g.js | 21 | ||||
-rw-r--r-- | src/group.js | 10 | ||||
-rw-r--r-- | src/image.js | 4 | ||||
-rw-r--r-- | src/path.js | 2 | ||||
-rw-r--r-- | src/shape.js | 35 | ||||
-rw-r--r-- | src/sugar.js | 49 | ||||
-rw-r--r-- | src/svg.js | 5 | ||||
-rw-r--r-- | src/utils.js | 10 |
18 files changed, 278 insertions, 221 deletions
@@ -1,10 +1,12 @@ -# svg.js +# Svg.js -svg.js is a small JavaScript library for manipulating SVG. +Svg.js is a small JavaScript library for manipulating SVG. Have a look at [svgjs.com](http://svgjs.com) for a examples. -svg.js is licensed under the terms of the MIT License. +Svg.js is licensed under the terms of the MIT License. + +Important: this library is still in alpha, therefore the API might be subject to change in the course of development. ## Usage @@ -1,6 +1,10 @@ -SVGJS_VERSION = '0.1' +SVGJS_VERSION = '0.1a' -DEFAULT_MODULES = %w[ svg utils container element doc defs shape rect circle ellipse path image g clip ] +CORE_MODULES = %w[ svg container element clip doc defs shape rect circle ellipse path image group ] + +OPTIONAL_MODULES = %w[ sugar ] + +DEFAULT_MODULES = CORE_MODULES + OPTIONAL_MODULES KILO = 1024 # how many bytes in a "kilobyte" @@ -10,7 +14,7 @@ task :default => :dist class BuildTask < Rake::FileTask def modules - prerequisites.map {|f| File.basename(f, '.js') } + prerequisites.map { |f| File.basename(f, '.js') } end def remove_prerequisites to_remove diff --git a/dist/svg.js b/dist/svg.js index f0b8cc8..1d6323c 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,4 +1,4 @@ -/* svg.js 0.1 - svg utils container element doc defs shape rect circle ellipse path image g clip - svgjs.com/license */ +/* svg.js 0.1a - svg container element clip doc defs shape rect circle ellipse path image group sugar - svgjs.com/license */ (function() { this.SVG = { @@ -7,6 +7,11 @@ create: function(e) { return document.createElementNS(this.ns, e); + }, + + extend: function(o, m) { + for (var k in m) + o.prototype[k] = m[k]; } }; @@ -14,15 +19,6 @@ return new SVG.Doc(e); }; - SVG.Utils = { - - merge: function(o, m) { - for (var k in m) - o.prototype[k] = m[k]; - } - - }; - SVG.Container = { add: function(e) { @@ -169,13 +165,13 @@ }; - SVG.Element = function Element(node) { - this.node = node; + SVG.Element = function Element(n) { + this.node = n; this.attrs = {}; }; // Add element-specific functions - SVG.Utils.merge(SVG.Element, { + SVG.extend(SVG.Element, { // move element to given x and y values move: function(x, y) { @@ -184,12 +180,7 @@ return this; }, - - // set element opacity - opacity: function(o) { - return this.attr('opacity', Math.max(0, Math.min(1, o))); - }, - + // set element size to given width and height size: function(w, h) { this.attr('width', w); @@ -197,20 +188,7 @@ return this; }, - - // clip element using another element - clip: function(block) { - var p = this.parentSVG().defs().clipPath(); - block(p); - - return this.clipTo(p); - }, - - // distribute clipping path to svg element - clipTo: function(p) { - return this.attr('clip-path', 'url(#' + p.id + ')'); - }, - + // remove element remove: function() { return this.parent != null ? this.parent.remove(this) : void 0; @@ -227,27 +205,69 @@ }, // set svg element attribute - attr: function(v) { - var a = arguments; + attr: function(a, v, n) { - this.attrs[a[0]] = a[1]; + if (arguments.length < 2) { + if (typeof a == 'object') + for (v in a) this.attr(v, a[v]); + else + return this.attrs[a]; - if (typeof v == 'object') - for (var k in v) - this.attr(k, v[k]); + } else { + this.attrs[a] = v; + n != null ? + this.node.setAttributeNS(n, a, v) : + this.node.setAttribute(a, v); - else if (a.length == 2) - this.node.setAttribute(a[0], a[1]); + } + + return this; + }, + + // transformations + transform: function(t, a) { + var n = [], + s = this.attr('transform') || '', + l = s.match(/([a-z]+\([^\)]+\))/g) || []; + + if (a === true) { + var v = t.match(/^([A-Za-z\-]+)/)[1], + r = new RegExp('^' + v); - else if (a.length == 3) - this.node.setAttributeNS(a[2], a[0], a[1]); - + for (var i = 0, s = l.length; i < s; i++) + if (!r.test(l[i])) + n.push(l[i]); + } else + n = l; + + n.push(t); + + this.attr('transform', n.join(' ')); + return this; }, // get bounding box bbox: function() { - return this.node.getBBox(); + var b = this.node.getBBox(); + + b.cx = b.x + b.width / 2; + b.cy = b.y + b.height / 2; + + return b; + }, + + // clip element using another element + clip: function(block) { + var p = this.parentSVG().defs().clipPath(); + block(p); + + return this.clipTo(p); + }, + + // distribute clipping path to svg element + clipTo: function(p) { + return this.attr('clip-path', 'url(#' + p.id + ')'); }, // private: find svg parent @@ -263,6 +283,21 @@ }); + var clipID = 0; + + SVG.Clip = function Clip() { + this.constructor.call(this, SVG.create('clipPath')); + this.id = '_' + (clipID++); + 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); + + SVG.Doc = function Doc(e) { this.constructor.call(this, SVG.create('svg')); @@ -280,7 +315,7 @@ SVG.Doc.prototype = new SVG.Element(); // include the container object - SVG.Utils.merge(SVG.Doc, SVG.Container); + SVG.extend(SVG.Doc, SVG.Container); SVG.Defs = function Defs() { this.constructor.call(this, SVG.create('defs')); @@ -290,10 +325,10 @@ SVG.Defs.prototype = new SVG.Element(); // include the container object - SVG.Utils.merge(SVG.Defs, SVG.Container); + SVG.extend(SVG.Defs, SVG.Container); // Add def-specific functions - SVG.Utils.merge(SVG.Defs, { + SVG.extend(SVG.Defs, { // define clippath clipPath: function() { @@ -311,39 +346,6 @@ // inherit from SVG.Element SVG.Shape.prototype = new SVG.Element(); - - // Add shape-specific functions - SVG.Utils.merge(SVG.Shape, { - - // set fill color and opacity - fill: function(f) { - if (f.color != null) - this.attr('fill', f.color); - - if (f.opacity != null) - this.attr('fill-opacity', f.opacity); - - return this; - }, - - // set stroke color and opacity - stroke: function(s) { - if (s.color) - this.attr('stroke', s.color); - - if (s.width != null) - this.attr('stroke-width', s.width); - - if (s.opacity != null) - this.attr('stroke-opacity', s.opacity); - - if (this.attrs['fill-opacity'] == null) - this.fill({ opacity: 0 }); - - return this; - } - - }); SVG.Rect = function Rect() { this.constructor.call(this, SVG.create('rect')); @@ -360,7 +362,7 @@ SVG.Circle.prototype = new SVG.Shape(); // Add circle-specific functions - SVG.Utils.merge(SVG.Circle, { + SVG.extend(SVG.Circle, { // custom move function move: function(x, y) { @@ -397,7 +399,7 @@ SVG.Ellipse.prototype = new SVG.Shape(); // Add ellipse-specific functions - SVG.Utils.merge(SVG.Ellipse, { + SVG.extend(SVG.Ellipse, { // custom move function move: function(x, y) { @@ -433,7 +435,7 @@ SVG.Path.prototype = new SVG.Shape(); // Add path-specific functions - SVG.Utils.merge(SVG.Path, { + SVG.extend(SVG.Path, { // set path data data: function(d) { @@ -451,10 +453,10 @@ SVG.Image.prototype = new SVG.Element(); // include the container object - SVG.Utils.merge(SVG.Image, SVG.Container); + SVG.extend(SVG.Image, SVG.Container); // Add image-specific functions - SVG.Utils.merge(SVG.Image, { + SVG.extend(SVG.Image, { // (re)load image load: function(u) { @@ -472,31 +474,55 @@ SVG.G.prototype = new SVG.Element(); // include the container object - SVG.Utils.merge(SVG.G, SVG.Container); - - // Add group-specific functions - SVG.Utils.merge(SVG.G, { + SVG.extend(SVG.G, SVG.Container); + + SVG.extend(SVG.Shape, { - // group rotation - rotate: function(d) { - this.attr('transform', 'rotate(' + d + ')'); + // set fill color and opacity + fill: function(f) { + if (f.color != null) + this.attr('fill', f.color); + + if (f.opacity != null) + this.attr('fill-opacity', f.opacity); + + return this; + }, + + // set stroke color and opacity + stroke: function(s) { + if (s.color) + this.attr('stroke', s.color); + + var a = ('width opacity linecap linejoin miterlimit dasharray dashoffset').split(' '); + + for (var i = a.length - 1; i >= 0; i--) + if (s[a[i]] != null) + this.attr('stroke-' + a[i], s[a[i]]); + + //-D if (this.attrs['fill-opacity'] == null) + //-D this.fill({ opacity: 0 }); + return this; } }); - - var clipID = 0; - SVG.Clip = function Clip() { - this.constructor.call(this, SVG.create('clipPath')); - this.id = '_' + (clipID++); - this.attr('id', this.id); - }; + // Add element-specific functions + SVG.extend(SVG.Element, { + + // rotation + rotate: function(o) { + var b = this.bbox(); + if (o.x == null) o.x = b.cx; + if (o.y == null) o.y = b.cy; - // inherit from SVG.Element - SVG.Clip.prototype = new SVG.Element(); + this.transform('rotate(' + (o.deg || 0) + ' ' + o.x + ' ' + o.y + ')', o.absolute); - // include the container object - SVG.Utils.merge(SVG.Clip, SVG.Container); + return this; + } + + }); + }).call(this); diff --git a/dist/svg.min.js b/dist/svg.min.js index c8f59e5..707a4f6 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js 0.1 - svg utils container element doc defs shape rect circle ellipse path image g clip - svgjs.com/license */ -(function(){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)}},this.svg=function(e){return new SVG.Doc(e)},SVG.Utils={merge:function(e,t){for(var n in t)e.prototype[n]=t[n]}},SVG.Container={add:function(e){return this.addAt(e)},addAt:function(e,t){return this.contains(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t+1]),e.parent=this),this},contains:function(e){return Array.prototype.indexOf.call(this.children(),e)>=0},children:function(){return this._children||[]},sendBack:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t-1)},bringForward:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t+1)},bringToFront:function(e){return this.contains(e)&&this.remove(e).add(e),this},sendToBottom:function(e){return this.contains(e)&&this.remove(e).addAt(e,0),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==null&&(this._defs=new SVG.Defs,this.add(this._defs)),this._defs},group:function(){var e=new SVG.G;return this.add(e),e},rect:function(e){return this.place(new SVG.Rect,e)},circle:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.r||e.radius?t.width=t.height=(e.r||e.radius)*2:t.width=t.height=e.width||e.height),this.place(new SVG.Circle,t)},ellipse:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.width&&(t.width=e.width),e.height&&(t.height=e.height),e.rx&&(t.width=e.rx*2),e.ry&&(t.height=e.ry*2)),this.place(new SVG.Ellipse,t)},path:function(e){return this.place(new SVG.Path,e)},image:function(e){return this.place(new SVG.Image,e)},place:function(e,t){return t!=null&&(t.x!=null&&t.y!=null&&e.move(t.x,t.y),t.width!=null&&t.height!=null&&e.size(t.width,t.height),t.data!=null&&e.data(t.data),t.src!=null&&e.load(t.src)),this.add(e),e}},SVG.Element=function(t){this.node=t,this.attrs={}},SVG.Utils.merge(SVG.Element,{move:function(e,t){return this.attr("x",e),this.attr("y",t),this},opacity:function(e){return this.attr("opacity",Math.max(0,Math.min(1,e)))},size:function(e,t){return this.attr("width",e),this.attr("height",t),this},clip:function(e){var t=this.parentSVG().defs().clipPath();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},parentSVG:function(){return this.parentDoc()},attr:function(e){var t=arguments;this.attrs[t[0]]=t[1];if(typeof e=="object")for(var n in e)this.attr(n,e[n]);else t.length==2?this.node.setAttribute(t[0],t[1]):t.length==3&&this.node.setAttributeNS(t[2],t[0],t[1]);return this},bbox:function(){return this.node.getBBox()},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t}}),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg")),this.attr("xmlns",SVG.ns),this.attr("version","1.1"),this.attr("xlink",SVG.xlink,SVG.ns),typeof t=="string"&&(t=document.getElementById(t)),t.appendChild(this.node)},SVG.Doc.prototype=new SVG.Element,SVG.Utils.merge(SVG.Doc,SVG.Container),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.Utils.merge(SVG.Defs,SVG.Container),SVG.Utils.merge(SVG.Defs,{clipPath:function(){var e=new SVG.Clip;return this.add(e),e}}),SVG.Shape=function(t){this.constructor.call(this,t)},SVG.Shape.prototype=new SVG.Element,SVG.Utils.merge(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){return e.color&&this.attr("stroke",e.color),e.width!=null&&this.attr("stroke-width",e.width),e.opacity!=null&&this.attr("stroke-opacity",e.opacity),this.attrs["fill-opacity"]==null&&this.fill({opacity:0}),this}}),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.Utils.merge(SVG.Circle,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center(),this},size:function(e,t){return this.attr("r",e/2),this.center(),this},center:function(e,t){var n=this.attrs.r||0;this.attr("cx",e||(this.attrs.x||0)+n),this.attr("cy",t||(this.attrs.y||0)+n)}}),SVG.Ellipse=function(){this.constructor.call(this,SVG.create("ellipse"))},SVG.Ellipse.prototype=new SVG.Shape,SVG.Utils.merge(SVG.Ellipse,{move:function(e,t){return this.attrs.x=e,this.attrs.y=t,this.center(),this},size:function(e,t){return this.attr("rx",e/2),this.attr("ry",t/2),this.center(),this},center:function(e,t){this.attr("cx",e||(this.attrs.x||0)+(this.attrs.rx||0)),this.attr("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.Utils.merge(SVG.Path,{data:function(e){return this.attr("d",e),this}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Element,SVG.Utils.merge(SVG.Image,SVG.Container),SVG.Utils.merge(SVG.Image,{load:function(e){return this.attr("href",e,SVG.xlink),this}}),SVG.G=function(){this.constructor.call(this,SVG.create("g"))},SVG.G.prototype=new SVG.Element,SVG.Utils.merge(SVG.G,SVG.Container),SVG.Utils.merge(SVG.G,{rotate:function(e){return this.attr("transform","rotate("+e+")"),this}});var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.Utils.merge(SVG.Clip,SVG.Container)}).call(this);
\ No newline at end of file +/* svg.js 0.1a - svg container element clip doc defs shape rect circle ellipse path image group sugar - svgjs.com/license */ +(function(){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]}},this.svg=function(e){return new SVG.Doc(e)},SVG.Container={add:function(e){return this.addAt(e)},addAt:function(e,t){return this.contains(e)||(t=t==null?this.children().length:t,this.children().splice(t,0,e),this.node.insertBefore(e.node,this.node.childNodes[t+1]),e.parent=this),this},contains:function(e){return Array.prototype.indexOf.call(this.children(),e)>=0},children:function(){return this._children||[]},sendBack:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t-1)},bringForward:function(e){var t=this.children().indexOf(e);if(t!==-1)return this.remove(e).addAt(e,t+1)},bringToFront:function(e){return this.contains(e)&&this.remove(e).add(e),this},sendToBottom:function(e){return this.contains(e)&&this.remove(e).addAt(e,0),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==null&&(this._defs=new SVG.Defs,this.add(this._defs)),this._defs},group:function(){var e=new SVG.G;return this.add(e),e},rect:function(e){return this.place(new SVG.Rect,e)},circle:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.r||e.radius?t.width=t.height=(e.r||e.radius)*2:t.width=t.height=e.width||e.height),this.place(new SVG.Circle,t)},ellipse:function(e){var t;return e!=null&&(t={x:e.x,y:e.y},e.width&&(t.width=e.width),e.height&&(t.height=e.height),e.rx&&(t.width=e.rx*2),e.ry&&(t.height=e.ry*2)),this.place(new SVG.Ellipse,t)},path:function(e){return this.place(new SVG.Path,e)},image:function(e){return this.place(new SVG.Image,e)},place:function(e,t){return t!=null&&(t.x!=null&&t.y!=null&&e.move(t.x,t.y),t.width!=null&&t.height!=null&&e.size(t.width,t.height),t.data!=null&&e.data(t.data),t.src!=null&&e.load(t.src)),this.add(e),e}},SVG.Element=function(t){this.node=t,this.attrs={}},SVG.extend(SVG.Element,{move:function(e,t){return this.attr("x",e),this.attr("y",t),this},size:function(e,t){return this.attr("width",e),this.attr("height",t),this},remove:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Doc)},parentSVG:function(){return this.parentDoc()},attr:function(e,t,n){if(arguments.length<2){if(typeof e!="object")return this.attrs[e];for(t in e)this.attr(t,e[t])}else this.attrs[e]=t,n!=null?this.node.setAttributeNS(n,e,t):this.node.setAttribute(e,t);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],o=new RegExp("^"+s);for(var u=0,r=i.length;u<r;u++)o.test(i[u])||n.push(i[u])}else n=i;return n.push(e),this.attr("transform",n.join(" ")),this},bbox:function(){var e=this.node.getBBox();return e.cx=e.x+e.width/2,e.cy=e.y+e.height/2,e},clip:function(e){var t=this.parentSVG().defs().clipPath();return e(t),this.clipTo(t)},clipTo:function(e){return this.attr("clip-path","url(#"+e.id+")")},_parent:function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t}});var e=0;SVG.Clip=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="_"+e++,this.attr("id",this.id)},SVG.Clip.prototype=new SVG.Element,SVG.extend(SVG.Clip,SVG.Container),SVG.Doc=function(t){this.constructor.call(this,SVG.create("svg")),this.attr("xmlns",SVG.ns),this.attr("version","1.1"),this.attr("xlink",SVG.xlink,SVG.ns),typeof t=="string"&&(t=document.getElementById(t)),t.appendChild(this.node)},SVG.Doc.prototype=new SVG.Element,SVG.extend(SVG.Doc,SVG.Container),SVG.Defs=function(){this.constructor.call(this,SVG.create("defs"))},SVG.Defs.prototype=new SVG.Element,SVG.extend(SVG.Defs,SVG.Container),SVG.extend(SVG.Defs,{clipPath:function(){var e=new SVG.Clip;return this.add(e),e}}),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(),this},size:function(e,t){return this.attr("r",e/2),this.center(),this},center:function(e,t){var n=this.attrs.r||0;this.attr("cx",e||(this.attrs.x||0)+n),this.attr("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(),this},size:function(e,t){return this.attr("rx",e/2),this.attr("ry",t/2),this.center(),this},center:function(e,t){this.attr("cx",e||(this.attrs.x||0)+(this.attrs.rx||0)),this.attr("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,{data:function(e){return this.attr("d",e),this}}),SVG.Image=function(){this.constructor.call(this,SVG.create("image"))},SVG.Image.prototype=new SVG.Element,SVG.extend(SVG.Image,SVG.Container),SVG.extend(SVG.Image,{load:function(e){return this.attr("href",e,SVG.xlink),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.Shape,{fill:function(e){return e.color!=null&&this.attr("fill",e.color),e.opacity!=null&&this.attr("fill-opacity",e.opacity),this},stroke:function(e){e.color&&this.attr("stroke",e.color);var t="width opacity linecap linejoin miterlimit dasharray dashoffset".split(" ");for(var n=t.length-1;n>=0;n--)e[t[n]]!=null&&this.attr("stroke-"+t[n],e[t[n]]);return this}}),SVG.extend(SVG.Element,{rotate:function(e){var t=this.bbox();return e.x==null&&(e.x=t.cx),e.y==null&&(e.y=t.cy),this.transform("rotate("+(e.deg||0)+" "+e.x+" "+e.y+")",e.absolute),this}})}).call(this);
\ No newline at end of file diff --git a/src/circle.js b/src/circle.js index 890d1d3..9576568 100644 --- a/src/circle.js +++ b/src/circle.js @@ -7,7 +7,7 @@ SVG.Circle = function Circle() { SVG.Circle.prototype = new SVG.Shape(); // Add circle-specific functions -SVG.Utils.merge(SVG.Circle, { +SVG.extend(SVG.Circle, { // custom move function move: function(x, y) { diff --git a/src/clip.js b/src/clip.js index 80d78d0..189b62c 100644 --- a/src/clip.js +++ b/src/clip.js @@ -1,3 +1,4 @@ + // initialize id sequence var clipID = 0; @@ -11,4 +12,4 @@ SVG.Clip = function Clip() { SVG.Clip.prototype = new SVG.Element(); // include the container object -SVG.Utils.merge(SVG.Clip, SVG.Container);
\ No newline at end of file +SVG.extend(SVG.Clip, SVG.Container); diff --git a/src/defs.js b/src/defs.js index 4ba7c8c..f5753e1 100644 --- a/src/defs.js +++ b/src/defs.js @@ -7,10 +7,10 @@ SVG.Defs = function Defs() { SVG.Defs.prototype = new SVG.Element(); // include the container object -SVG.Utils.merge(SVG.Defs, SVG.Container); +SVG.extend(SVG.Defs, SVG.Container); // Add def-specific functions -SVG.Utils.merge(SVG.Defs, { +SVG.extend(SVG.Defs, { // define clippath clipPath: function() { @@ -16,4 +16,4 @@ SVG.Doc = function Doc(e) { SVG.Doc.prototype = new SVG.Element(); // include the container object -SVG.Utils.merge(SVG.Doc, SVG.Container);
\ No newline at end of file +SVG.extend(SVG.Doc, SVG.Container);
\ No newline at end of file diff --git a/src/element.js b/src/element.js index 9536a79..39bca21 100644 --- a/src/element.js +++ b/src/element.js @@ -1,11 +1,11 @@ -SVG.Element = function Element(node) { - this.node = node; +SVG.Element = function Element(n) { + this.node = n; this.attrs = {}; }; // Add element-specific functions -SVG.Utils.merge(SVG.Element, { +SVG.extend(SVG.Element, { // move element to given x and y values move: function(x, y) { @@ -14,12 +14,7 @@ SVG.Utils.merge(SVG.Element, { return this; }, - - // set element opacity - opacity: function(o) { - return this.attr('opacity', Math.max(0, Math.min(1, o))); - }, - + // set element size to given width and height size: function(w, h) { this.attr('width', w); @@ -27,20 +22,7 @@ SVG.Utils.merge(SVG.Element, { return this; }, - - // clip element using another element - clip: function(block) { - var p = this.parentSVG().defs().clipPath(); - block(p); - - return this.clipTo(p); - }, - - // distribute clipping path to svg element - clipTo: function(p) { - return this.attr('clip-path', 'url(#' + p.id + ')'); - }, - + // remove element remove: function() { return this.parent != null ? this.parent.remove(this) : void 0; @@ -57,27 +39,69 @@ SVG.Utils.merge(SVG.Element, { }, // set svg element attribute - attr: function(v) { - var a = arguments; + attr: function(a, v, n) { - this.attrs[a[0]] = a[1]; + if (arguments.length < 2) { + if (typeof a == 'object') + for (v in a) this.attr(v, a[v]); + else + return this.attrs[a]; - if (typeof v == 'object') - for (var k in v) - this.attr(k, v[k]); + } else { + this.attrs[a] = v; + n != null ? + this.node.setAttributeNS(n, a, v) : + this.node.setAttribute(a, v); - else if (a.length == 2) - this.node.setAttribute(a[0], a[1]); + } + + return this; + }, + + // transformations + transform: function(t, a) { + var n = [], + s = this.attr('transform') || '', + l = s.match(/([a-z]+\([^\)]+\))/g) || []; + + if (a === true) { + var v = t.match(/^([A-Za-z\-]+)/)[1], + r = new RegExp('^' + v); - else if (a.length == 3) - this.node.setAttributeNS(a[2], a[0], a[1]); - + for (var i = 0, s = l.length; i < s; i++) + if (!r.test(l[i])) + n.push(l[i]); + } else + n = l; + + n.push(t); + + this.attr('transform', n.join(' ')); + return this; }, // get bounding box bbox: function() { - return this.node.getBBox(); + var b = this.node.getBBox(); + + b.cx = b.x + b.width / 2; + b.cy = b.y + b.height / 2; + + return b; + }, + + // clip element using another element + clip: function(block) { + var p = this.parentSVG().defs().clipPath(); + block(p); + + return this.clipTo(p); + }, + + // distribute clipping path to svg element + clipTo: function(p) { + return this.attr('clip-path', 'url(#' + p.id + ')'); }, // private: find svg parent diff --git a/src/ellipse.js b/src/ellipse.js index 1cfd0c1..f1638ee 100644 --- a/src/ellipse.js +++ b/src/ellipse.js @@ -7,7 +7,7 @@ SVG.Ellipse = function Ellipse() { SVG.Ellipse.prototype = new SVG.Shape(); // Add ellipse-specific functions -SVG.Utils.merge(SVG.Ellipse, { +SVG.extend(SVG.Ellipse, { // custom move function move: function(x, y) { diff --git a/src/g.js b/src/g.js deleted file mode 100644 index cd6de15..0000000 --- a/src/g.js +++ /dev/null @@ -1,21 +0,0 @@ - -SVG.G = function G() { - this.constructor.call(this, SVG.create('g')); -}; - -// inherit from SVG.Element -SVG.G.prototype = new SVG.Element(); - -// include the container object -SVG.Utils.merge(SVG.G, SVG.Container); - -// Add group-specific functions -SVG.Utils.merge(SVG.G, { - - // group rotation - rotate: function(d) { - this.attr('transform', 'rotate(' + d + ')'); - return this; - } - -});
\ No newline at end of file diff --git a/src/group.js b/src/group.js new file mode 100644 index 0000000..e3cb6a8 --- /dev/null +++ b/src/group.js @@ -0,0 +1,10 @@ + +SVG.G = function G() { + this.constructor.call(this, SVG.create('g')); +}; + +// inherit from SVG.Element +SVG.G.prototype = new SVG.Element(); + +// include the container object +SVG.extend(SVG.G, SVG.Container);
\ No newline at end of file diff --git a/src/image.js b/src/image.js index bc1dc60..107fef4 100644 --- a/src/image.js +++ b/src/image.js @@ -7,10 +7,10 @@ SVG.Image = function Image() { SVG.Image.prototype = new SVG.Element(); // include the container object -SVG.Utils.merge(SVG.Image, SVG.Container); +SVG.extend(SVG.Image, SVG.Container); // Add image-specific functions -SVG.Utils.merge(SVG.Image, { +SVG.extend(SVG.Image, { // (re)load image load: function(u) { diff --git a/src/path.js b/src/path.js index ee4484f..60ec9ef 100644 --- a/src/path.js +++ b/src/path.js @@ -7,7 +7,7 @@ SVG.Path = function Path() { SVG.Path.prototype = new SVG.Shape(); // Add path-specific functions -SVG.Utils.merge(SVG.Path, { +SVG.extend(SVG.Path, { // set path data data: function(d) { diff --git a/src/shape.js b/src/shape.js index 1a32a00..236a066 100644 --- a/src/shape.js +++ b/src/shape.js @@ -4,37 +4,4 @@ SVG.Shape = function Shape(element) { }; // inherit from SVG.Element -SVG.Shape.prototype = new SVG.Element(); - -// Add shape-specific functions -SVG.Utils.merge(SVG.Shape, { - - // set fill color and opacity - fill: function(f) { - if (f.color != null) - this.attr('fill', f.color); - - if (f.opacity != null) - this.attr('fill-opacity', f.opacity); - - return this; - }, - - // set stroke color and opacity - stroke: function(s) { - if (s.color) - this.attr('stroke', s.color); - - if (s.width != null) - this.attr('stroke-width', s.width); - - if (s.opacity != null) - this.attr('stroke-opacity', s.opacity); - - if (this.attrs['fill-opacity'] == null) - this.fill({ opacity: 0 }); - - return this; - } - -});
\ No newline at end of file +SVG.Shape.prototype = new SVG.Element();
\ No newline at end of file diff --git a/src/sugar.js b/src/sugar.js new file mode 100644 index 0000000..9b5b482 --- /dev/null +++ b/src/sugar.js @@ -0,0 +1,49 @@ + +// Add shape-specific functions +SVG.extend(SVG.Shape, { + + // set fill color and opacity + fill: function(f) { + if (f.color != null) + this.attr('fill', f.color); + + if (f.opacity != null) + this.attr('fill-opacity', f.opacity); + + return this; + }, + + // set stroke color and opacity + stroke: function(s) { + if (s.color) + this.attr('stroke', s.color); + + var a = ('width opacity linecap linejoin miterlimit dasharray dashoffset').split(' '); + + for (var i = a.length - 1; i >= 0; i--) + if (s[a[i]] != null) + this.attr('stroke-' + a[i], s[a[i]]); + + //-D if (this.attrs['fill-opacity'] == null) + //-D this.fill({ opacity: 0 }); + + return this; + } + +}); + +// Add element-specific functions +SVG.extend(SVG.Element, { + + // rotation + rotate: function(o) { + var b = this.bbox(); + if (o.x == null) o.x = b.cx; + if (o.y == null) o.y = b.cy; + + this.transform('rotate(' + (o.deg || 0) + ' ' + o.x + ' ' + o.y + ')', o.absolute); + + return this; + } + +}); @@ -5,6 +5,11 @@ this.SVG = { create: function(e) { return document.createElementNS(this.ns, e); + }, + + extend: function(o, m) { + for (var k in m) + o.prototype[k] = m[k]; } }; diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index 5cf1bda..0000000 --- a/src/utils.js +++ /dev/null @@ -1,10 +0,0 @@ - - -SVG.Utils = { - - merge: function(o, m) { - for (var k in m) - o.prototype[k] = m[k]; - } - -};
\ No newline at end of file |