-1

I have this piece of code in .aspx that should print the selected part of my web function.

           <div id="test" class="body-content animated fadeIn">
                <a href="javascript:getScreenshot()"> Get Sreenshot </a>
                <script>
                    function getScreenshot() {
                        html2canvas(divprint, {
                            onrendered: function(canvas) {
                                var canvasImg = canvas.toDataURL("image/jpg");
                                $('#test').html('<img src="'+canvasImg+'" alt="">');
                            }
                        });
                        var printContent = document.getElementById("test");
                        var printWindow = window.open("", "","left=50,top=50");
                        printWindow.document.write(printContent.innerHTML);
                        printWindow.document.write("<script src=\'http://code.jquery.com/jquery-1.10.1.min.js\'><\/script>");
                        printWindow.document.write("<script>$(window).load(function(){ print(); close(); });<\/script>");
                        printWindow.document.close();
                    }
                </script>
                ...

But clicking the the text "Get Sreenshot" nothing happens. Do you understand why? When I remove

                       html2canvas(divprint, {
                            onrendered: function(canvas) {
                                var canvasImg = canvas.toDataURL("image/jpg");
                                $('#test').html('<img src="'+canvasImg+'" alt="">');
                            }
                        });

it works more or less well

1 Answers1

0

The latest version of html2canvas uses promises rather than the 'onrendered` callback.
eg.

html2canvas(document.querySelector('#my-thing')).then(function(canvas) {
  // do something with canvas here.
  console.log(canvas);
});
2pha
  • 9,798
  • 2
  • 29
  • 43
  • Works fine, thanks! Do you know how to fix the problem of color change and zoom? –  Aug 16 '18 at 09:20
  • What problem with color change and zoom? I see nothing in your question about that. – 2pha Aug 16 '18 at 22:26
  • See the new question here: https://stackoverflow.com/questions/51878872/html2canvas-color-change-and-zoom –  Aug 17 '18 at 09:49