diff --git a/src/elements_parser.js b/src/elements_parser.js index 170849ab..35637097 100644 --- a/src/elements_parser.js +++ b/src/elements_parser.js @@ -43,6 +43,7 @@ fabric.ElementsParser.prototype._createObject = function(klass, el, index) { } else { var obj = klass.fromElement(el, this.options); + this.resolveGradient(obj); this.reviver && this.reviver(el, obj); this.instances[index] = obj; this.checkIfDone(); @@ -52,18 +53,31 @@ fabric.ElementsParser.prototype._createObject = function(klass, el, index) { fabric.ElementsParser.prototype.createCallback = function(index, el) { var _this = this; return function(obj) { + _this.resolveGradient(obj); _this.reviver && _this.reviver(el, obj); _this.instances[index] = obj; _this.checkIfDone(); }; }; +fabric.ElementsParser.prototype.resolveGradient = function(obj) { + + var instanceFillValue = obj.get('fill'); + if (!(/^url\(/).test(instanceFillValue)) { + return; + } + var gradientId = instanceFillValue.slice(5, instanceFillValue.length - 1); + if (fabric.gradientDefs[gradientId]) { + obj.set('fill', + fabric.Gradient.fromElement(fabric.gradientDefs[gradientId], obj)); + } +}; + fabric.ElementsParser.prototype.checkIfDone = function() { if (--this.numElements === 0) { this.instances = this.instances.filter(function(el) { return el != null; }); - fabric.resolveGradients(this.instances); this.callback(this.instances); } }; diff --git a/src/parser.js b/src/parser.js index 49f2eb93..de6c1c64 100644 --- a/src/parser.js +++ b/src/parser.js @@ -632,27 +632,6 @@ extend(fabric, { - /** - * Initializes gradients on instances, according to gradients parsed from a document - * @param {Array} instances - */ - resolveGradients: function(instances) { - for (var i = instances.length; i--; ) { - var instanceFillValue = instances[i].get('fill'); - - if (!(/^url\(/).test(instanceFillValue)) { - continue; - } - - var gradientId = instanceFillValue.slice(5, instanceFillValue.length - 1); - - if (fabric.gradientDefs[gradientId]) { - instances[i].set('fill', - fabric.Gradient.fromElement(fabric.gradientDefs[gradientId], instances[i])); - } - } - }, - /** * Parses an SVG document, returning all of the gradient declarations found in it * @static