diff options
author | wout <wout@impinc.co.uk> | 2012-12-16 19:40:03 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2012-12-16 19:40:03 +0100 |
commit | 80ed33ea8e22a45684d72e1ae939c1ff98d0bbf7 (patch) | |
tree | 2d122949e5f0dfbf12f7af8629a5f2da733d216c | |
parent | 0237a8cae8d545a8b8182dad29863b4426a4f6d2 (diff) | |
download | svg.js-80ed33ea8e22a45684d72e1ae939c1ff98d0bbf7.tar.gz svg.js-80ed33ea8e22a45684d72e1ae939c1ff98d0bbf7.zip |
Added attr function for SVG.Element
-rw-r--r-- | src/circle.js | 10 | ||||
-rw-r--r-- | src/element.js | 11 | ||||
-rw-r--r-- | src/ellipse.js | 10 |
3 files changed, 21 insertions, 10 deletions
diff --git a/src/circle.js b/src/circle.js index 7d1fbee..f2549c7 100644 --- a/src/circle.js +++ b/src/circle.js @@ -10,7 +10,7 @@ SVG.Circle.prototype = new SVG.Shape(); SVG.Circle.prototype.move = function(x, y) { this.attributes.x = x; this.attributes.y = y; - this._center(); + this.center(); return this; }; @@ -18,15 +18,15 @@ SVG.Circle.prototype.move = function(x, y) { // custom size function SVG.Circle.prototype.size = function(w, h) { this.setAttribute('r', w / 2); - this._center(); + this.center(); return this; }; // private: center -SVG.Circle.prototype._center = function() { +SVG.Circle.prototype.center = function(cx, cy) { var r = this.attributes.r || 0; - this.setAttribute('cx', (this.attributes.x || 0) + r); - this.setAttribute('cy', (this.attributes.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 diff --git a/src/element.js b/src/element.js index bf729cd..d6f4685 100644 --- a/src/element.js +++ b/src/element.js @@ -68,6 +68,17 @@ SVG.Element.prototype.setAttribute = function(a, v, ns) { 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(); diff --git a/src/ellipse.js b/src/ellipse.js index 9e03277..41443b9 100644 --- a/src/ellipse.js +++ b/src/ellipse.js @@ -10,7 +10,7 @@ SVG.Ellipse.prototype = new SVG.Shape(); SVG.Ellipse.prototype.move = function(x, y) { this.attributes.x = x; this.attributes.y = y; - this._center(); + this.center(); return this; }; @@ -19,12 +19,12 @@ SVG.Ellipse.prototype.move = function(x, y) { SVG.Ellipse.prototype.size = function(w, h) { this.setAttribute('rx', w / 2); this.setAttribute('ry', h / 2); - this._center(); + this.center(); return this; }; -SVG.Ellipse.prototype._center = function() { - this.setAttribute('cx', (this.attributes.x || 0) + (this.attributes.rx || 0)); - this.setAttribute('cy', (this.attributes.y || 0) + (this.attributes.ry || 0)); +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 |