diff --git a/src/shapes/image.class.js b/src/shapes/image.class.js index feb137976bb..7d39e63d746 100644 --- a/src/shapes/image.class.js +++ b/src/shapes/image.class.js @@ -369,6 +369,7 @@ setSrc: function(src, callback, options) { fabric.util.loadImage(src, function(img) { this.setElement(img, options); + this._setWidthHeight(); callback(this); }, this, options && options.crossOrigin); return this; @@ -555,13 +556,13 @@ * @param {Object} [options] Object with width/height properties */ _setWidthHeight: function(options) { - this.width = 'width' in options + this.width = options && ('width' in options) ? options.width : (this.getElement() ? this.getElement().width || 0 : 0); - this.height = 'height' in options + this.height = options && ('height' in options) ? options.height : (this.getElement() ? this.getElement().height || 0 diff --git a/test/unit/image.js b/test/unit/image.js index 6eb1bc5ea48..b6299389897 100644 --- a/test/unit/image.js +++ b/test/unit/image.js @@ -134,6 +134,22 @@ }); }); + QUnit.test('setSrc', function(assert) { + var done = assert.async(); + createImageObject(function(image) { + image.width = 100; + image.height = 100; + assert.ok(typeof image.setSrc === 'function'); + assert.equal(image.width, 100); + assert.equal(image.height, 100); + image.setSrc(IMG_SRC, function() { + assert.equal(image.width, IMG_WIDTH); + assert.equal(image.height, IMG_HEIGHT); + done(); + }); + }); + }); + QUnit.test('toObject with no element', function(assert) { var done = assert.async(); createImageObject(function(image) {