diff --git a/src/shapes/image.class.js b/src/shapes/image.class.js index 02998d8f767..0b737a5a367 100644 --- a/src/shapes/image.class.js +++ b/src/shapes/image.class.js @@ -200,14 +200,14 @@ * @return {Object} Object representation of an instance */ toObject: function(propertiesToInclude) { - var filters = [ ]; + var filters = [ ], element = this._originalElement; this.filters.forEach(function(filterObj) { if (filterObj) { filters.push(filterObj.toObject()); } }); var object = extend(this.callSuper('toObject', propertiesToInclude), { - src: this._originalElement.src || this._originalElement._src, + src: element ? element.src || element._src : '', filters: filters, crossOrigin: this.crossOrigin, alignX: this.alignX, diff --git a/test/unit/image.js b/test/unit/image.js index f4173650f3b..d75ec21b2d3 100644 --- a/test/unit/image.js +++ b/test/unit/image.js @@ -118,6 +118,23 @@ }); }); + asyncTest('toObject with no element', function() { + createImageObject(function(image) { + image._originalElement = null; + ok(typeof image.toObject == 'function'); + var toObject = image.toObject(); + // workaround for node-canvas sometimes producing images with width/height and sometimes not + if (toObject.width === 0) { + toObject.width = IMG_WIDTH; + } + if (toObject.height === 0) { + toObject.height = IMG_HEIGHT; + } + deepEqual(toObject, fabric.util.object.extend(REFERENCE_IMG_OBJECT, {src: ''})); + start(); + }); + }); + asyncTest('toObject with resize filter', function() { createImageObject(function(image) { ok(typeof image.toObject == 'function');