基本上,我有这个简单的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>
答案 0 :(得分:1)
我发现了另一篇文章here对此进行了很好的解释,但实际上您需要jQuery,HTML2Canvas(您似乎已经拥有)和jQuery.plugin.html2canvas.js。这是您应该使用的代码:
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://basedata.firebaseio.com/");
FirebaseApp.DefaultInstance.SetEditorP12FileName("basedata-32fe7b000928.p12");
FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("basedata@appspot.gserviceaccount.com");
FirebaseApp.DefaultInstance.SetEditorP12Password("basedata");
FirebaseDatabase.DefaultInstance
.GetReference("users")
.GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
userName.text = "upps";
}
else if (task.IsCompleted)
{
userName.text = "alright";
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
}
});
然后添加此代码:
function capture() {
html2canvas($('body'),{
onrendered: function (canvas) {
var imgString = canvas.toDataURL("image/png");
window.open(imgString);
}
}
});
这将在您单击按钮时使<a href="#" onclick="capture()">Click for screenshot!</a>
元素的背景(即页面的背景)成为页面的屏幕截图。