Skip to content

Commit

Permalink
use real class for Object.fromObject
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed Feb 15, 2022
1 parent 7b78236 commit 9f5ef64
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 71 deletions.
1 change: 0 additions & 1 deletion src/filters/blendimage_filter.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@
* Returns filter instance from an object representation
* @static
* @param {Object} object Object to create an instance from
* @param {function} callback to be invoked after filter creation
* @return {fabric.Image.filters.BlendImage} Instance of fabric.Image.filters.BlendImage
*/
fabric.Image.filters.BlendImage.fromObject = function(object) {
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/circle.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
* @returns {Promise<fabric.Circle>}
*/
fabric.Circle.fromObject = function(object) {
return fabric.Object._fromObject('Circle', object);
return fabric.Object._fromObject(fabric.Circle, object);
};

})(typeof exports !== 'undefined' ? exports : this);
2 changes: 1 addition & 1 deletion src/shapes/ellipse.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
* @returns {Promise<fabric.Ellipse>}
*/
fabric.Ellipse.fromObject = function(object) {
return fabric.Object._fromObject('Ellipse', object);
return fabric.Object._fromObject(fabric.Ellipse, object);
};

})(typeof exports !== 'undefined' ? exports : this);
3 changes: 0 additions & 3 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
* Please check video element events for seeking.
* @param {HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String} element Image element
* @param {Object} [options] Options object
* @param {function} [callback] callback function to call after eventual filters applied.
* @return {fabric.Image} thisArg
*/
initialize: function(element, options) {
Expand Down Expand Up @@ -370,7 +369,6 @@
/**
* Sets source of an image
* @param {String} src Source string (URL)
* @param {Function} [callback] Callback is invoked when image has been loaded (and all filters have been applied)
* @param {Object} [options] Options object
* @param {String} [options.crossOrigin] crossOrigin value (one of "", "anonymous", "use-credentials")
* @see https://developer.mozilla.org/en-US/docs/HTML/CORS_settings_attributes
Expand Down Expand Up @@ -709,7 +707,6 @@
* Creates an instance of fabric.Image from an URL string
* @static
* @param {String} url URL to create an image from
* @param {Function} [callback] Callback to invoke when image is created (newly created image is passed as a first argument). Second argument is a boolean indicating if an error occurred or not.
* @param {Object} [imgOptions] Options object
*/
fabric.Image.fromURL = function(url, imgOptions) {
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,6 @@
* @returns {Promise<fabric.IText>}
*/
fabric.IText.fromObject = function(object) {
return fabric.Object._fromObject('IText', object, 'text');
return fabric.Object._fromObject(fabric.IText, object, 'text');
};
})();
3 changes: 1 addition & 2 deletions src/shapes/line.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,12 @@
* @static
* @memberOf fabric.Line
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as first argument
* @returns {Promise<fabric.Line>}
*/
fabric.Line.fromObject = function(object) {
var options = clone(object, true);
options.points = [object.x1, object.y1, object.x2, object.y2];
return fabric.Object._fromObject('Line', options, 'points').then(function(fabricLine) {
return fabric.Object._fromObject(fabric.Line, options, 'points').then(function(fabricLine) {
delete fabricLine.points;
return fabricLine;
});
Expand Down
21 changes: 8 additions & 13 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@
},

/**
* Clones an instance, using a callback method will work for every object.
* Clones an instance.
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @returns {Promise<fabric.Object>}
*/
Expand All @@ -1670,9 +1670,7 @@
* If you need to get a real Jpeg or Png from an object, using toDataURL is the right way to do it.
* toCanvasElement and then toBlob from the obtained canvas is also a good option.
* This method is sync now, but still support the callback because we did not want to break.
* When fabricJS 5.0 will be planned, this will probably be changed to not have a callback.
* @param {Function} callback callback, invoked with an instance as a first argument
* @param {Object} [options] for clone as image, passed to toDataURL
* When fabricJS 5.0 will be planned, this will probably be changed to not have a callback. * @param {Object} [options] for clone as image, passed to toDataURL
* @param {Number} [options.multiplier=1] Multiplier to scale by
* @param {Number} [options.left] Cropping left offset. Introduced in v1.2.14
* @param {Number} [options.top] Cropping top offset. Introduced in v1.2.14
Expand All @@ -1681,14 +1679,11 @@
* @param {Boolean} [options.enableRetinaScaling] Enable retina scaling for clone image. Introduce in 1.6.4
* @param {Boolean} [options.withoutTransform] Remove current object transform ( no scale , no angle, no flip, no skew ). Introduced in 2.3.4
* @param {Boolean} [options.withoutShadow] Remove current object shadow. Introduced in 2.4.2
* @return {fabric.Object} thisArg
* @return {fabric.Image} Object cloned as image.
*/
cloneAsImage: function(callback, options) {
cloneAsImage: function(options) {
var canvasEl = this.toCanvasElement(options);
if (callback) {
callback(new fabric.Image(canvasEl));
}
return this;
return new fabric.Image(canvasEl);
},

/**
Expand Down Expand Up @@ -1972,16 +1967,16 @@
* @type string[]
*/

fabric.Object._fromObject = function(className, object, extraParam) {
var klass = fabric[className], serializedObject = clone(object, true);
fabric.Object._fromObject = function(klass, object, extraParam) {
var serializedObject = clone(object, true);
return fabric.util.enlivenObjectEnlivables(serializedObject).then(function(enlivedMap) {
var newObject = Object.assign(object, enlivedMap);
return extraParam ? new klass(object[extraParam], newObject) : new klass(newObject);
});
};

fabric.Object.fromObject = function(object) {
return fabric.Object._fromObject('Object', object);
return fabric.Object._fromObject(fabric.Object, object);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/path.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
* @returns {Promise<fabric.Path>}
*/
fabric.Path.fromObject = function(object) {
return fabric.Object._fromObject('Path', object, 'path');
return fabric.Object._fromObject(fabric.Path, object, 'path');
};

/* _FROM_SVG_START_ */
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/polygon.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
* @returns {Promise<fabric.Polygon>}
*/
fabric.Polygon.fromObject = function(object) {
return fabric.Object._fromObject('Polygon', object, 'points');
return fabric.Object._fromObject(fabric.Polygon, object, 'points');
};

})(typeof exports !== 'undefined' ? exports : this);
2 changes: 1 addition & 1 deletion src/shapes/polyline.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
* @returns {Promise<fabric.Polyline>}
*/
fabric.Polyline.fromObject = function(object) {
return fabric.Object._fromObject('Polyline', object, 'points');
return fabric.Object._fromObject(fabric.Polyline, object, 'points');
};

})(typeof exports !== 'undefined' ? exports : this);
2 changes: 1 addition & 1 deletion src/shapes/rect.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
* @returns {Promise<fabric.Rect>}
*/
fabric.Rect.fromObject = function(object) {
return fabric.Object._fromObject('Rect', object);
return fabric.Object._fromObject(fabric.Rect, object);
};

})(typeof exports !== 'undefined' ? exports : this);
2 changes: 1 addition & 1 deletion src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@
* @returns {Promise<fabric.Text>}
*/
fabric.Text.fromObject = function(object) {
return fabric.Object._fromObject('Text', object, 'text');
return fabric.Object._fromObject(fabric.Text, object, 'text');
};

