Skip to content

Instantly share code, notes, and snippets.

@InfernoZeus
Last active November 3, 2017 06:53
Show Gist options
  • Save InfernoZeus/6010328 to your computer and use it in GitHub Desktop.
Save InfernoZeus/6010328 to your computer and use it in GitHub Desktop.
Patches for html2canvas to fix: - jump back to previous scroll position after parsing - indexsizeerror dom exception when element being rendered had size 0
diff --git a/content/scripts/html2canvas.js b/content/scripts/html2canvas.js
index 7ab27ed..52f3eab 100644
--- a/content/scripts/html2canvas.js
+++ b/content/scripts/html2canvas.js
@@ -2830,9 +2830,10 @@ _html2canvas.Renderer.Canvas = function(options) {
newCanvas = doc.createElement('canvas');
newCanvas.width = bounds.width;
newCanvas.height = bounds.height;
- ctx = newCanvas.getContext("2d");
-
- ctx.drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
+ if (newCanvas.width != 0 && newCanvas.height != 0) {
+ ctx = newCanvas.getContext("2d");
+ ctx.drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
+ }
canvas = null;
return newCanvas;
}
From 88d7a3e5dadf67e28605e6c9cc943dc72de175db Mon Sep 17 00:00:00 2001
From: Ben Fox-Moore <[email protected]>
Date: Tue, 16 Jul 2013 18:03:52 +0200
Subject: [PATCH] Patch html2canvas to jump back to previous scroll position
after parsing
---
content/scripts/html2canvas.js | 3 +++
1 file changed, 3 insertions(+)
diff --git a/content/scripts/html2canvas.js b/content/scripts/html2canvas.js
index 4689971..04820f8 100644
--- a/content/scripts/html2canvas.js
+++ b/content/scripts/html2canvas.js
@@ -1013,6 +1013,8 @@ function h2cRenderContext(width, height) {
};
}
_html2canvas.Parse = function (images, options) {
+ var top = window.pageYOffset || document.documentElement.scrollTop
+ var left = window.pageXOffset || document.documentElement.scrollLeft
window.scroll(0,0);
var element = (( options.elements === undefined ) ? document.body : options.elements[0]), // select body by default
@@ -2155,6 +2157,7 @@ _html2canvas.Parse = function (images, options) {
stack.backgroundColor = getCSS(document.documentElement, "backgroundColor");
body.removeChild(hidePseudoElements);
+ window.scroll(left, top);
return stack;
}
--
1.8.3.1
@tansautn
Copy link

tansautn commented Nov 3, 2017

After a brief review of how the 0.4 core code works. I realized that it is possible to capture the area above the scrollbar (hidden on screen view, but visible on document). By inserting the element's exact position when cliping element to newCanvas. (Using jQuery (element) .offset () can do this)

I have fork v0.4 and made a patch with the modifications from @InfernoZeus and added a new offset.

See the difference here: ultra-bugs/html2canvas@84284cc

Or use the built-in bower with the package name: z-html2canvas#0.4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment