diff --git a/dist/all.js b/dist/all.js index 84d013e8..8648847f 100644 --- a/dist/all.js +++ b/dist/all.js @@ -2744,7 +2744,10 @@ fabric.util.animate = animate; }; function resolveGradients(instances) { - var ctx = fabric.Element.activeInstance.getContext(); + var activeInstance = fabric.Element.activeInstance, + ctx = activeInstance ? activeInstance.getContext() : null; + + if (!ctx) return; for (var i = instances.length; i--; ) { var instanceFillValue = instances[i].get('fill'); @@ -2755,12 +2758,7 @@ fabric.util.animate = animate; if (fabric.gradientDefs[gradientId]) { instances[i].set('fill', - fabric.Gradient.fromElement( - fabric.gradientDefs[gradientId], - ctx, - instances[i] - ) - ); + fabric.Gradient.fromElement(fabric.gradientDefs[gradientId], ctx, instances[i])); } } } diff --git a/src/parser.js b/src/parser.js index 46927e90..9f44edd9 100644 --- a/src/parser.js +++ b/src/parser.js @@ -287,24 +287,21 @@ }; function resolveGradients(instances) { - var ctx = fabric.Element.activeInstance.getContext(); - + var activeInstance = fabric.Element.activeInstance, + ctx = activeInstance ? activeInstance.getContext() : null; + + if (!ctx) return; + for (var i = instances.length; i--; ) { var instanceFillValue = instances[i].get('fill'); - + if (/^url\(/.test(instanceFillValue)) { - - // url(#grad1) --> grad1 + var gradientId = instanceFillValue.slice(5, instanceFillValue.length - 1); - + if (fabric.gradientDefs[gradientId]) { - instances[i].set('fill', - fabric.Gradient.fromElement( - fabric.gradientDefs[gradientId], - ctx, - instances[i] - ) - ); + instances[i].set('fill', + fabric.Gradient.fromElement(fabric.gradientDefs[gradientId], ctx, instances[i])); } } }