fabric.Text.genericFonts = ['sans-serif', 'serif', 'cursive', 'fantasy', 'monospace'];
Expand Down
3 changes: 1 addition & 2 deletions src/shapes/textbox.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,9 @@
* @static
* @memberOf fabric.Textbox
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Textbox instance is created
* @returns {Promise<fabric.Textbox>}
*/
fabric.Textbox.fromObject = function(object) {
return fabric.Object._fromObject('Textbox', object, 'text');
return fabric.Object._fromObject(fabric.Textbox, object, 'text');
};
})(typeof exports !== 'undefined' ? exports : this);
2 changes: 1 addition & 1 deletion src/shapes/triangle.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
* @returns {Promise<fabric.Triangle>}
*/
fabric.Triangle.fromObject = function(object) {
return fabric.Object._fromObject('Triangle', object);
return fabric.Object._fromObject(fabric.Triangle, object);
};

})(typeof exports !== 'undefined' ? exports : this);
1 change: 1 addition & 0 deletions src/util/dom_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* Cross-browser abstraction for sending XMLHttpRequest
* @memberOf fabric.util
* @deprecated this has to go away, we can use a modern browser method to do the same.
* @param {String} url URL to send XMLHttpRequest to
* @param {Object} [options] Options object
* @param {String} [options.method="GET"]
Expand Down
26 changes: 0 additions & 26 deletions src/util/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,32 +485,6 @@
});
},

/**
* Attaches SVG image with data: URL to the dom
* @memberOf fabric.util
* @param {Object} img Image object with data:image/svg src
* @param {Function} callback Callback; invoked with loaded image
* @return {Object} DOM element (div containing the SVG image)
*/
loadImageInDom: function(img, onLoadCallback) {
var div = fabric.document.createElement('div');
div.style.width = div.style.height = '1px';
div.style.left = div.style.top = '-100%';
div.style.position = 'absolute';
div.appendChild(img);
fabric.document.querySelector('body').appendChild(div);
/**
* Wrap in function to:
* 1. Call existing callback
* 2. Cleanup DOM
*/
img.onload = function () {
onLoadCallback();
div.parentNode.removeChild(div);
div = null;
};
},

/**
* Creates corresponding fabric instances from their object representations
* @static
Expand Down
24 changes: 9 additions & 15 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,28 +376,22 @@
});

QUnit.test('cloneAsImage', function(assert) {
var done = assert.async();
var cObj = new fabric.Rect({ width: 100, height: 100, fill: 'red', strokeWidth: 0 });
assert.ok(typeof cObj.cloneAsImage === 'function');
cObj.cloneAsImage(function(image) {
assert.ok(image);
assert.ok(image instanceof fabric.Image);
assert.equal(image.width, 100, 'the image has same dimension of object');
done();
});
var image = cObj.cloneAsImage();
assert.ok(image);
assert.ok(image instanceof fabric.Image);
assert.equal(image.width, 100, 'the image has same dimension of object');
});

QUnit.test('cloneAsImage with retina scaling enabled', function(assert) {
var done = assert.async();
var cObj = new fabric.Rect({ width: 100, height: 100, fill: 'red', strokeWidth: 0 });
fabric.devicePixelRatio = 2;
cObj.cloneAsImage(function(image) {
assert.ok(image);
assert.ok(image instanceof fabric.Image);
assert.equal(image.width, 200, 'the image has been scaled by retina');
fabric.devicePixelRatio = 1;
done();
}, { enableRetinaScaling: true });
var image = cObj.cloneAsImage({ enableRetinaScaling: true });
assert.ok(image);
assert.ok(image instanceof fabric.Image);
assert.equal(image.width, 200, 'the image has been scaled by retina');
fabric.devicePixelRatio = 1;
});

QUnit.test('toCanvasElement', function(assert) {
Expand Down

0 comments on commit 9f5ef64

Please sign in to comment.