diff options
author | wout <wout@impinc.co.uk> | 2012-12-29 18:12:53 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2012-12-29 18:12:53 +0100 |
commit | 68367e494fcab754ae4833e64804a824032cf31c (patch) | |
tree | c9d5d9fe5995ccbbe819f29752c1133c7415882d /src/gradient.js | |
parent | 52ed3ba98715ed3c63e74ff29a9892972a7bec19 (diff) | |
download | svg.js-68367e494fcab754ae4833e64804a824032cf31c.tar.gz svg.js-68367e494fcab754ae4833e64804a824032cf31c.zip |
Code refactoring
Diffstat (limited to 'src/gradient.js')
-rw-r--r-- | src/gradient.js | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/src/gradient.js b/src/gradient.js index 0239b15..b9007ef 100644 --- a/src/gradient.js +++ b/src/gradient.js @@ -4,10 +4,13 @@ var gradID = 0; SVG.Gradient = function Gradient(t) { this.constructor.call(this, SVG.create(t + 'Gradient')); - this.id = 'svgjs_grad_' + (gradID++); - this.type = t; + // set unique id + this.id = 'svgjs_grad_' + (gradID++); this.attr('id', this.id); + + // store type + this.type = t; }; // inherit from SVG.Element @@ -40,21 +43,20 @@ SVG.extend(SVG.Gradient, { this; }, - // add color stops + // add a color stop at: function(o) { - var m = new SVG.Stop(o); - this.add(m); - - return m; + return this.put(new SVG.Stop(o)); }, // update gradient update: function(b) { + // remove all stops while (this.node.hasChildNodes()) this.node.removeChild(this.node.lastChild); + // invoke passed block b(this); - + return this; }, @@ -70,8 +72,9 @@ SVG.extend(SVG.Defs, { // define clippath gradient: function(t, b) { - var e = new SVG.Gradient(t); - this.add(e); + var e = this.put(new SVG.Gradient(t)); + + // invoke passed block b(e); return e; @@ -83,6 +86,7 @@ SVG.extend(SVG.Defs, { SVG.Stop = function Stop(o) { this.constructor.call(this, SVG.create('stop')); + // immediatelly build stop this.update(o); }; @@ -94,15 +98,18 @@ SVG.extend(SVG.Stop, { // add color stops update: function(o) { - var s = '', + var i, + s = '', a = ['opacity', 'color']; - - for (var i = a.length - 1; i >= 0; i--) + + // build style attribute + for (i = a.length - 1; i >= 0; i--) if (o[a[i]] != null) s += 'stop-' + a[i] + ':' + o[a[i]] + ';'; - + + // set attributes return this.attr({ - offset: (o.offset != null ? o.offset : this.attr('offset') || 0) + '%', + offset: (o.offset != null ? o.offset : this.attrs.offset || 0) + '%', style: s }); } |