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

Proper fix for enableRetinaScaling on dataUrl and tests #4705

Merged
merged 1 commit into from
Feb 10, 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
2 changes: 1 addition & 1 deletion src/mixins/canvas_dataurl_exporter.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

var format = options.format || 'png',
quality = options.quality || 1,
multiplier = options.multiplier || (options.enableRetinaScaling ? 1 : 1 / this.getRetinaScaling()),
multiplier = (options.multiplier || 1) * (options.enableRetinaScaling ? 1 : 1 / this.getRetinaScaling()),
cropping = {
left: options.left || 0,
top: options.top || 0,
Expand Down
77 changes: 76 additions & 1 deletion test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
}
});

QUnit.test('toDataURL with enableRetinaScaling: true', function(assert) {
QUnit.test('toDataURL with enableRetinaScaling: true and no multiplier', function(assert) {
var done = assert.async();
fabric.devicePixelRatio = 2;
var c = new fabric.StaticCanvas(null, { enableRetinaScaling: true, width: 10, height: 10 });
Expand All @@ -485,6 +485,81 @@
img.src = dataUrl;
});

QUnit.test('toDataURL with enableRetinaScaling: true and multiplier = 1', function(assert) {
var done = assert.async();
fabric.devicePixelRatio = 2;
var c = new fabric.StaticCanvas(null, { enableRetinaScaling: true, width: 10, height: 10 });
var dataUrl = c.toDataURL({ enableRetinaScaling: true, multiplier: 1 });
var img = fabric.document.createElement('img');
img.onload = function() {
assert.equal(img.width, c.width * fabric.devicePixelRatio, 'output width is bigger');
assert.equal(img.height, c.height * fabric.devicePixelRatio, 'output height is bigger');
fabric.devicePixelRatio = 1;
done();
};
img.src = dataUrl;
});

QUnit.test('toDataURL with enableRetinaScaling: true and multiplier = 3', function(assert) {
var done = assert.async();
fabric.devicePixelRatio = 2;
var c = new fabric.StaticCanvas(null, { enableRetinaScaling: true, width: 10, height: 10 });
var dataUrl = c.toDataURL({ enableRetinaScaling: true, multiplier: 3 });
var img = fabric.document.createElement('img');
img.onload = function() {
assert.equal(img.width, c.width * fabric.devicePixelRatio * 3, 'output width is bigger by 6');
assert.equal(img.height, c.height * fabric.devicePixelRatio * 3, 'output height is bigger by 6');
fabric.devicePixelRatio = 1;
done();
};
img.src = dataUrl;
});

QUnit.test('toDataURL with enableRetinaScaling: false and no multiplier', function(assert) {
var done = assert.async();
fabric.devicePixelRatio = 2;
var c = new fabric.StaticCanvas(null, { enableRetinaScaling: true, width: 10, height: 10 });
var dataUrl = c.toDataURL({ enableRetinaScaling: false });
var img = fabric.document.createElement('img');
img.onload = function() {
assert.equal(img.width, c.width, 'output width is not bigger');
assert.equal(img.height, c.height, 'output height is not bigger');
fabric.devicePixelRatio = 1;
done();
};
img.src = dataUrl;
});

QUnit.test('toDataURL with enableRetinaScaling: false and multiplier = 1', function(assert) {
var done = assert.async();
fabric.devicePixelRatio = 2;
var c = new fabric.StaticCanvas(null, { enableRetinaScaling: true, width: 10, height: 10 });
var dataUrl = c.toDataURL({ enableRetinaScaling: false, multiplier: 1 });
var img = fabric.document.createElement('img');
img.onload = function() {
assert.equal(img.width, c.width, 'output width is not bigger');
assert.equal(img.height, c.height, 'output height is not bigger');
fabric.devicePixelRatio = 1;
done();
};
img.src = dataUrl;
});

QUnit.test('toDataURL with enableRetinaScaling: false and multiplier = 3', function(assert) {
var done = assert.async();
fabric.devicePixelRatio = 2;
var c = new fabric.StaticCanvas(null, { enableRetinaScaling: true, width: 10, height: 10 });
var dataUrl = c.toDataURL({ enableRetinaScaling: false, multiplier: 3 });
var img = fabric.document.createElement('img');
img.onload = function() {
assert.equal(img.width, c.width * 3, 'output width is bigger by 3');
assert.equal(img.height, c.height * 3, 'output height is bigger by 3');
fabric.devicePixelRatio = 1;
done();
};
img.src = dataUrl;
});

QUnit.test('toDataURL with enableRetinaScaling: false', function(assert) {
var done = assert.async();
fabric.devicePixelRatio = 2;
Expand Down