Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset width and height for an image when swapping src #4877

Merged
merged 4 commits into from
Apr 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm... did you forget to pass options to _setWidthHeight or is this intentional? Just wondering.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scriptspry Great question. I just tried using setSrc with width and height specified in options, and the provided options were not set. I ended up needing to use loadImage directly, which shouldn't be necessary. I vote for options to be passed in to this._setWidthHeight() or better, don't call _setWidthHeight at all and let setElement take care of it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was some reason this was done.
I'll try to recover it.
It could be that setSrc will intentionally reset the image from scratch, and if you need to set it with different widht/height/scropX/cropY you do that in the callback.
What would be wrong with this approach?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asturur Hi, nothing wrong with the approach of changing the width and height in the callback, it's just when I looked at the fabric source code I thought for sure not passing options along was a bug. I almost created a PR to fix it, then I noticed this comment and wanted to follow up.

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