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

Add enableRetinaScaling to cloneAsImage #3147

Merged
merged 4 commits into from
Aug 8, 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
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