From 50e1e48708d86ac0392348190f36d6c0c557a1d6 Mon Sep 17 00:00:00 2001 From: wout Date: Sun, 6 Jan 2013 16:03:33 +0100 Subject: Added patterns --- src/container.js | 6 +++++- src/gradient.js | 2 +- src/pattern.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/pattern.js (limited to 'src') diff --git a/src/container.js b/src/container.js index 14ad570..a43ce75 100644 --- a/src/container.js +++ b/src/container.js @@ -101,10 +101,14 @@ SVG.Container = { nested: function() { return this.put(new SVG.Nested()); }, - // create element in defs + // Create gradient element in defs gradient: function(type, block) { return this.defs().gradient(type, block); }, + // Create pattern element in defs + pattern: function(width, height, block) { + return this.defs().pattern(width, height, block); + }, // Create masking element mask: function() { return this.defs().put(new SVG.Mask()); diff --git a/src/gradient.js b/src/gradient.js index b34200e..0414d72 100644 --- a/src/gradient.js +++ b/src/gradient.js @@ -60,7 +60,7 @@ SVG.extend(SVG.Gradient, { // SVG.extend(SVG.Defs, { - /* define clippath */ + /* define gradient */ gradient: function(type, block) { var element = this.put(new SVG.Gradient(type)); diff --git a/src/pattern.js b/src/pattern.js new file mode 100644 index 0000000..80985c8 --- /dev/null +++ b/src/pattern.js @@ -0,0 +1,43 @@ +SVG.Pattern = function Pattern(type) { + this.constructor.call(this, SVG.create('pattern')); + + /* set unique id */ + this.id = 'svgjs_' + (SVG.did++); + this.attr('id', this.id); +}; + +// Inherit from SVG.Element +SVG.Pattern.prototype = new SVG.Element(); + +// Include the container object +SVG.extend(SVG.Pattern, SVG.Container); + +// +SVG.extend(SVG.Pattern, { + // Return the fill id + fill: function() { + return 'url(#' + this.id + ')'; + } + +}); + +// +SVG.extend(SVG.Defs, { + + /* define gradient */ + pattern: function(width, height, block) { + var element = this.put(new SVG.Pattern()); + + /* invoke passed block */ + block(element); + + return element.attr({ + x: 0, + y: 0, + width: width, + height: height, + patternUnits: 'userSpaceOnUse' + }); + } + +}); \ No newline at end of file -- cgit v1.2.3