html2canvas实现js截图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html2canvas实现js截图相关的知识,希望对你有一定的参考价值。

<html lang="en">

<head>

<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>

<script type="text/javascript" src="js/html2canvas.js"></script>

<title></title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="this is my page">

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<script type="text/javascript">

function print(){ var w = $("#canv").width(); var h = $("#canv").height();

//要将 canvas 的宽高设置成容器宽高的 2 倍

var canvas = document.createElement("canvas");

canvas.width = w * 2; canvas.height = h * 2;

canvas.style.width = w + "px";

canvas.style.height = h + "px";

var context = canvas.getContext("2d");

//然后将画布缩放,将图像放大两倍画到画布上

context.scale(2,2); html2canvas($("#canv") ,{

onrendered: function(canvas){

var dataUrl = canvas.toDataURL();

document.body.innerHTML="";

var newImg = document.createElement("img");

newImg.src = dataUrl; document.body.appendChild(newImg);

//window.location.href = dataUrl;

} }); }

</script>

</head> <body>

<divid="canv"> 测试 <hr/> </div>

<type="button"id="down_button"href="javascript:print()">生成</a>

</body> </html>

 

 

 

 

 

///**/toDateUrl有跨域问题*/怎么解决??

以上是关于html2canvas实现js截图的主要内容,如果未能解决你的问题,请参考以下文章

介绍两个关于生成截图图片的js库:html2canvas

js实现截图功能

Js实现将html页面或div生成图片

html2canvas.js插件截图空白问题

html2canvas.js插件截图空白问题

js截图及绕过服务器图片保存至本地(html2canvas)