diff options
Diffstat (limited to 'src/elements/Gradient.js')
-rw-r--r-- | src/elements/Gradient.js | 66 |
1 files changed, 45 insertions, 21 deletions
diff --git a/src/elements/Gradient.js b/src/elements/Gradient.js index 23de97d..7116fc8 100644 --- a/src/elements/Gradient.js +++ b/src/elements/Gradient.js @@ -12,71 +12,95 @@ import baseFind from '../modules/core/selector.js' import * as gradiented from '../modules/core/gradiented.js' export default class Gradient extends Container { - constructor (type, attrs) { + + constructor ( type, attrs ) { + super( - nodeOrNew(type + 'Gradient', typeof type === 'string' ? null : type), + nodeOrNew( type + 'Gradient', typeof type === 'string' ? null : type ), attrs ) + } // Add a color stop - stop (offset, color, opacity) { - return this.put(new Stop()).update(offset, color, opacity) + stop ( offset, color, opacity ) { + + return this.put( new Stop() ).update( offset, color, opacity ) + } // Update gradient - update (block) { + update ( block ) { + // remove all stops this.clear() // invoke passed block - if (typeof block === 'function') { - block.call(this, this) + if ( typeof block === 'function' ) { + + block.call( this, this ) + } return this + } // Return the fill id url () { + return 'url(#' + this.id() + ')' + } // Alias string convertion to fill toString () { + return this.url() + } // custom attr to handle transform - attr (a, b, c) { - if (a === 'transform') a = 'gradientTransform' - return super.attr(a, b, c) + attr ( a, b, c ) { + + if ( a === 'transform' ) a = 'gradientTransform' + return super.attr( a, b, c ) + } targets () { - return baseFind('svg [fill*="' + this.id() + '"]') + + return baseFind( 'svg [fill*="' + this.id() + '"]' ) + } bbox () { + return new Box() + } + } -extend(Gradient, gradiented) +extend( Gradient, gradiented ) -registerMethods({ +registerMethods( { Container: { // Create gradient element in defs - gradient: wrapWithAttrCheck(function (type, block) { - return this.defs().gradient(type, block) - }) + gradient: wrapWithAttrCheck( function ( type, block ) { + + return this.defs().gradient( type, block ) + + } ) }, // define gradient Defs: { - gradient: wrapWithAttrCheck(function (type, block) { - return this.put(new Gradient(type)).update(block) - }) + gradient: wrapWithAttrCheck( function ( type, block ) { + + return this.put( new Gradient( type ) ).update( block ) + + } ) } -}) +} ) -register(Gradient) +register( Gradient ) |