Merge pull request #1595 from asturur/Load-Gradient-before-reviver

Load gradient before reviver
This commit is contained in:
Juriy Zaytsev 2014-08-13 19:14:48 +02:00
commit 2cc901f2f7
2 changed files with 15 additions and 22 deletions

View file

@ -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);
}
};

View file

@ -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