html2canvas 实现网页截图 / 下载html2canvas.js插件包

html2canvas可以通过纯JS对浏览器网页进行截屏,但截图的精确度还有待提高

官网: http://html2canvas.hertzen.com/

gitHub: https://github.com/niklasvh/html2canvas

引用 : <script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>

主要用法是:

html2canvas(document.getElementById('view'), {
        onrendered: function(canvas) {
            document.body.appendChild(canvas);

           // console.log(canvas.toDataURL());
        },
      // width: 300,
     // height: 300
 });

这是一个demo

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>html2canvas example</title>
    <style>
    	#view{
    		border: 1px solid red;
    		background-image: url(image/c05ddd91c8803ea8306f02cf4a10fcdb.png);
    		background-size: 100%;
    		width: 100px; 
    		height: 100px;
    	}
    </style>
</head>
<body>
    <div id="view">
        <input type="button" value="截图" onclick="takePhoto()">
    </div>
</body>
<script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>
<script type="text/javascript">
	function takePhoto() {
	    html2canvas(document.getElementById('view'), {
	        onrendered: function(canvas) {
	        	console.log(canvas.toDataURL());//这是截图的base64
	            document.body.appendChild(canvas);//这是有截图的画布
	        },
	    });
	} 
</script>
</html>
	

运行之后:

点击截图按钮:

下面出现的就是截图,不过不太清楚。

当我们需要对html2canvas.js的文件进行修改的时候,就不能再直接引用链接,需要获取它的插件包

获取html2canvas.js地址:download html2canvas

步骤:

1.打开上面的地址,可以看到下载页面:

2.点击

出现弹框如图:

3.点击external mirror(Alpha),就可以下载到0.5.0-beta4版本的插件包,如图:

补充:刚刚找到了一个更简单的下载html2canvas的方法!链接:https://www.bootcdn.cn/html2canvas/

点进去,就是如图的界面

各种版本号应有尽有,下载源码的话,要到github上下。

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值