Skip to content

Commit 8b4ef00

Browse files
authored
Update html2canvas.js
niklasvh/html2canvas#1087
1 parent b9feeac commit 8b4ef00

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

plugins/html2canvas.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,15 @@ function renderWindow(node, container, options, windowWidth, windowHeight) {
998998
canvas = crop(renderer.canvas, {width: renderer.canvas.width, height: renderer.canvas.height, top: 0, left: 0, x: 0, y: 0});
999999
} else if (node === clonedWindow.document.body || node === clonedWindow.document.documentElement || options.canvas != null) {
10001000
canvas = renderer.canvas;
1001+
} else if (options.scale) {
1002+
var origBounds = {width: options.width != null ? options.width : bounds.width, height: options.height != null ? options.height : bounds.height, top: bounds.top, left: bounds.left, x: 0, y: 0};
1003+
var cropBounds = {};
1004+
for (var key in origBounds) {
1005+
if (origBounds.hasOwnProperty(key)) { cropBounds[key] = origBounds[key] * options.scale; }
1006+
}
1007+
canvas = crop(renderer.canvas, cropBounds);
1008+
canvas.style.width = origBounds.width + 'px';
1009+
canvas.style.height = origBounds.height + 'px';
10011010
} else {
10021011
canvas = crop(renderer.canvas, {width: options.width != null ? options.width : bounds.width, height: options.height != null ? options.height : bounds.height, top: bounds.top, left: bounds.left, x: 0, y: 0});
10031012
}
@@ -2944,11 +2953,22 @@ var log = _dereq_('../log');
29442953
function CanvasRenderer(width, height) {
29452954
Renderer.apply(this, arguments);
29462955
this.canvas = this.options.canvas || this.document.createElement("canvas");
2956+
this.ctx = this.canvas.getContext("2d");
29472957
if (!this.options.canvas) {
2948-
this.canvas.width = width;
2949-
this.canvas.height = height;
2958+
if (this.options.dpi) {
2959+
this.options.scale = this.options.dpi / 96; // 1 CSS inch = 96px.
2960+
}
2961+
if (this.options.scale) {
2962+
this.canvas.style.width = width + 'px';
2963+
this.canvas.style.height = height + 'px';
2964+
this.canvas.width = Math.floor(width * this.options.scale);
2965+
this.canvas.height = Math.floor(height * this.options.scale);
2966+
this.ctx.scale(this.options.scale, this.options.scale);
2967+
} else {
2968+
this.canvas.width = width;
2969+
this.canvas.height = height;
2970+
}
29502971
}
2951-
this.ctx = this.canvas.getContext("2d");
29522972
this.taintCtx = this.document.createElement("canvas").getContext("2d");
29532973
this.ctx.textBaseline = "bottom";
29542974
this.variables = {};
@@ -3080,9 +3100,13 @@ CanvasRenderer.prototype.backgroundRepeatShape = function(imageContainer, backgr
30803100
CanvasRenderer.prototype.renderBackgroundRepeat = function(imageContainer, backgroundPosition, size, bounds, borderLeft, borderTop) {
30813101
var offsetX = Math.round(bounds.left + backgroundPosition.left + borderLeft), offsetY = Math.round(bounds.top + backgroundPosition.top + borderTop);
30823102
this.setFillStyle(this.ctx.createPattern(this.resizeImage(imageContainer, size), "repeat"));
3103+
this.ctx.save();
30833104
this.ctx.translate(offsetX, offsetY);
3105+
if (this.options.scale) {
3106+
this.ctx.scale(1/this.options.scale, 1/this.options.scale);
3107+
}
30843108
this.ctx.fill();
3085-
this.ctx.translate(-offsetX, -offsetY);
3109+
this.ctx.restore();
30863110
};
30873111

30883112
CanvasRenderer.prototype.renderBackgroundGradient = function(gradientImage, bounds) {
@@ -3100,16 +3124,21 @@ CanvasRenderer.prototype.renderBackgroundGradient = function(gradientImage, boun
31003124
};
31013125

31023126
CanvasRenderer.prototype.resizeImage = function(imageContainer, size) {
3127+
var width = size.width, height = size.height;
3128+
if (this.options.scale) {
3129+
width *= this.options.scale;
3130+
height *= this.options.scale;
3131+
}
31033132
var image = imageContainer.image;
3104-
if(image.width === size.width && image.height === size.height) {
3133+
if(image.width === width && image.height === height) {
31053134
return image;
31063135
}
31073136

31083137
var ctx, canvas = document.createElement('canvas');
3109-
canvas.width = size.width;
3110-
canvas.height = size.height;
3138+
canvas.width = width;
3139+
canvas.height = height;
31113140
ctx = canvas.getContext("2d");
3112-
ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, size.width, size.height );
3141+
ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, width, height );
31133142
return canvas;
31143143
};
31153144

0 commit comments

Comments
 (0)