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
@elyngved
Copy link

Thanks for the patch! I see no scrolling at all during canvas canvas conversion. However it seems that the more the browser is scrolled down the page, there is like a 'padding' on the top of the canvas element with nothing there. The rendering is shifted downward. The canvas size is what it should be, but that means the bottom of the rendering is cut off. I'm using Chrome 31 on OS X. Have you run into this problem?

@chrisinajar
Copy link

Great patch. I kind of can't believe how well it works. The diff points have changed though, I had to insert it here:

      Util.log('Done parsing, moving to Render.');

      cb({
        backgroundColor: background,
        stack: stack
      });

      window.scroll(left, top); // this

    });
  }

@andrewsxx
Copy link

Doesn't work if the object is out screen, it saves only the visible part. Do u have any fix for that issue?

@derekm
Copy link

derekm commented Aug 28, 2015

I still get the IndexSizeError exception due to a negative bounds.top value.

@fawadahmad
Copy link

@elyngved, Im facing the same. No page scrolling in html to canvas conversion but, if the page is scrolled down from where it was loaded, it adds a padding above the content and leaves some of the bottom content. Any solution for that?

@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