Skip to content

Commit 3b49cba

Browse files
committed
Fix rendering of content when window is scrolled
1 parent a4aa0c6 commit 3b49cba

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

dist/html2canvas.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ function renderWindow(node, container, options, windowWidth, windowHeight) {
999999
} else if (node === clonedWindow.document.body || node === clonedWindow.document.documentElement || options.canvas != null) {
10001000
canvas = renderer.canvas;
10011001
} else {
1002-
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: clonedWindow.pageXOffset, y: clonedWindow.pageYOffset});
1002+
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});
10031003
}
10041004

10051005
cleanupContainer(container, options);
@@ -1022,9 +1022,11 @@ function crop(canvas, bounds) {
10221022
var y2 = Math.min(canvas.height, Math.max(1, bounds.top + bounds.height));
10231023
croppedCanvas.width = bounds.width;
10241024
croppedCanvas.height = bounds.height;
1025-
log("Cropping canvas at:", "left:", bounds.left, "top:", bounds.top, "width:", (x2-x1), "height:", (y2-y1));
1026-
log("Resulting crop with width", bounds.width, "and height", bounds.height, " with x", x1, "and y", y1);
1027-
croppedCanvas.getContext("2d").drawImage(canvas, x1, y1, x2-x1, y2-y1, bounds.x, bounds.y, x2-x1, y2-y1);
1025+
var width = x2-x1;
1026+
var height = y2-y1;
1027+
log("Cropping canvas at:", "left:", bounds.left, "top:", bounds.top, "width:", width, "height:", height);
1028+
log("Resulting crop with width", bounds.width, "and height", bounds.height, "with x", x1, "and y", y1);
1029+
croppedCanvas.getContext("2d").drawImage(canvas, x1, y1, width, height, bounds.x, bounds.y, width, height);
10281030
return croppedCanvas;
10291031
}
10301032

dist/html2canvas.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function renderWindow(node, container, options, windowWidth, windowHeight) {
100100
} else if (node === clonedWindow.document.body || node === clonedWindow.document.documentElement || options.canvas != null) {
101101
canvas = renderer.canvas;
102102
} else {
103-
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: clonedWindow.pageXOffset, y: clonedWindow.pageYOffset});
103+
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});
104104
}
105105

106106
cleanupContainer(container, options);
@@ -123,9 +123,11 @@ function crop(canvas, bounds) {
123123
var y2 = Math.min(canvas.height, Math.max(1, bounds.top + bounds.height));
124124
croppedCanvas.width = bounds.width;
125125
croppedCanvas.height = bounds.height;
126-
log("Cropping canvas at:", "left:", bounds.left, "top:", bounds.top, "width:", (x2-x1), "height:", (y2-y1));
127-
log("Resulting crop with width", bounds.width, "and height", bounds.height, " with x", x1, "and y", y1);
128-
croppedCanvas.getContext("2d").drawImage(canvas, x1, y1, x2-x1, y2-y1, bounds.x, bounds.y, x2-x1, y2-y1);
126+
var width = x2-x1;
127+
var height = y2-y1;
128+
log("Cropping canvas at:", "left:", bounds.left, "top:", bounds.top, "width:", width, "height:", height);
129+
log("Resulting crop with width", bounds.width, "and height", bounds.height, "with x", x1, "and y", y1);
130+
croppedCanvas.getContext("2d").drawImage(canvas, x1, y1, width, height, bounds.x, bounds.y, width, height);
129131
return croppedCanvas;
130132
}
131133

tests/mocha/scrolling.html

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
<div style="height: 650px; background: green"></div>
2020
</div>
2121
</div>
22-
<div style="height: 2200px;"></div>
23-
<div style="height: 500px;background: green;"><a name="content">content</a></div>
22+
<div style="height: 2200px"></div>
23+
<div style="height: 500px;background: green;" id="bottom-content"><a name="content">&nbsp;</a></div>
2424
<script>
2525
describe("Scrolling", function() {
2626
it("with random scroll", function (done) {
@@ -58,7 +58,6 @@
5858
setTimeout(function() {
5959
html2canvas(document.querySelector("#scroll-render")).then(function (canvas) {
6060
expect($("#scrollable").scrollTop()).to.equal(500);
61-
document.body.appendChild(canvas);
6261
expect(canvas.width).to.equal(200);
6362
expect(canvas.height).to.equal(200);
6463
validCanvasPixels(canvas);
@@ -68,6 +67,21 @@
6867
});
6968
}, 0);
7069
});
70+
71+
72+
it("with window scroll", function (done) {
73+
$(window).scrollTop(500);
74+
setTimeout(function() {
75+
console.log(document.querySelector("#bottom-content").getBoundingClientRect().top);
76+
html2canvas(document.querySelector("#bottom-content")).then(function (canvas) {
77+
expect($(window).scrollTop()).to.equal(500);
78+
validCanvasPixels(canvas);
79+
done();
80+
}).catch(function (error) {
81+
done(error);
82+
});
83+
}, 0);
84+
});
7185
});
7286

7387
function validCanvasPixels(canvas) {

0 commit comments

Comments
 (0)