单击按钮拍摄 window 的屏幕截图
Take screen shot of window on click of a button
基本上,我有一个简单的 HTML 站点 - 我只想拥有一个 html 文件,上面有所有内容。在网站上,应该有一个按钮,点击它会截取页面的屏幕截图,并将其上传到域中的 /image。我尝试使用 HTML2Canvas,但在大多数文章中它说要下载东西并制作新文件等。如何在一个 HTML 文件上使用 HTML2Canvas?
<!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>
我发现另一个 post here 解释得很好,但本质上你需要 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>
元素的背景(即页面背景)变成页面的屏幕截图。
基本上,我有一个简单的 HTML 站点 - 我只想拥有一个 html 文件,上面有所有内容。在网站上,应该有一个按钮,点击它会截取页面的屏幕截图,并将其上传到域中的 /image。我尝试使用 HTML2Canvas,但在大多数文章中它说要下载东西并制作新文件等。如何在一个 HTML 文件上使用 HTML2Canvas?
<!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>
我发现另一个 post here 解释得很好,但本质上你需要 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>
元素的背景(即页面背景)变成页面的屏幕截图。