Skip to content

Commit b6ccf47

Browse files
Fixed Retina support bugs
1 parent 3747e90 commit b6ccf47

File tree

5 files changed

+216
-206
lines changed

5 files changed

+216
-206
lines changed

demo/js/vendor/html2canvas.js

+95-88
Original file line numberDiff line numberDiff line change
@@ -1635,8 +1635,6 @@ function cloneNode(node, javascriptEnabled) {
16351635
var child = node.firstChild;
16361636
while(child) {
16371637
if (javascriptEnabled === true || child.nodeType !== 1 || child.nodeName !== 'SCRIPT') {
1638-
console.log(clone);
1639-
16401638
clone.appendChild(cloneNode(child, javascriptEnabled));
16411639
}
16421640
child = child.nextSibling;
@@ -4027,13 +4025,14 @@ module.exports = Renderer;
40274025
},{"./log":15}],23:[function(require,module,exports){
40284026
var Renderer = require('../renderer');
40294027
var LinearGradientContainer = require('../lineargradientcontainer');
4028+
var Utils = require('../utils');
40304029
var log = require('../log');
40314030

40324031
function CanvasRenderer(width, height) {
4033-
this.ratio = window.devicePixelRatio;
4032+
this.ratio = Utils.getDeviceRatio();
40344033

4035-
width = this.applyRatio(width);
4036-
height = this.applyRatio(height);
4034+
width = Utils.applyRatio(width);
4035+
height = Utils.applyRatio(height);
40374036

40384037
Renderer.apply(this, arguments);
40394038
this.canvas = this.options.canvas || this.document.createElement("canvas");
@@ -4057,62 +4056,16 @@ function CanvasRenderer(width, height) {
40574056

40584057
CanvasRenderer.prototype = Object.create(Renderer.prototype);
40594058

4060-
CanvasRenderer.prototype.applyRatio = function(value) {
4061-
return value * this.ratio;
4062-
}
4063-
4064-
CanvasRenderer.prototype.applyRatioToBounds = function(bounds) {
4065-
bounds.width = bounds.width * this.ratio;
4066-
bounds.top = bounds.top * this.ratio;
4067-
4068-
//In case this is a size
4069-
try {
4070-
bounds.left = bounds.left * this.ratio;
4071-
bounds.height = bounds.height * this.ratio;
4072-
} catch (e) {
4073-
4074-
}
4075-
4076-
return bounds;
4077-
}
4078-
4079-
CanvasRenderer.prototype.applyRatioToPosition = function(position) {
4080-
position.left = position.left * this.ratio;
4081-
position.height = position.height * this.ratio;
4082-
4083-
return bounds;
4084-
}
4085-
4086-
CanvasRenderer.prototype.applyRatioToShape = function(shape) {
4087-
for (var i = 0; i < shape.length; i++) {
4088-
if (shape[i] instanceof Array) {
4089-
for (var k = 1; k < shape[i].length; k++) {
4090-
shape[i][k] = this.applyRatio(shape[i][k]);
4091-
}
4092-
}
4093-
}
4094-
return shape;
4095-
}
4096-
4097-
CanvasRenderer.prototype.applyRatioToFontSize = function(fontSize) {
4098-
var numericPart = parseFloat(fontSize) * this.ratio;
4099-
var stringPart = fontSize.replace(/[0-9]/g, '');
4100-
4101-
fontSize = numericPart + stringPart;
4102-
4103-
return fontSize;
4104-
}
4105-
41064059
CanvasRenderer.prototype.setFillStyle = function(fillStyle) {
41074060
this.ctx.fillStyle = typeof(fillStyle) === "object" && !!fillStyle.isColor ? fillStyle.toString() : fillStyle;
41084061
return this.ctx;
41094062
};
41104063

41114064
CanvasRenderer.prototype.rectangle = function(left, top, width, height, color) {
4112-
left = this.applyRatio(left);
4113-
top = this.applyRatio(top);
4114-
width = this.applyRatio(width);
4115-
height = this.applyRatio(height);
4065+
left = Utils.applyRatio(left);
4066+
top = Utils.applyRatio(top);
4067+
width = Utils.applyRatio(width);
4068+
height = Utils.applyRatio(height);
41164069

41174070
this.setFillStyle(color).fillRect(left, top, width, height);
41184071
};
@@ -4126,17 +4079,17 @@ CanvasRenderer.prototype.circle = function(left, top, size, color) {
41264079
};
41274080

41284081
CanvasRenderer.prototype.circleStroke = function(left, top, size, color, stroke, strokeColor) {
4129-
left = this.applyRatio(left);
4130-
top = this.applyRatio(top);
4131-
size = this.applyRatio(size);
4082+
left = Utils.applyRatio(left);
4083+
top = Utils.applyRatio(top);
4084+
size = Utils.applyRatio(size);
41324085

41334086
this.circle(left, top, size, color);
41344087
this.ctx.strokeStyle = strokeColor.toString();
41354088
this.ctx.stroke();
41364089
};
41374090

41384091
CanvasRenderer.prototype.drawShape = function(shape, color) {
4139-
shape = this.applyRatioToShape(shape);
4092+
shape = Utils.applyRatioToShape(shape);
41404093

41414094
this.shape(shape);
41424095
this.setFillStyle(color).fill();
@@ -4158,14 +4111,16 @@ CanvasRenderer.prototype.taints = function(imageContainer) {
41584111
};
41594112

41604113
CanvasRenderer.prototype.drawImage = function(imageContainer, sx, sy, sw, sh, dx, dy, dw, dh) {
4161-
sx = this.applyRatio(sx);
4162-
sy = this.applyRatio(sy);
4163-
//sw = this.applyRatio(sw);
4164-
//sh = this.applyRatio(sh);
4165-
dx = this.applyRatio(dx);
4166-
dy = this.applyRatio(dy);
4167-
dw = this.applyRatio(dw);
4168-
dh = this.applyRatio(dh);
4114+
//Do not scale source coordinates
4115+
//sx = Utils.applyRatio(sx);
4116+
//sy = Utils.applyRatio(sy);
4117+
//sw = Utils.applyRatio(sw);
4118+
//sh = Utils.applyRatio(sh);
4119+
4120+
dx = Utils.applyRatio(dx);
4121+
dy = Utils.applyRatio(dy);
4122+
dw = Utils.applyRatio(dw);
4123+
dh = Utils.applyRatio(dh);
41694124

41704125
if (!this.taints(imageContainer) || this.options.allowTaint) {
41714126
this.ctx.drawImage(imageContainer.image, sx, sy, sw, sh, dx, dy, dw, dh);
@@ -4175,8 +4130,8 @@ CanvasRenderer.prototype.drawImage = function(imageContainer, sx, sy, sw, sh, dx
41754130
CanvasRenderer.prototype.clip = function(shapes, callback, context) {
41764131
this.ctx.save();
41774132
shapes.filter(hasEntries).forEach(function(shape) {
4178-
shape = this.applyRatioToShape(shape);
4179-
this.shape(shape).clip();
4133+
shape = Utils.applyRatioToShape(shape);
4134+
this.shape(shape)//.clip();
41804135
}, this);
41814136
callback.call(context);
41824137
this.ctx.restore();
@@ -4196,13 +4151,13 @@ CanvasRenderer.prototype.shape = function(shape) {
41964151
};
41974152

41984153
CanvasRenderer.prototype.font = function(color, style, variant, weight, size, family) {
4199-
size = this.applyRatioToFontSize(size);
4154+
size = Utils.applyRatioToFontSize(size);
42004155
this.setFillStyle(color).font = [style, variant, weight, size, family].join(" ").split(",")[0];
42014156
};
42024157

42034158
CanvasRenderer.prototype.fontShadow = function(color, offsetX, offsetY, blur) {
4204-
offsetX = this.applyRatio(offsetX);
4205-
offsetY = this.applyRatio(offsetY);
4159+
offsetX = Utils.applyRatio(offsetX);
4160+
offsetY = Utils.applyRatio(offsetY);
42064161

42074162
this.setVariable("shadowColor", color.toString())
42084163
.setVariable("shadowOffsetY", offsetX)
@@ -4234,20 +4189,20 @@ CanvasRenderer.prototype.setVariable = function(property, value) {
42344189
};
42354190

42364191
CanvasRenderer.prototype.text = function(text, left, bottom) {
4237-
left = this.applyRatio(left);
4238-
bottom = this.applyRatio(bottom);
4192+
left = Utils.applyRatio(left);
4193+
bottom = Utils.applyRatio(bottom);
42394194

42404195
this.ctx.fillText(text, left, bottom);
42414196
};
42424197

42434198
CanvasRenderer.prototype.backgroundRepeatShape = function(imageContainer, backgroundPosition, size, bounds, left, top, width, height, borderData) {
42444199
debugger;
4245-
size = this.applyRatio(size);
4246-
bounds = this.applyRatioToBounds(bounds);
4247-
left = this.applyRatio(left);
4248-
top = this.applyRatio(top);
4249-
width = this.applyRatio(width);
4250-
height = this.applyRatio(height);
4200+
size = Utils.applyRatio(size);
4201+
bounds = Utils.applyRatioToBounds(bounds);
4202+
left = Utils.applyRatio(left);
4203+
top = Utils.applyRatio(top);
4204+
width = Utils.applyRatio(width);
4205+
height = Utils.applyRatio(height);
42514206

42524207
var shape = [
42534208
["line", Math.round(left), Math.round(top)],
@@ -4262,11 +4217,11 @@ CanvasRenderer.prototype.backgroundRepeatShape = function(imageContainer, backgr
42624217

42634218
CanvasRenderer.prototype.renderBackgroundRepeat = function(imageContainer, backgroundPosition, size, bounds, borderLeft, borderTop) {
42644219
debugger;
4265-
bounds = this.applyRatioToBounds(bounds);
4266-
size = this.applyRatioToBounds(size);
4267-
backgroundPosition = this.applyRatioToBounds(backgroundPosition);
4268-
borderLeft = this.applyRatio(borderLeft);
4269-
borderTop = this.applyRatio(borderTop);
4220+
bounds = Utils.applyRatioToBounds(bounds);
4221+
size = Utils.applyRatioToBounds(size);
4222+
backgroundPosition = Utils.applyRatioToBounds(backgroundPosition);
4223+
borderLeft = Utils.applyRatio(borderLeft);
4224+
borderTop = Utils.applyRatio(borderTop);
42704225

42714226
var offsetX = Math.round(bounds.left + backgroundPosition.left + borderLeft), offsetY = Math.round(bounds.top + backgroundPosition.top + borderTop);
42724227
this.setFillStyle(this.ctx.createPattern(this.resizeImage(imageContainer, size), "repeat"));
@@ -4277,7 +4232,7 @@ CanvasRenderer.prototype.renderBackgroundRepeat = function(imageContainer, backg
42774232

42784233
CanvasRenderer.prototype.renderBackgroundGradient = function(gradientImage, bounds) {
42794234
debugger;
4280-
bounds = this.applyRatioToBounds(bounds);
4235+
bounds = Utils.applyRatioToBounds(bounds);
42814236

42824237
if (gradientImage instanceof LinearGradientContainer) {
42834238
var gradient = this.ctx.createLinearGradient(
@@ -4293,7 +4248,7 @@ CanvasRenderer.prototype.renderBackgroundGradient = function(gradientImage, boun
42934248
};
42944249

42954250
CanvasRenderer.prototype.resizeImage = function(imageContainer, size) {
4296-
size = this.applyRatioToBounds(size);
4251+
size = Utils.applyRatioToBounds(size);
42974252

42984253
var image = imageContainer.image;
42994254
if(image.width === size.width && image.height === size.height) {
@@ -4314,7 +4269,7 @@ function hasEntries(array) {
43144269

43154270
module.exports = CanvasRenderer;
43164271

4317-
},{"../lineargradientcontainer":14,"../log":15,"../renderer":22}],24:[function(require,module,exports){
4272+
},{"../lineargradientcontainer":14,"../log":15,"../renderer":22,"../utils":29}],24:[function(require,module,exports){
43184273
var NodeContainer = require('./nodecontainer');
43194274

43204275
function StackingContext(hasOwnStacking, opacity, element, parent) {
@@ -4676,6 +4631,58 @@ exports.parseBackgrounds = function(backgroundImage) {
46764631
return results;
46774632
};
46784633

4634+
exports.getDeviceRatio = function() {
4635+
return window.devicePixelRatio;
4636+
};
4637+
4638+
exports.applyRatio = function(value) {
4639+
return value * exports.getDeviceRatio();
4640+
}
4641+
4642+
exports.applyRatioToBounds = function(bounds) {
4643+
bounds.width = bounds.width * exports.getDeviceRatio();
4644+
bounds.top = bounds.top * exports.getDeviceRatio();
4645+
4646+
//In case this is a size
4647+
try {
4648+
bounds.left = bounds.left * exports.getDeviceRatio();
4649+
bounds.height = bounds.height * exports.getDeviceRatio();
4650+
} catch (e) {
4651+
4652+
}
4653+
4654+
return bounds;
4655+
}
4656+
4657+
exports.applyRatioToPosition = function(position) {
4658+
position.left = position.left * exports.getDeviceRatio();
4659+
position.height = position.height * exports.getDeviceRatio();
4660+
4661+
return bounds;
4662+
}
4663+
4664+
exports.applyRatioToShape = function(shape) {
4665+
for (var i = 0; i < shape.length; i++) {
4666+
if (shape[i] instanceof Array) {
4667+
for (var k = 1; k < shape[i].length; k++) {
4668+
shape[i][k] = this.applyRatio(shape[i][k]);
4669+
}
4670+
}
4671+
}
4672+
return shape;
4673+
}
4674+
4675+
exports.applyRatioToFontSize = function(fontSize) {
4676+
var numericPart = parseFloat(fontSize) * exports.getDeviceRatio();
4677+
var stringPart = fontSize.replace(/[0-9]/g, '');
4678+
4679+
fontSize = numericPart + stringPart;
4680+
4681+
return fontSize;
4682+
}
4683+
4684+
4685+
46794686
},{}],30:[function(require,module,exports){
46804687
var GradientContainer = require('./gradientcontainer');
46814688

0 commit comments

Comments
 (0)