我有一个网页,我需要一个按钮,其中截取屏幕截图。现在,我有以下js函数,它被html上的按钮驱动:
var takeScreenShot = function() {
html2canvas(document.getElementById("container"), {
onrendered: function (canvas) {
var tempcanvas=document.createElement('canvas');
tempcanvas.width=550;
tempcanvas.height=650;
var context=tempcanvas.getContext('2d');
context.drawImage(canvas,0,0,tempcanvas.width,tempcanvas.height,0,0,tempcanvas.width,tempcanvas.height);
var link=document.createElement("a");
link.href=tempcanvas.toDataURL('image/jpg');
link.download = 'screenshot.jpg';
link.click();
}
});
}
然后,html具有以下内容:
<div style="margin-right: 100px; margin-left: 600px" id="container">
<table class="myTable">
<th class="titles">Title</th>
<!-- code code code -->
<button onclick="takeScreenShot()">Table Screenshot</button>
</table>
</div>
这样可行,但问题是所拍摄的屏幕出现错误。下一张图片是如何截取屏幕截图:
Screenshot taken with the button
这就是它应该如何。