diff options
author | wout <wout@impinc.co.uk> | 2012-12-29 15:20:50 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2012-12-29 15:20:50 +0100 |
commit | 331068a979be8b735e92f074ce0d2438f5f2127b (patch) | |
tree | 25983c08fc3c28ab7bdf5c394a4800b3b095d092 /src | |
parent | a20c0b1430fb97ff203a9afd6ba6945cf18e58d1 (diff) | |
download | svg.js-331068a979be8b735e92f074ce0d2438f5f2127b.tar.gz svg.js-331068a979be8b735e92f074ce0d2438f5f2127b.zip |
Added polyline and polygon, removed cicle
Diffstat (limited to 'src')
-rw-r--r-- | src/circle.js | 35 | ||||
-rw-r--r-- | src/container.js | 54 | ||||
-rw-r--r-- | src/ellipse.js | 2 | ||||
-rw-r--r-- | src/path.js | 2 | ||||
-rw-r--r-- | src/poly.js | 38 | ||||
-rw-r--r-- | src/text.js | 5 |
6 files changed, 65 insertions, 71 deletions
diff --git a/src/circle.js b/src/circle.js deleted file mode 100644 index 059813e..0000000 --- a/src/circle.js +++ /dev/null @@ -1,35 +0,0 @@ - -SVG.Circle = function Circle() { - this.constructor.call(this, SVG.create('circle')); -}; - -// inherit from SVG.Shape -SVG.Circle.prototype = new SVG.Shape(); - -// Add circle-specific functions -SVG.extend(SVG.Circle, { - - // custom move function - move: function(x, y) { - this.attrs.x = x; - this.attrs.y = y; - - return this.center(); - }, - - // custom size function (no height value here!) - size: function(w) { - return this.attr('r', w / 2).center(); - }, - - // position element by its center - center: function(x, y) { - var r = this.attrs.r || 0; - - return this.attr({ - cx: x || (this.attrs.x || 0) + r, - cy: y || (this.attrs.y || 0) + r - }); - } - -});
\ No newline at end of file diff --git a/src/container.js b/src/container.js index 9e73758..36988ea 100644 --- a/src/container.js +++ b/src/container.js @@ -12,6 +12,11 @@ SVG.Container = { return this; }, + put: function(e, i) { + this.add(e, i); + return e; + }, + has: function(e) { return this.children().indexOf(e) >= 0; }, @@ -47,59 +52,44 @@ SVG.Container = { level: function() { var d = this.defs(); - this.remove(d).add(d, 0); - - return this; + return this.remove(d).add(d, 0); }, group: function() { - var e = new SVG.G(); - this.add(e); - - return e; + return this.put(new SVG.G()); }, rect: function(w, h) { - var e = new SVG.Rect().size(w, h); - this.add(e); - - return e; + return this.put(new SVG.Rect().size(w, h)); }, - circle: function(r) { - var e = new SVG.Circle().size(r); - this.add(e); - - return e; + circle: function(d) { + return this.ellipse(d); }, ellipse: function(w, h) { - var e = new SVG.Ellipse().size(w, h); - this.add(e); - - return e; + return this.put(new SVG.Ellipse().size(w, h)); + }, + + polyline: function(p) { + return this.put(new SVG.Polyline().plot(p)); + }, + + polygon: function(p) { + return this.put(new SVG.Polygon().plot(p)); }, path: function(d) { - var e = new SVG.Path().plot(d); - this.add(e); - - return e; + return this.put(new SVG.Path().plot(d)); }, image: function(s, w, h) { w = w != null ? w : 100; - var e = new SVG.Image().load(s).size(w, h != null ? h : w); - this.add(e); - - return e; + return this.put(new SVG.Image().load(s).size(w, h != null ? h : w)); }, text: function(t) { - var e = new SVG.Text().text(t); - this.add(e); - - return e; + return this.put(new SVG.Text().text(t)); }, gradient: function(t, b) { diff --git a/src/ellipse.js b/src/ellipse.js index d897969..006f89f 100644 --- a/src/ellipse.js +++ b/src/ellipse.js @@ -20,7 +20,7 @@ SVG.extend(SVG.Ellipse, { // custom size function size: function(w, h) { return this. - attr({ rx: w / 2, ry: h / 2 }). + attr({ rx: w / 2, ry: (h != null ? h : w) / 2 }). center(); }, diff --git a/src/path.js b/src/path.js index 27c28b3..fef5809 100644 --- a/src/path.js +++ b/src/path.js @@ -11,7 +11,7 @@ SVG.extend(SVG.Path, { // set path data plot: function(d) { - return this.attr('d', d || 'M0,0L0,0'); + return this.attr('d', d || 'M0,0'); }, // move path using translate diff --git a/src/poly.js b/src/poly.js new file mode 100644 index 0000000..e504b4c --- /dev/null +++ b/src/poly.js @@ -0,0 +1,38 @@ + +SVG.Poly = { + + // set polygon data with default zero point if no data is passed + plot: function(p) { + return this.attr('points', p || '0,0'); + }, + + // move path using translate + move: function(x, y) { + return this.transform({ x: x, y: y }); + } + +}; + + + +SVG.Polyline = function Polyline() { + this.constructor.call(this, SVG.create('polyline')); +}; + +// inherit from SVG.Shape +SVG.Polyline.prototype = new SVG.Shape(); + +// Add polygon-specific functions +SVG.extend(SVG.Polyline, SVG.Poly); + + + +SVG.Polygon = function Polygon() { + this.constructor.call(this, SVG.create('polygon')); +}; + +// inherit from SVG.Shape +SVG.Polygon.prototype = new SVG.Shape(); + +// Add polygon-specific functions +SVG.extend(SVG.Polygon, SVG.Poly);
\ No newline at end of file diff --git a/src/text.js b/src/text.js index 7528c27..301373c 100644 --- a/src/text.js +++ b/src/text.js @@ -20,7 +20,8 @@ SVG.extend(SVG.Text, { var i, n, s = this._style(), p = this.parentDoc(), - a = t.split("\n"); + a = t.split("\n"), + f = this.style['font-size']; while (this.node.hasChildNodes()) this.node.removeChild(this.node.lastChild); @@ -29,7 +30,7 @@ SVG.extend(SVG.Text, { n = new TSpan(). text(a[i]). attr({ - dy: this.style['font-size'] * this.leading, + dy: f * this.leading - (i == 0 ? f * 0.3 : 0), x: (this.attr('x') || 0), style: s }); |