Search code examples
javascriptjqueryhtmlcsshtml2canvas

Take screen shot of window on click of a button


Essentially, I have this plain HTML site - I only want to have one html file, with everything on that. On the site, there should be a button, and on-click it takes a screen shot of the page, and uploads that to /image on the domain. I tried using HTML2Canvas, but in most articles it says to download stuff and make new files etc. How do I use HTML2Canvas all on one HTML File?

<!DOCTYPE html>
<html>
<!--CSS:-->
<style>
#mydiv {
    position: absolute;
    z-index: 9;
    text-align: center;

}

#mydivheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;
}
</style>

<!--HTML:-->
<body>
<a href="#" onclick="capture()">Click for screenshot!</a>
<div id="mydiv">
<img class="two" src=https://pbs.twimg.com/profile_images/786024596998909953/ubcBmeAM_400x400.jpg width="40%" height="40%">

</div>



<!--JavaScript:-->
<script>
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv"));

function dragElement(elmnt) {
	elmnt.style.left = 20 + "px";
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
</script>
<script>
function capture() {
    html2canvas($('body'),{
        onrendered: function (canvas) {                     
            var imgString = canvas.toDataURL("image/png");
            window.open(imgString);
        }                  
    }
});
---JUST COPPIED AND PASTED the HTML2canvas code, from this site: https://html2canvas.hertzen.com/dist/html2canvas.js

</script>


</body>
</html>



<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body, html {
    height: 100%;
    margin: 0;
}

.bg {
    /* The image used */
    background-image: url(https://rfclipart.com/image/big/af-4a-f2/neighbourhood-silhouettes-of-country-houses-Download-Royalty-free-Vector-File-EPS-69644.jpg);

    /* Full height */
    height: 100%; 
	width: 100%;
    /* Center and scale the image nicely */
    background-position: center;
    background-repeat: no-repeat;
    background-size: 100%;
}
</style>
</head>
<body>

<div class="bg"></div>

</body>


Solution

  • I found another post here that explains it well, but essentially you need jQuery, HTML2Canvas (you seem to have it already) and jQuery.plugin.html2canvas.js. Here's the code you should use:

    function capture() {
        html2canvas($('body'),{
            onrendered: function (canvas) {                     
                var imgString = canvas.toDataURL("image/png");
                window.open(imgString);
            }                  
        }
    });
    

    Then add this code:

    <a href="#" onclick="capture()">Click for screenshot!</a>
    

    This will, when you click on the button, make the <body> element's background (So, the page's background) into a screenshot of the page.