Skip to content

Commit

Permalink
Reset width and height for an image when swapping src (#4877)
Browse files Browse the repository at this point in the history
* ok

* added simple test

* working test
  • Loading branch information
asturur authored Apr 1, 2018
1 parent ea4b53c commit 1e5c64f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/unit/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1e5c64f

Please sign in to comment.