1

Html2Canvas is not able to render multiple DIVs properly when

  1. The number of DIVs increases significantly, OR
  2. The size of each DIV increases significantly (then it fails with fewer number of DIVs)

I am using Html2Canvas Plugin to convert div (containing image) to canvas. I have implemented a recursive function to convert multiple images at a time (see code below).

JavaScript

//counter 
var div_number = 0;

    function image2canvas() {
        var img2canvasObj = {
            logging:true,
            onrendered: function (canvas) {
                canvas.id = "cvs" + div_number;
                $("#canvasid").append(canvas);
         //incrementing the counter
                div_number = div_number + 1;

                if (div_number <= 18) {
                    html2canvas($("#tempDivforpublishplan" + div_number), img2canvasObj);

                    }
                    if (div_number === 19) {
        <Some Ajax Call>
                    }
                }
            };
            html2canvas($("#Content_to_convert" + div_number), img2canvasObj);
        }

The HTML for multiple images (which I am converting to canvas) is as follows. Note that I am using images as background of div elements. Each of these elements have a base64 string appended to the image URL which is used to convert the image to canvas. HTML:

<div id="tempDivforpublishplan0" style='background: url("data:image/png;base64,iVBORw0KGgo…..") 0% 0% / 634px 2266.26px; width: 634px; height: 2266.26px; position: relative; cursor: auto;'> </div>
<div id="tempDivforpublishplan1" style='background: url("data:image/png;base64,iVBORw0KGgo…..") 0% 0% / 634px 2266.26px; width: 634px; height: 2266.26px; position: relative; cursor: auto;'> </div>
.
.
.
<div id="tempDivforpublishplan19" style='background: url("data:image/png;base64,iVBORw0KGgo…..") 0% 0% / 634px 2266.26px; width: 634px; height: 2266.26px; position: relative; cursor: auto;'> </div>

When image count is more, or the size and resolution of images is high, only few images are converted properly. Remaining images are either converted partially or not converted at all. Is there any workaround which can be used to fix this issue?

ToBe
  • 2,667
  • 1
  • 18
  • 30
lofty khanna
  • 111
  • 1
  • 4

2 Answers2

0

i have achieved this using callback and delay.... before calling your recursive function use delay and use callback as html2canvas is asynchronous call

  • Can you please provide me with particular example – lofty khanna Feb 05 '15 at 08:03
  • I applied the delay to if (div_number <= 18) { setTimeout(function(){ html2canvas($("#tempDivforpublishplan" + div_number), img2canvasObj); }, 3000); } But still the same issue Please help :) – lofty khanna Feb 05 '15 at 08:04
0

I think you are having this trouble because you reach CanvasRenderer height limit during rendering. Assuming all your divs your page height is more than 40000px. Html2Canvas renders your page completely and then crops your div from it according to your div's position. You can see how big your canvas object is if you take a look throw your console having 'logging: true' in html2canvas options. Here is some more info about this subject: Maximum size of a <canvas> element

Community
  • 1
  • 1