summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2012-12-18 13:16:25 +0100
committerwout <wout@impinc.co.uk>2012-12-18 13:16:25 +0100
commitecc7c2b21cc484a0a57cd7df02a533c65f7bde97 (patch)
treeccf3cce51e73e7dfd454d44ce366901a1242635d /src
parent1d8f6a6bfda2c09a1fafe330ac2d91e9d38e2019 (diff)
downloadsvg.js-ecc7c2b21cc484a0a57cd7df02a533c65f7bde97.tar.gz
svg.js-ecc7c2b21cc484a0a57cd7df02a533c65f7bde97.zip
Added transform()
Diffstat (limited to 'src')
-rw-r--r--src/circle.js2
-rw-r--r--src/clip.js3
-rw-r--r--src/defs.js4
-rw-r--r--src/doc.js2
-rw-r--r--src/element.js94
-rw-r--r--src/ellipse.js2
-rw-r--r--src/g.js21
-rw-r--r--src/group.js10
-rw-r--r--src/image.js4
-rw-r--r--src/path.js2
-rw-r--r--src/shape.js35
-rw-r--r--src/sugar.js49
-rw-r--r--src/svg.js5
-rw-r--r--src/utils.js10
14 files changed, 134 insertions, 109 deletions
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() {
diff --git a/src/doc.js b/src/doc.js
index 41b157f..e361e29 100644
--- a/src/doc.js
+++ b/src/doc.js
@@ -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;
+ }
+
+});
diff --git a/src/svg.js b/src/svg.js
index 331d756..f33f9e7 100644
--- a/src/svg.js
+++ b/src/svg.js
@@ -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