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

fix imageToObject with resize filters #2954

Merged
merged 10 commits into from
May 9, 2016
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
22 changes: 16 additions & 6 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,36 @@
* @return {Object} Object representation of an instance
*/
toObject: function(propertiesToInclude) {
var filters = [ ], element = this._originalElement;
var filters = [ ], resizeFilters = [ ],
element = this._originalElement,
scaleX = 1, scaleY = 1;

this.filters.forEach(function(filterObj) {
if (filterObj) {
if (filterObj.type === 'Resize') {
scaleX *= filterObj.scaleX;
scaleY *= filterObj.scaleY;
}
filters.push(filterObj.toObject());
}
});

this.resizeFilters.forEach(function(filterObj) {
filterObj && resizeFilters.push(filterObj.toObject());
});

var object = extend(this.callSuper('toObject', propertiesToInclude), {
src: element ? element.src || element._src : '',
filters: filters,
resizeFilters: resizeFilters,
crossOrigin: this.crossOrigin,
alignX: this.alignX,
alignY: this.alignY,
meetOrSlice: this.meetOrSlice
});

if (this.resizeFilters.length > 0) {
object.resizeFilters = this.resizeFilters.map(function(filterObj) {
return filterObj && filterObj.toObject();
});
}
object.width /= scaleX;
object.height /= scaleY;

if (!this.includeDefaultValues) {
this._removeDefaultValues(object);
Expand Down
1 change: 1 addition & 0 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
'backgroundColor': '',
'clipTo': null,
'filters': [],
'resizeFilters': [],
'fillRule': 'nonzero',
'globalCompositeOperation': 'source-over',
'transformMatrix': null,
Expand Down
29 changes: 29 additions & 0 deletions test/unit/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'backgroundColor': '',
'clipTo': null,
'filters': [],
'resizeFilters': [],
'fillRule': 'nonzero',
'globalCompositeOperation': 'source-over',
'skewX': 0,
Expand Down Expand Up @@ -156,6 +157,34 @@
});
});

asyncTest('toObject with applied resize filter', function() {
createImageObject(function(image) {
ok(typeof image.toObject == 'function');
var filter = new fabric.Image.filters.Resize({resizeType: 'bilinear', scaleX: 0.5, scaleY: 0.5});
image.filters.push(filter);
var width = image.width, height = image.height;
ok(image.filters[0] instanceof fabric.Image.filters.Resize, 'should inherit from fabric.Image.filters.Resize');
image.applyFilters(function() {
equal(image.width, width / 2, 'width should be halved now');
equal(image.height, height / 2, 'height should be halved now');
var toObject = image.toObject();
deepEqual(toObject.filters[0], filter.toObject());
equal(toObject.width, width, 'width is stored as before filters');
equal(toObject.height, height, 'height is stored as before filters');
fabric.Image.fromObject(toObject, function(imageFromObject) {
var filterFromObj = imageFromObject.filters[0];
ok(filterFromObj instanceof fabric.Image.filters.Resize, 'should inherit from fabric.Image.filters.Resize');
equal(filterFromObj.scaleY, 0.5);
equal(filterFromObj.scaleX, 0.5);
//equal(imageFromObject.width, width, 'on image reload width is halved again');
//equal(imageFromObject.height, height, 'on image reload width is halved again');
start();
});
});
});
});


// asyncTest('toObject without default values', function() {
// createImageObject(function(image) {

Expand Down