在开始使用html2canvas以前,有一些关于html2canvas及其一些限制的好处。css
该脚本容许您直接在用户浏览器上截取网页或部分网页的“屏幕截图”。屏幕截图基于DOM,所以它可能不是真实表示的100%准确,由于它没有制做实际的屏幕截图,而是根据页面上可用的信息构建屏幕截图。html
该脚本遍历其加载的页面的DOM。它收集有关那里全部元素的信息,而后用它来构建页面的表示。换句话说,它实际上并不截取页面的截图,而是根据从DOM读取的属性构建它的表示。
所以,它只能正确呈现它理解的属性,这意味着有许多CSS属性不起做用。
canvas
脚本使用的全部图像都须要位于相同的原点 下,以便可以在没有代理帮助的状况下读取它们。一样,若是您canvas
在页面上有其余元素,这些元素已被污染了跨源内容,它们将变得脏,而且再也不被html2canvas读取。小程序
该脚本不会呈现插件内容,如Flash或Java小程序。浏览器
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html2canvas</title> <script src="http://html2canvas.hertzen.com/dist/html2canvas.min.js"></script> </head> <body> <h1>html内容</h1> <div id="htmlcanvas" style="padding: 10px; background: red"> <h4 style="color: #fff; ">one!two!!three!!!</h4> <h2 style="text-align:center;">那一瞬间有一百万可能</h2> </div> <h1>canvas内容</h1> <div id="canid"></div> <script> html2canvas(document.querySelector("#htmlcanvas")).then(canvas => { document.getElementById("canid").appendChild(canvas); }); </script> </body> </html>
效果图:app
想了解html2canvas更多的伙伴们,能够去访问官网:http://html2canvas.hertzen.com/getting-startedflex