html2canvas
html2canvas 介绍
html2canvas可以通过纯js对浏览器端经行截屏,但截图的精确度还有待提高,部分css不可识别,所以在canvas中不能完美呈现原画面样式 。
html2canvas兼容性:
Firefox 3.5+
Google Chrome
Opera 12+
IE9+
Safari 6+
html2canvas使用
1、安装npm install --save html2canvas//或者yarn add html2canvas2、使用html2canvas 的使用非常简单,简单到只需要传入一个 DOM 元素,然后通过回调拿到 canvas html:<div id="capture" padding: 10px; background: #f5da55"><h4 color: #000; ">Hello world!</h4></div>js:html2canvas(document.querySelector("#capture")).then(canvas => {document.body.appendChild(canvas)});html2canvas注意点
1.html2canvas 通过解析元素实际的样式来生成 canvas 图片内容,因此它对元素实际的布局和视觉显示有要求。如果要完整截图,最好将元素从文档流中独立出来(例如 position:absolute)
2.默认生成的 canvas 图片在 retina 设备上显示很模糊,处理成 2 倍图能解决这个问题
网站地址:https://html2canvas.hertzen.com/
GitHub:https://github.com/niklasvh/html2canvas
网站描述:一个js实现从浏览器网页截图的开源库