Skip to content

Commit ebd40ae

Browse files
author
Aleksander Burzec
committed
Merge branch '69-blurry-png-and-jpeg' of https://github.com/iqdoq-dfischer/dom-to-image into iqdoq-dfischer-69-blurry-png-and-jpeg
# Conflicts: # src/dom-to-image.js
2 parents a740bb8 + 9864791 commit ebd40ae

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/dom-to-image.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
* @param {Number} options.width - width to be applied to node before rendering.
4545
* @param {Number} options.height - height to be applied to node before rendering.
4646
* @param {Object} options.style - an object whose properties to be copied to node's style before rendering.
47-
* @param {Number} options.quality - a Number between 0 and 1 indicating image quality (applicable to JPEG only), defaults to 1.0.
47+
* @param {Number} options.quality - a Number between 0 and 1 indicating image quality (applicable to JPEG only),
48+
defaults to 1.0.
49+
* @param {Number} options.scale - scale factor for image quality rendering (to unblurry JPEG and PNG output),
50+
default = unset = no scaling applied = 1.0.
4851
* @param {String} options.imagePlaceholder - dataURL to use as a placeholder for failed images, default behaviour is to fail fast on images we can't fetch
4952
* @param {Boolean} options.cacheBust - set to true to cache bust by appending the time to the request url
5053
* @return {Promise} - A promise that is fulfilled with a SVG image data URL
@@ -155,15 +158,23 @@
155158
.then(function (image) {
156159
var canvas = newCanvas(domNode);
157160
var ctx = canvas.getContext('2d');
158-
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
161+
if (options.scale) {
162+
ctx.scale(options.scale, options.scale);
163+
}
159164
ctx.drawImage(image, 0, 0);
160165
return canvas;
161166
});
162167

163168
function newCanvas(domNode) {
164169
var canvas = document.createElement('canvas');
165-
canvas.width = options.width || window.devicePixelRatio * util.width(domNode);
166-
canvas.height = options.height || window.devicePixelRatio * util.height(domNode);
170+
if (options.scale) {
171+
canvas.width = options.width || options.scale * util.width(domNode);
172+
canvas.height = options.height || options.scale * util.height(domNode);
173+
}
174+
else {
175+
canvas.width = options.width || util.width(domNode);
176+
canvas.height = options.height || util.height(domNode);
177+
}
167178

168179
if (options.bgcolor) {
169180
var ctx = canvas.getContext('2d');

0 commit comments

Comments
 (0)