首页 > 代码库 > html2canvas实现js截图

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 = http://www.mamicode.com/dataUrl; document.body.appendChild(newImg);

//window.location.href = http://www.mamicode.com/dataUrl;

} }); }

</script>

</head> <body>

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

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

</body> </html>

 

 

 

 

 

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

html2canvas实现js截图