summaryrefslogtreecommitdiffstats
path: root/src/gradient.js
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2014-02-03 15:14:47 +0100
committerwout <wout@impinc.co.uk>2014-02-03 15:14:47 +0100
commite2304534e0cfb6f6f4ab8c37ea5275ae26cd455a (patch)
tree2386e9f361d9c5fa1308387aeeaf33f00241b3c5 /src/gradient.js
parent7a29817ffd764cf7ab6906250b57f234801c94e0 (diff)
downloadsvg.js-e2304534e0cfb6f6f4ab8c37ea5275ae26cd455a.tar.gz
svg.js-e2304534e0cfb6f6f4ab8c37ea5275ae26cd455a.zip
Implemented SVG.invent function and bumped to v1.0rc3
Diffstat (limited to 'src/gradient.js')
-rwxr-xr-xsrc/gradient.js154
1 files changed, 75 insertions, 79 deletions
diff --git a/src/gradient.js b/src/gradient.js
index c5c51d1..e95e18b 100755
--- a/src/gradient.js
+++ b/src/gradient.js
@@ -1,59 +1,68 @@
-SVG.Gradient = function(type) {
- this.constructor.call(this, SVG.create(type + 'Gradient'))
-
- /* store type */
- this.type = type
-}
-
-// Inherit from SVG.Container
-SVG.Gradient.prototype = new SVG.Container
-
-//
-SVG.extend(SVG.Gradient, {
- // From position
- from: function(x, y) {
- return this.type == 'radial' ?
- this.attr({ fx: new SVG.Number(x), fy: new SVG.Number(y) }) :
- this.attr({ x1: new SVG.Number(x), y1: new SVG.Number(y) })
- }
- // To position
-, to: function(x, y) {
- return this.type == 'radial' ?
- this.attr({ cx: new SVG.Number(x), cy: new SVG.Number(y) }) :
- this.attr({ x2: new SVG.Number(x), y2: new SVG.Number(y) })
- }
- // Radius for radial gradient
-, radius: function(r) {
- return this.type == 'radial' ?
- this.attr({ r: new SVG.Number(r) }) :
- this
- }
- // Add a color stop
-, at: function(stop) {
- return this.put(new SVG.Stop(stop))
- }
- // Update gradient
-, update: function(block) {
- /* remove all stops */
- this.clear()
-
- /* invoke passed block */
- block(this)
+SVG.Gradient = SVG.invent({
+ // Initialize node
+ create: function(type) {
+ this.constructor.call(this, SVG.create(type + 'Gradient'))
- return this
+ /* store type */
+ this.type = type
}
- // Return the fill id
-, fill: function() {
- return 'url(#' + this.attr('id') + ')'
- }
- // Alias string convertion to fill
-, toString: function() {
- return this.fill()
+
+ // Inherit from
+, inherit: SVG.Container
+
+ // Add class methods
+, extend: {
+ // From position
+ from: function(x, y) {
+ return this.type == 'radial' ?
+ this.attr({ fx: new SVG.Number(x), fy: new SVG.Number(y) }) :
+ this.attr({ x1: new SVG.Number(x), y1: new SVG.Number(y) })
+ }
+ // To position
+ , to: function(x, y) {
+ return this.type == 'radial' ?
+ this.attr({ cx: new SVG.Number(x), cy: new SVG.Number(y) }) :
+ this.attr({ x2: new SVG.Number(x), y2: new SVG.Number(y) })
+ }
+ // Radius for radial gradient
+ , radius: function(r) {
+ return this.type == 'radial' ?
+ this.attr({ r: new SVG.Number(r) }) :
+ this
+ }
+ // Add a color stop
+ , at: function(stop) {
+ return this.put(new SVG.Stop().update(stop))
+ }
+ // Update gradient
+ , update: function(block) {
+ /* remove all stops */
+ this.clear()
+
+ /* invoke passed block */
+ block(this)
+
+ return this
+ }
+ // Return the fill id
+ , fill: function() {
+ return 'url(#' + this.attr('id') + ')'
+ }
+ // Alias string convertion to fill
+ , toString: function() {
+ return this.fill()
+ }
}
+ // Add parent method
+, construct: {
+ // Create gradient element in defs
+ gradient: function(type, block) {
+ return this.defs().gradient(type, block)
+ }
+ }
})
-//
SVG.extend(SVG.Defs, {
// define gradient
gradient: function(type, block) {
@@ -67,37 +76,24 @@ SVG.extend(SVG.Defs, {
})
-//
-SVG.extend(SVG.Container, {
- // Create gradient element in defs
- gradient: function(type, block) {
- return this.defs().gradient(type, block)
- }
-
-})
+SVG.Stop = SVG.invent({
+ // Initialize node
+ create: 'stop'
+ // Inherit from
+, inherit: SVG.Element
-SVG.Stop = function(stop) {
- this.constructor.call(this, SVG.create('stop'))
-
- /* immediatelly build stop */
- this.update(stop)
-}
+ // Add class methods
+, extend: {
+ // add color stops
+ update: function(o) {
+ /* set attributes */
+ if (o.opacity != null) this.attr('stop-opacity', o.opacity)
+ if (o.color != null) this.attr('stop-color', o.color)
+ if (o.offset != null) this.attr('offset', new SVG.Number(o.offset))
-// Inherit from SVG.Element
-SVG.Stop.prototype = new SVG.Element
-
-//
-SVG.extend(SVG.Stop, {
- // add color stops
- update: function(o) {
- /* set attributes */
- if (o.opacity != null) this.attr('stop-opacity', o.opacity)
- if (o.color != null) this.attr('stop-color', o.color)
- if (o.offset != null) this.attr('offset', new SVG.Number(o.offset))
-
- return this
+ return this
+ }
}
-
-})
+})