Fix loadFromJSON 404s breaking fabric (Image)

Prevent image 404s in Images from loadFromJSON from breaking everything
trying to get attributes of a source which is null, while passing it
upwards to allow dealing with images which failed to load outside of
Fabric.JS Issue #1079
This commit is contained in:
Max Kaplan 2014-03-07 01:12:50 -05:00
parent f2943ef2bb
commit 6f3f1ff7c9
2 changed files with 20 additions and 12 deletions

View file

@ -252,7 +252,9 @@
* @return {String} Source of an image
*/
getSrc: function() {
return this.getElement().src || this.getElement()._src;
if(this.getElement()){
return this.getElement().src || this.getElement()._src;
}
},
/**
@ -281,6 +283,10 @@
*/
applyFilters: function(callback) {
if(!this._originalElement){
return;
}
if (this.filters.length === 0) {
this._element = this._originalElement;
callback && callback();
@ -330,13 +336,13 @@
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_render: function(ctx) {
ctx.drawImage(
this._element,
-this.width / 2,
-this.height / 2,
this.width,
this.height
);
this._element && ctx.drawImage(
this._element,
-this.width / 2,
-this.height / 2,
this.width,
this.height
);
},
/**
@ -368,7 +374,9 @@
options || (options = { });
this.setOptions(options);
this._setWidthHeight(options);
this._element.crossOrigin = this.crossOrigin;
if(this._element){
this._element.crossOrigin = this.crossOrigin;
}
},
/**
@ -394,11 +402,11 @@
_setWidthHeight: function(options) {
this.width = 'width' in options
? options.width
: (this.getElement().width || 0);
: (this.getElement() ? this.getElement().width || 0 : 0);
this.height = 'height' in options
? options.height
: (this.getElement().height || 0);
: (this.getElement() ? this.getElement().height || 0 : 0);
},
/**

View file

@ -68,7 +68,7 @@
* @param {String} className Class to add to an element
*/
function addClass(element, className) {
if ((' ' + element.className + ' ').indexOf(' ' + className + ' ') === -1) {
if (element && (' ' + element.className + ' ').indexOf(' ' + className + ' ') === -1) {
element.className += (element.className ? ' ' : '') + className;
}
}