create screenshot of webpage using html2canvas (unable to initialize properly)

后端 未结 5 2060
醉酒成梦
醉酒成梦 2020-12-01 06:03

I am attempting to use http://html2canvas.hertzen.com/ to take screenshots of my webpage. I am unable to initialize a canvas element using...

var canvas = $         


        
相关标签:
5条回答
  • 2020-12-01 06:21

    This is what worked for me.

    html2canvas(document.body, {
       onrendered: function(canvas) {
         var img = canvas.toDataURL()
         window.open(img);
      }
    });
    

    This created a new window for the screenshot.

    I only wanted a portion of my page in the screenshot, specifically a container div. So I did the following:

    html2canvas($('#myDiv'), {
       onrendered: function(canvas) {
         var img = canvas.toDataURL()
         window.open(img);
      }
    });
    

    For people looking up the same question, if the above options don't help, hopefully this will.

    0 讨论(0)
  • 2020-12-01 06:22

    You could also use the following:

    var html2obj = html2canvas($('body'));
    
    var queue  = html2obj.parse();
    var canvas = html2obj.render(queue);
    var img = canvas.toDataURL();
    
    window.open(img);
    
    0 讨论(0)
  • 2020-12-01 06:29

    You can use the following code to capture a screenshot and download the screenshot.

    html button creation

    <button class="btn btn-default btn-sm" style="margin:0px 0px -10px 970px; padding:2px 4px 1px 4px" onclick="genScreenshot()"><span class="glyphicon glyphicon-envelope"></span></button>
    <a id="test"></a>
    <div id="box1"></div>
    

    function definition

    <script type="text/javascript">                         
    function genScreenshot() {
    html2canvas(document.body, {
      onrendered: function(canvas) {
      $('#box1').html("");
    
      if (navigator.userAgent.indexOf("MSIE ") > 0 || 
                    navigator.userAgent.match(/Trident.*rv\:11\./)) 
            {
        var blob = canvas.msToBlob();
        window.navigator.msSaveBlob(blob,'Test file.png');
      }
      else {
        $('#test').attr('href', canvas.toDataURL("image/png"));
        $('#test').attr('download','screenshot.png');
        $('#test')[0].click();
      }
    
    
      }
    });
    }  
    </script>
    

    note: I have created a html button where I have called the function. test is an attribute and box1 is to get the canvas elements.

    0 讨论(0)
  • 2020-12-01 06:33

    You should use it this way:

    $('body').html2canvas();
    var queue = html2canvas.Parse();
    var canvas = html2canvas.Renderer(queue,{elements:{length:1}});
    var img = canvas.toDataURL();
    window.open(img);
    

    It took me few hours to figure it out, how to use it the right way. The {elements:{length:1}} is required, due to incomplete implementation of the plugin, otherwise you'll get an error.

    Good luck!

    0 讨论(0)
  • 2020-12-01 06:36

    To just get a part of the page you can use it this way:

    $('#map').html2canvas({
    onrendered: function( canvas ) {
      var img = canvas.toDataURL()
      window.open(img);
    }
    
    0 讨论(0)
提交回复
热议问题