Skip to content

Commit

Permalink
Add enableRetinaScaling to cloneAsImage (#3147)
Browse files Browse the repository at this point in the history
* Add enableRetinaScaling to cloneAsImage
* added test
  • Loading branch information
asturur authored Aug 8, 2016
1 parent c362250 commit a2b4ef5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,12 @@
/**
* Creates an instance of fabric.Image out of an object
* @param {Function} callback callback, invoked with an instance as a first argument
* @param {Object} [options] for clone as image, passed to toDataURL
* @param {Boolean} [options.enableRetinaScaling] enable retina scaling for the cloned image
* @return {fabric.Object} thisArg
*/
cloneAsImage: function(callback) {
var dataUrl = this.toDataURL();
cloneAsImage: function(callback, options) {
var dataUrl = this.toDataURL(options);
fabric.util.loadImage(dataUrl, function(img) {
if (callback) {
callback(new fabric.Image(img));
Expand All @@ -1328,6 +1330,7 @@
* @param {Number} [options.top] Cropping top offset. Introduced in v1.2.14
* @param {Number} [options.width] Cropping width. Introduced in v1.2.14
* @param {Number} [options.height] Cropping height. Introduced in v1.2.14
* @param {Boolean} [options.enableRetina] Enable retina scaling for clone image. Introduce in 1.6.4
* @return {String} Returns a data: URL containing a representation of the object in the format specified by options.format
*/
toDataURL: function(options) {
Expand All @@ -1340,7 +1343,7 @@
el.height = boundingRect.height;

fabric.util.wrapElement(el, 'div');
var canvas = new fabric.StaticCanvas(el);
var canvas = new fabric.StaticCanvas(el, { enableRetinaScaling: options.enableRetinaScaling });

// to avoid common confusion https://github.com/kangax/fabric.js/issues/806
if (options.format === 'jpg') {
Expand Down
29 changes: 27 additions & 2 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ test('getBoundingRectWithStroke', function() {
});

asyncTest('cloneAsImage', function() {
var cObj = new fabric.Rect({ width: 100, height: 100, fill: 'red' });
var cObj = new fabric.Rect({ width: 100, height: 100, fill: 'red', strokeWidth: 0 });

ok(typeof cObj.cloneAsImage == 'function');

Expand All @@ -580,6 +580,7 @@ test('getBoundingRectWithStroke', function() {
setTimeout(function() {
ok(image);
ok(image instanceof fabric.Image);
equal(image.width, 100, 'the image has same dimension of object');
start();
}, 500);

Expand All @@ -589,6 +590,30 @@ test('getBoundingRectWithStroke', function() {
}
});

asyncTest('cloneAsImage with retina scaling enabled', function() {
var cObj = new fabric.Rect({ width: 100, height: 100, fill: 'red', strokeWidth: 0 });
fabric.devicePixelRatio = 2;
if (!fabric.Canvas.supports('toDataURL')) {
fabric.log('`toDataURL` is not supported by this environment; skipping `cloneAsImage` test (as it relies on `toDataURL`)');
start();
}
else {
var image;

setTimeout(function() {
ok(image);
ok(image instanceof fabric.Image);
equal(image.width, 200, 'the image has been scaled by retina');
fabric.devicePixelRatio = 1;
start();
}, 500);

cObj.cloneAsImage(function(i) {
image = i;
}, { enableRetinaScaling: true });
}
});

test('toDataURL', function() {
// var data =
// 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQA'+
Expand All @@ -601,7 +626,7 @@ test('getBoundingRectWithStroke', function() {
// 'JC0eQCGpM0DMCRtHsDjB5K06yueJFXJAAAAAElFTkSuQmCC';

var cObj = new fabric.Rect({
width: 100, height: 100, fill: 'red'
width: 100, height: 100, fill: 'red', strokeWidth: 0
});

ok(typeof cObj.toDataURL == 'function');
Expand Down

0 comments on commit a2b4ef5

Please sign in to comment.