Home  >  Q&A  >  body text

html5 - Pictures generated by html2canvas, checkbox and radio selected items cannot be screenshotted.

Generate a picture through html2canvas, and find that the checkbox selected item does not take a screenshot successfully.

<!DOCTYPE html>
<html>
<head>
    <title>
        html2canvas
    </title>
</head>
<body id="myPage"  onload="createImage()">
    <p style="height: 100px;width: 100px;background-color: blue">
        我是p
        性别:
        <input type="checkbox" name="sex">男
        <input type="checkbox" name="sex">女
    </p>
</body>
<script type="text/javascript" src="html2canvas.js"></script>
<script type="text/javascript">
    function createImage(){
        html2canvas(document.getElementById('myPage'),{
            allowTaint:true,
            height: 500
        }).then(function(canvas) {
           console.log(canvas);
           var aTag = document.createElement("a");
           aTag.innerHTML = "This is a test";
           aTag.setAttribute("style", "position:absolute; top:50%; z-index:999");
           aTag.setAttribute("href", canvas.toDataURL());
           aTag.setAttribute("download", "myPic.png");
           document.body.appendChild(aTag);
        });
    }
</script>
</html>

Page and production pictures:

我想大声告诉你我想大声告诉你2714 days ago1105

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:34:24

    It can be used, because the image is generated as soon as you enter the page, and subsequent clicks will have no effect, so you can do it this way.

    <!DOCTYPE html>
    <html>
    <head>
    <title>
        html2canvas
    </title>
    </head>
    <body id="myPage">
    <p style="height: 100px;width: 100px;background-color: blue">
        我是p
        性别:
        <input type="checkbox" onchange="createImage()" name="sex">男
        <input type="checkbox" onchange="createImage()" name="sex">女
    </p>
    
    </body>
    <script type="text/javascript" src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.min.js"></script>
    <script type="text/javascript">
    var aTag = document.createElement("a");
    aTag.innerHTML = "This is a test";
    aTag.setAttribute("style", "position:absolute; top:50%; z-index:999");
    document.body.appendChild(aTag);
    function createImage(){
        html2canvas(document.getElementById('myPage'),{
            allowTaint:true,
            height: 500
        }).then(function(canvas) {
           console.log(canvas);
           aTag.setAttribute("href", canvas.toDataURL());
           aTag.setAttribute("download", "myPic.png");   
        });
    }
    </script>
    </html>

    reply
    0
  • Cancelreply