加载的html2canvas为官网上的新版本。

 

style样式:

*{
margin: 0;padding: 0;
font-family: "微软雅黑";
}
html,body{
width: 100%;
}
#capture,#imgDiv{
width: 100%;
}
.imgDiv_img{
width: 100%;
}

 

html:

<div id="capture" style="padding: 10px; background: #f5da55">
<h4 style="color: #000; ">页面内容</h4>
</div>
<div id="imgDiv">
<div class="imgDiv_title">生成的图片</div>
<img class="imgDiv_img" src="" />
</div>

 

script代码:

<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/html2canvas.js" type="text/javascript" charset="utf-8"></script>

$(function() {
html2canvas(document.querySelector("#capture")).then(function(canvas) {
var imgUri = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); // 获取生成的图片的url
$(".imgDiv_img").attr("src",imgUri)
})
});