Сделайте снимок экрана окна по нажатию кнопки - PullRequest
0 голосов
/ 29 августа 2018

По сути, у меня есть этот простой HTML-сайт - я хочу иметь только один html-файл со всем этим. На сайте должна быть кнопка, и при нажатии она делает снимок экрана страницы и загружает его в / изображение в домене. Я пытался использовать HTML2Canvas, но в большинстве статей говорится, что нужно загружать файлы, создавать новые файлы и т. Д. Как использовать HTML2Canvas в одном файле HTML?

<!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>

1 Ответ

0 голосов
/ 29 августа 2018

Я нашел еще один пост здесь , который хорошо это объясняет, но, по сути, вам нужны jQuery, HTML2Canvas (у вас, кажется, он уже есть) и jQuery.plugin.html2canvas.js. Вот код, который вы должны использовать:

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

Затем добавьте этот код:

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

Когда вы нажмете на кнопку, фон <body> (так, фон страницы) превратится в скриншот страницы.

...