<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Test</title>
<style type="text/css">
*{
margin:0 auto;
padding:0;
}
.btn{
width:100px;
height:30px;
line-height:30px;
background:green;
color:#fff;
border-radius:10px;
-webkit-border-radius:10px;
text-align:center;
display:block;
text-decoration:none;
}
.container{
background:#e5e5e5;
}
</style>
<script src="https://cdn.bootcss.com/html2canvas/0.4.1/html2canvas.js"></script>
</head>
<body>
<div style="padding:10px 0">
<div class="btn" id="btn" onclick="copyEle()">截取</div>
<p style="color:red; text-align:center;">截图</p>
<div style="margin-top:10px">
<a href="javascript:;" class="btn" id="download">下载</a>
</div>
</div>
<div class="container" id="container">
<p style="text-align:center">以下是测试内容</p>
<img src='http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20171210/998c17cbf43d4e5ea75740282e751c86.jpeg' />
</div>
<script>
function copyEle(){
html2canvas(document.getElementById('container'), {
scale:2,logging:false,useCORS:true,
onrendered: function(canvas) {
//把截取到的图片替换到a标签的路径下载
document.getElementById('download').setAttribute('href',canvas.toDataURL());
//下载下来的图片名字
document.getElementById('download').setAttribute('download','下载.png') ;
document.body.appendChild(canvas);
}
//可以带上宽高截取你所需要的部分内容
// ,
// width: 300,
// height: 300
});
}
</script>
</body>
</html>