diff options
author | wout <wout@impinc.co.uk> | 2012-12-17 16:55:16 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2012-12-17 16:55:16 +0100 |
commit | 6a013f19d6fa84e538d31bca5f1466c5f3479630 (patch) | |
tree | 3807acc4240bb1dbd5f25406b262a240351fa67a | |
parent | 9ba8a780e0da4de4839df119f1d57d7f6327b1c6 (diff) | |
download | svg.js-6a013f19d6fa84e538d31bca5f1466c5f3479630.tar.gz svg.js-6a013f19d6fa84e538d31bca5f1466c5f3479630.zip |
Refactored code for readability and size
-rw-r--r-- | Rakefile | 2 | ||||
-rw-r--r-- | dist/svg.js | 459 | ||||
-rw-r--r-- | dist/svg.min.js | 4 | ||||
-rw-r--r-- | src/circle.js | 47 | ||||
-rw-r--r-- | src/clip_path.js | 6 | ||||
-rw-r--r-- | src/container.js | 4 | ||||
-rw-r--r-- | src/defs.js | 24 | ||||
-rw-r--r-- | src/document.js | 17 | ||||
-rw-r--r-- | src/element.js | 194 | ||||
-rw-r--r-- | src/ellipse.js | 45 | ||||
-rw-r--r-- | src/group.js | 21 | ||||
-rw-r--r-- | src/image.js | 21 | ||||
-rw-r--r-- | src/object.js | 40 | ||||
-rw-r--r-- | src/path.js | 17 | ||||
-rw-r--r-- | src/rect.js | 2 | ||||
-rw-r--r-- | src/shape.js | 53 | ||||
-rw-r--r-- | src/svg.js | 12 | ||||
-rw-r--r-- | src/utils.js | 10 |
18 files changed, 551 insertions, 427 deletions
@@ -1,6 +1,6 @@ SVGJS_VERSION = '0.1' -DEFAULT_MODULES = %w[ svg container object element document defs shape rect circle ellipse path image group clip_path ] +DEFAULT_MODULES = %w[ svg utils container element document defs shape rect circle ellipse path image group clip_path ] KILO = 1024 # how many bytes in a "kilobyte" diff --git a/dist/svg.js b/dist/svg.js index 63c8780..feea266 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -1,16 +1,27 @@ -/* svg.js 0.1 - svg container object element document defs shape rect circle ellipse path image group clip_path - svgjs.com/license */ +/* svg.js 0.1 - svg utils container element document defs shape rect circle ellipse path image group clip_path - svgjs.com/license */ (function() { - var SVG = { - namespace: "http://www.w3.org/2000/svg", - xlink: "http://www.w3.org/1999/xlink", + this.SVG = { + ns: 'http://www.w3.org/2000/svg', + xlink: 'http://www.w3.org/1999/xlink', - createElement: function(e) { - return document.createElementNS(this.namespace, e); + create: function(e) { + return document.createElementNS(this.ns, e); } }; - this.SVG = SVG; + this.svg = function(e) { + return new SVG.Document(e); + }; + + SVG.Utils = { + + merge: function(o, m) { + for (var k in m) + o.prototype[k] = m[k]; + } + + }; SVG.Container = { @@ -22,7 +33,7 @@ if (!this.contains(e)) { i = i == null ? this.children().length : i; this.children().splice(i, 0, e); - this.svgElement.insertBefore(e.svgElement, this.svgElement.childNodes[i + 1]); + this.node.insertBefore(e.node, this.node.childNodes[i + 1]); e.parent = this; } @@ -71,7 +82,7 @@ if (0 <= i && i < this.children().length) { var e = this.children()[i]; this.children().splice(i, 1); - this.svgElement.removeChild(e.svgElement); + this.node.removeChild(e.node); e.parent = null; } @@ -158,150 +169,153 @@ }; - Object.prototype.include = function(module) { - - for (var key in module) - this.prototype[key] = module[key]; - - if (module.included != null) - module.included.apply(this); - - return this; - }; - - SVG.Element = function Element(svgElement) { - this.svgElement = svgElement; - this.attributes = {}; + SVG.Element = function Element(node) { + this.node = node; + this.attrs = {}; }; - //-D // inherit from SVG.Object - //-D SVG.Element.prototype = new SVG.Object(); - - // move element to given x and y values - SVG.Element.prototype.move = function(x, y) { - this.setAttribute('x', x); - this.setAttribute('y', y); + // Add element-specific functions + SVG.Utils.merge(SVG.Element, { - return this; - }; + // move element to given x and y values + move: function(x, y) { + this.attr('x', x); + this.attr('y', y); - // set element opacity - SVG.Element.prototype.opacity = function(o) { - return this.setAttribute('opacity', Math.max(0, Math.min(1, o))); - }; + return this; + }, - // set element size to given width and height - SVG.Element.prototype.size = function(w, h) { - this.setAttribute('width', w); - this.setAttribute('height', h); - - return this; - }; + // set element opacity + opacity: function(o) { + return this.attr('opacity', Math.max(0, Math.min(1, o))); + }, - // clip element using another element - SVG.Element.prototype.clip = function(block) { - var p = this.parentSVG().defs().clipPath(); - block(p); - - return this.clipTo(p); - }; + // set element size to given width and height + size: function(w, h) { + this.attr('width', w); + this.attr('height', h); - // distribute clipping path to svg element - SVG.Element.prototype.clipTo = function(p) { - return this.setAttribute('clip-path', 'url(#' + p.id + ')'); - }; + return this; + }, - // remove element - SVG.Element.prototype.destroy = function() { - return this.parent != null ? this.parent.remove(this) : void 0; - }; + // clip element using another element + clip: function(block) { + var p = this.parentSVG().defs().clipPath(); + block(p); - // get parent document - SVG.Element.prototype.parentDoc = function() { - return this._findParent(SVG.Document); - }; + return this.clipTo(p); + }, - // get parent svg wrapper - SVG.Element.prototype.parentSVG = function() { - return this.parentDoc(); - }; + // distribute clipping path to svg element + clipTo: function(p) { + return this.attr('clip-path', 'url(#' + p.id + ')'); + }, - // set svg element attribute - SVG.Element.prototype.setAttribute = function(a, v, ns) { - this.attributes[a] = v; - - if (ns != null) - this.svgElement.setAttributeNS(ns, a, v); - else - this.svgElement.setAttribute(a, v); - - return this; - }; + // remove element + destroy: function() { + return this.parent != null ? this.parent.remove(this) : void 0; + }, - // set svg element attribute - SVG.Element.prototype.attr = function(v) { - if (typeof v == 'object') - for (var k in v) - this.setAttribute(k, v[k]); - else if (arguments.length == 2) - this.setAttribute(arguments[0], arguments[1]); - - return this; - }; + // get parent document + parentDoc: function() { + return this._parent(SVG.Document); + }, - // get bounding box - SVG.Element.prototype.getBBox = function() { - return this.svgElement.getBBox(); - }; + // get parent svg wrapper + parentSVG: function() { + return this.parentDoc(); + }, - // private: find svg parent - SVG.Element.prototype._findParent = function(pt) { - var e = this; - - while (e != null && !(e instanceof pt)) - e = e.parent; - - return e; - }; + //_D // set svg element attribute + //_D setAttribute: function(a, v, ns) { + //_D this.attrs[a] = v; + //_D + //_D if (ns != null) + //_D this.node.setAttributeNS(ns, a, v); + //_D else + //_D this.node.setAttribute(a, v); + //_D + //_D return this; + //_D }, + + // set svg element attribute + attr: function(v) { + var a = arguments; + + this.attrs[a[0]] = a[1]; + + if (typeof v == 'object') + for (var k in v) + this.attr(k, v[k]); + + else if (a.length == 2) + this.node.setAttribute(a[0], a[1]); + + else if (a.length == 3) + this.node.setAttributeNS(a[2], a[0], a[1]); + + return this; + }, + // get bounding box + bbox: function() { + return this.node.getBBox(); + }, + // private: find svg parent + _parent: function(pt) { + var e = this; + while (e != null && !(e instanceof pt)) + e = e.parent; + return e; + } + + }); - SVG.Document = function Document(c) { - this.constructor.call(this, SVG.createElement('svg')); + SVG.Document = function Document(e) { + this.constructor.call(this, SVG.create('svg')); - this.setAttribute('xmlns', SVG.namespace); - this.setAttribute('version', '1.1'); - this.setAttribute('xlink', 'http://www.w3.org/1999/xlink', SVG.namespace); + this.attr('xmlns', SVG.ns); + this.attr('version', '1.1'); + this.attr('xlink', SVG.xlink, SVG.ns); - document.getElementById(c).appendChild(this.svgElement); + if (typeof e == 'string') + e = document.getElementById(e); + + e.appendChild(this.node); }; // inherit from SVG.Element SVG.Document.prototype = new SVG.Element(); // include the container object - SVG.Document.include(SVG.Container); + SVG.Utils.merge(SVG.Document, SVG.Container); SVG.Defs = function Defs() { - this.constructor.call(this, SVG.createElement('defs')); + this.constructor.call(this, SVG.create('defs')); }; // inherit from SVG.Element SVG.Defs.prototype = new SVG.Element(); - // define clippath - SVG.Defs.prototype.clipPath = function() { - var e = new SVG.ClipPath(); - this.add(e); + // include the container object + SVG.Utils.merge(SVG.Defs, SVG.Container); + + // Add def-specific functions + SVG.Utils.merge(SVG.Defs, { - return e; - }; + // define clippath + clipPath: function() { + var e = new SVG.ClipPath(); + this.add(e); - // include the container object - SVG.Defs.include(SVG.Container); + return e; + } + + }); SVG.Shape = function Shape(element) { this.constructor.call(this, element); @@ -310,160 +324,191 @@ // inherit from SVG.Element SVG.Shape.prototype = new SVG.Element(); - // set fill color and opacity - SVG.Shape.prototype.fill = function(fill) { - if (fill.color != null) - this.setAttribute('fill', fill.color); + // Add shape-specific functions + SVG.Utils.merge(SVG.Shape, { - if (fill.opacity != null) - this.setAttribute('fill-opacity', fill.opacity); - - return this; - }; + // set fill color and opacity + fill: function(fill) { + if (fill.color != null) + this.attr('fill', fill.color); - // set stroke color and opacity - SVG.Shape.prototype.stroke = function(stroke) { - if (stroke.color != null) - this.setAttribute('stroke', stroke.color); - - if (stroke.width != null) - this.setAttribute('stroke-width', stroke.width); - - if (stroke.opacity != null) - this.setAttribute('stroke-opacity', stroke.opacity); - - if (this.attributes['fill-opacity'] == null) - this.fill({ opacity: 0 }); + if (fill.opacity != null) + this.attr('fill-opacity', fill.opacity); + + return this; + }, + + // set stroke color and opacity + stroke: function(stroke) { + if (stroke.color != null) + this.attr('stroke', stroke.color); + + if (stroke.width != null) + this.attr('stroke-width', stroke.width); + + if (stroke.opacity != null) + this.attr('stroke-opacity', stroke.opacity); + + if (this.attrs['fill-opacity'] == null) + this.fill({ opacity: 0 }); + + return this; + } - return this; - }; + }); SVG.Rect = function Rect() { - this.constructor.call(this, SVG.createElement('rect')); + this.constructor.call(this, SVG.create('rect')); }; // inherit from SVG.Shape SVG.Rect.prototype = new SVG.Shape(); SVG.Circle = function Circle() { - this.constructor.call(this, SVG.createElement('circle')); + this.constructor.call(this, SVG.create('circle')); }; // inherit from SVG.Shape SVG.Circle.prototype = new SVG.Shape(); - // custom move function - SVG.Circle.prototype.move = function(x, y) { - this.attributes.x = x; - this.attributes.y = y; - this.center(); + // Add circle-specific functions + SVG.Utils.merge(SVG.Circle, { - return this; - }; + // custom move function + move: function(x, y) { + this.attrs.x = x; + this.attrs.y = y; + this.center(); - // custom size function - SVG.Circle.prototype.size = function(w, h) { - this.setAttribute('r', w / 2); - this.center(); - - return this; - }; + return this; + }, - // private: center - SVG.Circle.prototype.center = function(cx, cy) { - var r = this.attributes.r || 0; + // custom size function + size: function(w, h) { + this.attr('r', w / 2); + this.center(); + + return this; + }, + + // private: center + center: function(cx, cy) { + var r = this.attrs.r || 0; + + this.attr('cx', cx || ((this.attrs.x || 0) + r)); + this.attr('cy', cy || ((this.attrs.y || 0) + r)); + } - this.setAttribute('cx', cx || ((this.attributes.x || 0) + r)); - this.setAttribute('cy', cy || ((this.attributes.y || 0) + r)); - }; + }); SVG.Ellipse = function Ellipse() { - this.constructor.call(this, SVG.createElement('ellipse')); + this.constructor.call(this, SVG.create('ellipse')); }; // inherit from SVG.Shape SVG.Ellipse.prototype = new SVG.Shape(); - // custom move function - SVG.Ellipse.prototype.move = function(x, y) { - this.attributes.x = x; - this.attributes.y = y; - this.center(); + // Add ellipse-specific functions + SVG.Utils.merge(SVG.Ellipse, { - return this; - }; + // custom move function + move: function(x, y) { + this.attrs.x = x; + this.attrs.y = y; + this.center(); - // custom size function - SVG.Ellipse.prototype.size = function(w, h) { - this.setAttribute('rx', w / 2); - this.setAttribute('ry', h / 2); - this.center(); - - return this; - }; + return this; + }, - SVG.Ellipse.prototype.center = function(cx, cy) { - this.setAttribute('cx', cx || ((this.attributes.x || 0) + (this.attributes.rx || 0))); - this.setAttribute('cy', cy || ((this.attributes.y || 0) + (this.attributes.ry || 0))); - }; + // custom size function + size: function(w, h) { + this.attr('rx', w / 2); + this.attr('ry', h / 2); + this.center(); + + return this; + }, + + center: function(cx, cy) { + this.attr('cx', cx || ((this.attrs.x || 0) + (this.attrs.rx || 0))); + this.attr('cy', cy || ((this.attrs.y || 0) + (this.attrs.ry || 0))); + } + + }); + SVG.Path = function Path() { - this.constructor.call(this, SVG.createElement('path')); + this.constructor.call(this, SVG.create('path')); }; // inherit from SVG.Shape SVG.Path.prototype = new SVG.Shape(); - // set path data - SVG.Path.prototype.data = function(d) { - this.setAttribute('d', d); - return this; - }; + // Add path-specific functions + SVG.Utils.merge(SVG.Path, { + + // set path data + data: function(d) { + this.attr('d', d); + return this; + } + + }); SVG.Image = function Image() { - this.constructor.call(this, SVG.createElement('image')); + this.constructor.call(this, SVG.create('image')); }; // inherit from SVG.Element SVG.Image.prototype = new SVG.Element(); - // (re)load image - SVG.Image.prototype.load = function(url) { - this.setAttribute('href', url, SVG.xlink); - return this; - }; - // include the container object - SVG.Image.include(SVG.Container); + SVG.Utils.merge(SVG.Image, SVG.Container); + + // Add image-specific functions + SVG.Utils.merge(SVG.Image, { + + // (re)load image + load: function(u) { + this.attr('href', u, SVG.xlink); + return this; + } + + }); SVG.Group = function Group() { - this.constructor.call(this, SVG.createElement("g")); + this.constructor.call(this, SVG.create('g')); }; // inherit from SVG.Element SVG.Group.prototype = new SVG.Element(); - // group rotation - SVG.Group.prototype.rotate = function(d) { - this.setAttribute('transform', 'rotate(' + d + ')'); - return this; - }; - // include the container object - SVG.Group.include(SVG.Container); + SVG.Utils.merge(SVG.Group, SVG.Container); + + // Add group-specific functions + SVG.Utils.merge(SVG.Group, { + + // group rotation + rotate: function(d) { + this.attr('transform', 'rotate(' + d + ')'); + return this; + } + + }); var clipID = 0; SVG.ClipPath = function ClipPath() { - this.constructor.call(this, SVG.createElement('clipPath')); + this.constructor.call(this, SVG.create('clipPath')); this.id = '_' + (clipID++); - this.setAttribute('id', this.id); + this.attr('id', this.id); }; // inherit from SVG.Element SVG.ClipPath.prototype = new SVG.Element(); // include the container object - SVG.ClipPath.include(SVG.Container); + SVG.Utils.merge(SVG.ClipPath, SVG.Container); }).call(this); diff --git a/dist/svg.min.js b/dist/svg.min.js index ee616ba..cc7eb88 100644 --- a/dist/svg.min.js +++ b/dist/svg.min.js @@ -1,2 +1,2 @@ -/* svg.js 0.1 - svg container object element document defs shape rect circle ellipse path image group clip_path - svgjs.com/license */ -(function(){var e={namespace:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",createElement:function(e){return document.createElementNS(this.namespace,e)}};this.SVG=e,e.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.svgElement.insertBefore(e.svgElement,this.svgElement.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.svgElement.removeChild(t.svgElement),t.parent=null}return this},defs:function(){return this._defs==null&&(this._defs=new e.Defs,this.add(this._defs)),this._defs},group:function(){var t=new e.Group;return this.add(t),t},rect:function(t){return this.place(new e.Rect,t)},circle:function(t){var n;return t!=null&&(n={x:t.x,y:t.y},t.r||t.radius?n.width=n.height=(t.r||t.radius)*2:n.width=n.height=t.width||t.height),this.place(new e.Circle,n)},ellipse:function(t){var n;return t!=null&&(n={x:t.x,y:t.y},t.width&&(n.width=t.width),t.height&&(n.height=t.height),t.rx&&(n.width=t.rx*2),t.ry&&(n.height=t.ry*2)),this.place(new e.Ellipse,n)},path:function(t){return this.place(new e.Path,t)},image:function(t){return this.place(new e.Image,t)},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}},Object.prototype.include=function(e){for(var t in e)this.prototype[t]=e[t];return e.included!=null&&e.included.apply(this),this},e.Element=function(t){this.svgElement=t,this.attributes={}},e.Element.prototype.move=function(e,t){return this.setAttribute("x",e),this.setAttribute("y",t),this},e.Element.prototype.opacity=function(e){return this.setAttribute("opacity",Math.max(0,Math.min(1,e)))},e.Element.prototype.size=function(e,t){return this.setAttribute("width",e),this.setAttribute("height",t),this},e.Element.prototype.clip=function(e){var t=this.parentSVG().defs().clipPath();return e(t),this.clipTo(t)},e.Element.prototype.clipTo=function(e){return this.setAttribute("clip-path","url(#"+e.id+")")},e.Element.prototype.destroy=function(){return this.parent!=null?this.parent.remove(this):void 0},e.Element.prototype.parentDoc=function(){return this._findParent(e.Document)},e.Element.prototype.parentSVG=function(){return this.parentDoc()},e.Element.prototype.setAttribute=function(e,t,n){return this.attributes[e]=t,n!=null?this.svgElement.setAttributeNS(n,e,t):this.svgElement.setAttribute(e,t),this},e.Element.prototype.attr=function(e){if(typeof e=="object")for(var t in e)this.setAttribute(t,e[t]);else arguments.length==2&&this.setAttribute(arguments[0],arguments[1]);return this},e.Element.prototype.getBBox=function(){return this.svgElement.getBBox()},e.Element.prototype._findParent=function(e){var t=this;while(t!=null&&!(t instanceof e))t=t.parent;return t},e.Document=function(n){this.constructor.call(this,e.createElement("svg")),this.setAttribute("xmlns",e.namespace),this.setAttribute("version","1.1"),this.setAttribute("xlink","http://www.w3.org/1999/xlink",e.namespace),document.getElementById(n).appendChild(this.svgElement)},e.Document.prototype=new e.Element,e.Document.include(e.Container),e.Defs=function(){this.constructor.call(this,e.createElement("defs"))},e.Defs.prototype=new e.Element,e.Defs.prototype.clipPath=function(){var t=new e.ClipPath;return this.add(t),t},e.Defs.include(e.Container),e.Shape=function(t){this.constructor.call(this,t)},e.Shape.prototype=new e.Element,e.Shape.prototype.fill=function(e){return e.color!=null&&this.setAttribute("fill",e.color),e.opacity!=null&&this.setAttribute("fill-opacity",e.opacity),this},e.Shape.prototype.stroke=function(e){return e.color!=null&&this.setAttribute("stroke",e.color),e.width!=null&&this.setAttribute("stroke-width",e.width),e.opacity!=null&&this.setAttribute("stroke-opacity",e.opacity),this.attributes["fill-opacity"]==null&&this.fill({opacity:0}),this},e.Rect=function(){this.constructor.call(this,e.createElement("rect"))},e.Rect.prototype=new e.Shape,e.Circle=function(){this.constructor.call(this,e.createElement("circle"))},e.Circle.prototype=new e.Shape,e.Circle.prototype.move=function(e,t){return this.attributes.x=e,this.attributes.y=t,this.center(),this},e.Circle.prototype.size=function(e,t){return this.setAttribute("r",e/2),this.center(),this},e.Circle.prototype.center=function(e,t){var n=this.attributes.r||0;this.setAttribute("cx",e||(this.attributes.x||0)+n),this.setAttribute("cy",t||(this.attributes.y||0)+n)},e.Ellipse=function(){this.constructor.call(this,e.createElement("ellipse"))},e.Ellipse.prototype=new e.Shape,e.Ellipse.prototype.move=function(e,t){return this.attributes.x=e,this.attributes.y=t,this.center(),this},e.Ellipse.prototype.size=function(e,t){return this.setAttribute("rx",e/2),this.setAttribute("ry",t/2),this.center(),this},e.Ellipse.prototype.center=function(e,t){this.setAttribute("cx",e||(this.attributes.x||0)+(this.attributes.rx||0)),this.setAttribute("cy",t||(this.attributes.y||0)+(this.attributes.ry||0))},e.Path=function(){this.constructor.call(this,e.createElement("path"))},e.Path.prototype=new e.Shape,e.Path.prototype.data=function(e){return this.setAttribute("d",e),this},e.Image=function(){this.constructor.call(this,e.createElement("image"))},e.Image.prototype=new e.Element,e.Image.prototype.load=function(t){return this.setAttribute("href",t,e.xlink),this},e.Image.include(e.Container),e.Group=function(){this.constructor.call(this,e.createElement("g"))},e.Group.prototype=new e.Element,e.Group.prototype.rotate=function(e){return this.setAttribute("transform","rotate("+e+")"),this},e.Group.include(e.Container);var t=0;e.ClipPath=function(){this.constructor.call(this,e.createElement("clipPath")),this.id="_"+t++,this.setAttribute("id",this.id)},e.ClipPath.prototype=new e.Element,e.ClipPath.include(e.Container)}).call(this);
\ No newline at end of file +/* svg.js 0.1 - svg utils container element document defs shape rect circle ellipse path image group clip_path - 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.Document(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.Group;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+")")},destroy:function(){return this.parent!=null?this.parent.remove(this):void 0},parentDoc:function(){return this._parent(SVG.Document)},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.Document=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.Document.prototype=new SVG.Element,SVG.Utils.merge(SVG.Document,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.ClipPath;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!=null&&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.Group=function(){this.constructor.call(this,SVG.create("g"))},SVG.Group.prototype=new SVG.Element,SVG.Utils.merge(SVG.Group,SVG.Container),SVG.Utils.merge(SVG.Group,{rotate:function(e){return this.attr("transform","rotate("+e+")"),this}});var e=0;SVG.ClipPath=function(){this.constructor.call(this,SVG.create("clipPath")),this.id="_"+e++,this.attr("id",this.id)},SVG.ClipPath.prototype=new SVG.Element,SVG.Utils.merge(SVG.ClipPath,SVG.Container)}).call(this);
\ No newline at end of file diff --git a/src/circle.js b/src/circle.js index f2549c7..890d1d3 100644 --- a/src/circle.js +++ b/src/circle.js @@ -1,32 +1,37 @@ SVG.Circle = function Circle() { - this.constructor.call(this, SVG.createElement('circle')); + this.constructor.call(this, SVG.create('circle')); }; // inherit from SVG.Shape SVG.Circle.prototype = new SVG.Shape(); -// custom move function -SVG.Circle.prototype.move = function(x, y) { - this.attributes.x = x; - this.attributes.y = y; - this.center(); +// Add circle-specific functions +SVG.Utils.merge(SVG.Circle, { - return this; -}; + // custom move function + move: function(x, y) { + this.attrs.x = x; + this.attrs.y = y; + this.center(); -// custom size function -SVG.Circle.prototype.size = function(w, h) { - this.setAttribute('r', w / 2); - this.center(); - - return this; -}; + return this; + }, + + // custom size function + size: function(w, h) { + this.attr('r', w / 2); + this.center(); + + return this; + }, + + // private: center + center: function(cx, cy) { + var r = this.attrs.r || 0; -// private: center -SVG.Circle.prototype.center = function(cx, cy) { - var r = this.attributes.r || 0; + this.attr('cx', cx || ((this.attrs.x || 0) + r)); + this.attr('cy', cy || ((this.attrs.y || 0) + r)); + } - this.setAttribute('cx', cx || ((this.attributes.x || 0) + r)); - this.setAttribute('cy', cy || ((this.attributes.y || 0) + r)); -};
\ No newline at end of file +});
\ No newline at end of file diff --git a/src/clip_path.js b/src/clip_path.js index 2eece49..57bff82 100644 --- a/src/clip_path.js +++ b/src/clip_path.js @@ -2,13 +2,13 @@ var clipID = 0; SVG.ClipPath = function ClipPath() { - this.constructor.call(this, SVG.createElement('clipPath')); + this.constructor.call(this, SVG.create('clipPath')); this.id = '_' + (clipID++); - this.setAttribute('id', this.id); + this.attr('id', this.id); }; // inherit from SVG.Element SVG.ClipPath.prototype = new SVG.Element(); // include the container object -SVG.ClipPath.include(SVG.Container);
\ No newline at end of file +SVG.Utils.merge(SVG.ClipPath, SVG.Container);
\ No newline at end of file diff --git a/src/container.js b/src/container.js index 9f13282..cc2fd4f 100644 --- a/src/container.js +++ b/src/container.js @@ -9,7 +9,7 @@ SVG.Container = { if (!this.contains(e)) { i = i == null ? this.children().length : i; this.children().splice(i, 0, e); - this.svgElement.insertBefore(e.svgElement, this.svgElement.childNodes[i + 1]); + this.node.insertBefore(e.node, this.node.childNodes[i + 1]); e.parent = this; } @@ -58,7 +58,7 @@ SVG.Container = { if (0 <= i && i < this.children().length) { var e = this.children()[i]; this.children().splice(i, 1); - this.svgElement.removeChild(e.svgElement); + this.node.removeChild(e.node); e.parent = null; } diff --git a/src/defs.js b/src/defs.js index 4661a54..4c252a5 100644 --- a/src/defs.js +++ b/src/defs.js @@ -1,17 +1,23 @@ + SVG.Defs = function Defs() { - this.constructor.call(this, SVG.createElement('defs')); + this.constructor.call(this, SVG.create('defs')); }; // inherit from SVG.Element SVG.Defs.prototype = new SVG.Element(); -// define clippath -SVG.Defs.prototype.clipPath = function() { - var e = new SVG.ClipPath(); - this.add(e); +// include the container object +SVG.Utils.merge(SVG.Defs, SVG.Container); + +// Add def-specific functions +SVG.Utils.merge(SVG.Defs, { - return e; -}; + // define clippath + clipPath: function() { + var e = new SVG.ClipPath(); + this.add(e); -// include the container object -SVG.Defs.include(SVG.Container);
\ No newline at end of file + return e; + } + +});
\ No newline at end of file diff --git a/src/document.js b/src/document.js index a510c44..32780c1 100644 --- a/src/document.js +++ b/src/document.js @@ -1,16 +1,19 @@ -SVG.Document = function Document(c) { - this.constructor.call(this, SVG.createElement('svg')); +SVG.Document = function Document(e) { + this.constructor.call(this, SVG.create('svg')); - this.setAttribute('xmlns', SVG.namespace); - this.setAttribute('version', '1.1'); - this.setAttribute('xlink', 'http://www.w3.org/1999/xlink', SVG.namespace); + this.attr('xmlns', SVG.ns); + this.attr('version', '1.1'); + this.attr('xlink', SVG.xlink, SVG.ns); - document.getElementById(c).appendChild(this.svgElement); + if (typeof e == 'string') + e = document.getElementById(e); + + e.appendChild(this.node); }; // inherit from SVG.Element SVG.Document.prototype = new SVG.Element(); // include the container object -SVG.Document.include(SVG.Container);
\ No newline at end of file +SVG.Utils.merge(SVG.Document, SVG.Container);
\ No newline at end of file diff --git a/src/element.js b/src/element.js index 3dcf785..7926e1f 100644 --- a/src/element.js +++ b/src/element.js @@ -1,99 +1,105 @@ -SVG.Element = function Element(svgElement) { - this.svgElement = svgElement; - this.attributes = {}; +SVG.Element = function Element(node) { + this.node = node; + this.attrs = {}; }; -//-D // inherit from SVG.Object -//-D SVG.Element.prototype = new SVG.Object(); - -// move element to given x and y values -SVG.Element.prototype.move = function(x, y) { - this.setAttribute('x', x); - this.setAttribute('y', y); - - return this; -}; - -// set element opacity -SVG.Element.prototype.opacity = function(o) { - return this.setAttribute('opacity', Math.max(0, Math.min(1, o))); -}; - -// set element size to given width and height -SVG.Element.prototype.size = function(w, h) { - this.setAttribute('width', w); - this.setAttribute('height', h); - - return this; -}; - -// clip element using another element -SVG.Element.prototype.clip = function(block) { - var p = this.parentSVG().defs().clipPath(); - block(p); - - return this.clipTo(p); -}; - -// distribute clipping path to svg element -SVG.Element.prototype.clipTo = function(p) { - return this.setAttribute('clip-path', 'url(#' + p.id + ')'); -}; - -// remove element -SVG.Element.prototype.destroy = function() { - return this.parent != null ? this.parent.remove(this) : void 0; -}; - -// get parent document -SVG.Element.prototype.parentDoc = function() { - return this._findParent(SVG.Document); -}; - -// get parent svg wrapper -SVG.Element.prototype.parentSVG = function() { - return this.parentDoc(); -}; - -// set svg element attribute -SVG.Element.prototype.setAttribute = function(a, v, ns) { - this.attributes[a] = v; +// Add element-specific functions +SVG.Utils.merge(SVG.Element, { - if (ns != null) - this.svgElement.setAttributeNS(ns, a, v); - else - this.svgElement.setAttribute(a, v); + // move element to given x and y values + move: function(x, y) { + this.attr('x', x); + this.attr('y', y); + + 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); + this.attr('height', h); + + 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 + destroy: function() { + return this.parent != null ? this.parent.remove(this) : void 0; + }, + + // get parent document + parentDoc: function() { + return this._parent(SVG.Document); + }, + + // get parent svg wrapper + parentSVG: function() { + return this.parentDoc(); + }, + + //_D // set svg element attribute + //_D setAttribute: function(a, v, ns) { + //_D this.attrs[a] = v; + //_D + //_D if (ns != null) + //_D this.node.setAttributeNS(ns, a, v); + //_D else + //_D this.node.setAttribute(a, v); + //_D + //_D return this; + //_D }, + + // set svg element attribute + attr: function(v) { + var a = arguments; + + this.attrs[a[0]] = a[1]; + + if (typeof v == 'object') + for (var k in v) + this.attr(k, v[k]); + + else if (a.length == 2) + this.node.setAttribute(a[0], a[1]); + + else if (a.length == 3) + this.node.setAttributeNS(a[2], a[0], a[1]); + + return this; + }, + + // get bounding box + bbox: function() { + return this.node.getBBox(); + }, + + // private: find svg parent + _parent: function(pt) { + var e = this; + + while (e != null && !(e instanceof pt)) + e = e.parent; + + return e; + } - return this; -}; - -// set svg element attribute -SVG.Element.prototype.attr = function(v) { - if (typeof v == 'object') - for (var k in v) - this.setAttribute(k, v[k]); - else if (arguments.length == 2) - this.setAttribute(arguments[0], arguments[1]); - - return this; -}; - -// get bounding box -SVG.Element.prototype.getBBox = function() { - return this.svgElement.getBBox(); -}; - -// private: find svg parent -SVG.Element.prototype._findParent = function(pt) { - var e = this; - - while (e != null && !(e instanceof pt)) - e = e.parent; - - return e; -}; - - - - +}); diff --git a/src/ellipse.js b/src/ellipse.js index 41443b9..1cfd0c1 100644 --- a/src/ellipse.js +++ b/src/ellipse.js @@ -1,30 +1,35 @@ SVG.Ellipse = function Ellipse() { - this.constructor.call(this, SVG.createElement('ellipse')); + this.constructor.call(this, SVG.create('ellipse')); }; // inherit from SVG.Shape SVG.Ellipse.prototype = new SVG.Shape(); -// custom move function -SVG.Ellipse.prototype.move = function(x, y) { - this.attributes.x = x; - this.attributes.y = y; - this.center(); +// Add ellipse-specific functions +SVG.Utils.merge(SVG.Ellipse, { - return this; -}; + // custom move function + move: function(x, y) { + this.attrs.x = x; + this.attrs.y = y; + this.center(); -// custom size function -SVG.Ellipse.prototype.size = function(w, h) { - this.setAttribute('rx', w / 2); - this.setAttribute('ry', h / 2); - this.center(); - - return this; -}; + return this; + }, -SVG.Ellipse.prototype.center = function(cx, cy) { - this.setAttribute('cx', cx || ((this.attributes.x || 0) + (this.attributes.rx || 0))); - this.setAttribute('cy', cy || ((this.attributes.y || 0) + (this.attributes.ry || 0))); -};
\ No newline at end of file + // custom size function + size: function(w, h) { + this.attr('rx', w / 2); + this.attr('ry', h / 2); + this.center(); + + return this; + }, + + center: function(cx, cy) { + this.attr('cx', cx || ((this.attrs.x || 0) + (this.attrs.rx || 0))); + this.attr('cy', cy || ((this.attrs.y || 0) + (this.attrs.ry || 0))); + } + +}); diff --git a/src/group.js b/src/group.js index e46035f..2236c09 100644 --- a/src/group.js +++ b/src/group.js @@ -1,16 +1,21 @@ SVG.Group = function Group() { - this.constructor.call(this, SVG.createElement("g")); + this.constructor.call(this, SVG.create('g')); }; // inherit from SVG.Element SVG.Group.prototype = new SVG.Element(); -// group rotation -SVG.Group.prototype.rotate = function(d) { - this.setAttribute('transform', 'rotate(' + d + ')'); - return this; -}; - // include the container object -SVG.Group.include(SVG.Container);
\ No newline at end of file +SVG.Utils.merge(SVG.Group, SVG.Container); + +// Add group-specific functions +SVG.Utils.merge(SVG.Group, { + + // group rotation + rotate: function(d) { + this.attr('transform', 'rotate(' + d + ')'); + return this; + } + +});
\ No newline at end of file diff --git a/src/image.js b/src/image.js index 3c60f2a..bc1dc60 100644 --- a/src/image.js +++ b/src/image.js @@ -1,16 +1,21 @@ SVG.Image = function Image() { - this.constructor.call(this, SVG.createElement('image')); + this.constructor.call(this, SVG.create('image')); }; // inherit from SVG.Element SVG.Image.prototype = new SVG.Element(); -// (re)load image -SVG.Image.prototype.load = function(url) { - this.setAttribute('href', url, SVG.xlink); - return this; -}; - // include the container object -SVG.Image.include(SVG.Container);
\ No newline at end of file +SVG.Utils.merge(SVG.Image, SVG.Container); + +// Add image-specific functions +SVG.Utils.merge(SVG.Image, { + + // (re)load image + load: function(u) { + this.attr('href', u, SVG.xlink); + return this; + } + +});
\ No newline at end of file diff --git a/src/object.js b/src/object.js index ecf43ee..1ec7ecc 100644 --- a/src/object.js +++ b/src/object.js @@ -1,11 +1,31 @@ -Object.prototype.include = function(module) { - - for (var key in module) - this.prototype[key] = module[key]; - - if (module.included != null) - module.included.apply(this); - - return this; -};
\ No newline at end of file +//SVG.Object = function Object() {}; +// +//Object.prototype.include = function(module) { +// +// for (var key in module) +// this.prototype[key] = module[key]; +// +// if (module.included != null) +// module.included.apply(this); +// +// return this; +//}; + +//SVG.Object = function Object() {}; +// +//SVG.Object.moduleKeywords = ['included', 'extended']; +// +//SVG.Object.include = function(module) { +// var key, value, _ref; +// for (key in module) { +// value = module[key]; +// if (Array.prototype.indexOf.call(this.moduleKeywords, key) < 0) { +// this.prototype[key] = value; +// } +// } +// if ((_ref = module.included) != null) { +// _ref.apply(this); +// } +// return this; +//};
\ No newline at end of file diff --git a/src/path.js b/src/path.js index c064a91..ee4484f 100644 --- a/src/path.js +++ b/src/path.js @@ -1,13 +1,18 @@ SVG.Path = function Path() { - this.constructor.call(this, SVG.createElement('path')); + this.constructor.call(this, SVG.create('path')); }; // inherit from SVG.Shape SVG.Path.prototype = new SVG.Shape(); -// set path data -SVG.Path.prototype.data = function(d) { - this.setAttribute('d', d); - return this; -};
\ No newline at end of file +// Add path-specific functions +SVG.Utils.merge(SVG.Path, { + + // set path data + data: function(d) { + this.attr('d', d); + return this; + } + +});
\ No newline at end of file diff --git a/src/rect.js b/src/rect.js index b08d68e..f286c5d 100644 --- a/src/rect.js +++ b/src/rect.js @@ -1,6 +1,6 @@ SVG.Rect = function Rect() { - this.constructor.call(this, SVG.createElement('rect')); + this.constructor.call(this, SVG.create('rect')); }; // inherit from SVG.Shape diff --git a/src/shape.js b/src/shape.js index 194ff84..a57b9f2 100644 --- a/src/shape.js +++ b/src/shape.js @@ -6,30 +6,35 @@ SVG.Shape = function Shape(element) { // inherit from SVG.Element SVG.Shape.prototype = new SVG.Element(); -// set fill color and opacity -SVG.Shape.prototype.fill = function(fill) { - if (fill.color != null) - this.setAttribute('fill', fill.color); +// Add shape-specific functions +SVG.Utils.merge(SVG.Shape, { - if (fill.opacity != null) - this.setAttribute('fill-opacity', fill.opacity); - - return this; -}; + // set fill color and opacity + fill: function(fill) { + if (fill.color != null) + this.attr('fill', fill.color); -// set stroke color and opacity -SVG.Shape.prototype.stroke = function(stroke) { - if (stroke.color != null) - this.setAttribute('stroke', stroke.color); - - if (stroke.width != null) - this.setAttribute('stroke-width', stroke.width); - - if (stroke.opacity != null) - this.setAttribute('stroke-opacity', stroke.opacity); - - if (this.attributes['fill-opacity'] == null) - this.fill({ opacity: 0 }); + if (fill.opacity != null) + this.attr('fill-opacity', fill.opacity); + + return this; + }, + + // set stroke color and opacity + stroke: function(stroke) { + if (stroke.color != null) + this.attr('stroke', stroke.color); + + if (stroke.width != null) + this.attr('stroke-width', stroke.width); + + if (stroke.opacity != null) + this.attr('stroke-opacity', stroke.opacity); + + if (this.attrs['fill-opacity'] == null) + this.fill({ opacity: 0 }); + + return this; + } - return this; -};
\ No newline at end of file +});
\ No newline at end of file @@ -1,9 +1,13 @@ this.SVG = { - namespace: "http://www.w3.org/2000/svg", - xlink: "http://www.w3.org/1999/xlink", + ns: 'http://www.w3.org/2000/svg', + xlink: 'http://www.w3.org/1999/xlink', - createElement: function(e) { - return document.createElementNS(this.namespace, e); + create: function(e) { + return document.createElementNS(this.ns, e); } +}; + +this.svg = function(e) { + return new SVG.Document(e); };
\ No newline at end of file diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..5cf1bda --- /dev/null +++ b/src/utils.js @@ -0,0 +1,10 @@ + + +SVG.Utils = { + + merge: function(o, m) { + for (var k in m) + o.prototype[k] = m[k]; + } + +};
\ No newline at end of file |