html2canvas通过获取页面的DOM和元素的样式信息,并将其渲染成canvas图片,从而实现给页面截图的功能。
因为每个浏览器渲染页面的方式都不尽相同,所以生成的图片也不太一样。
环境要求: jQuery
兼容性: Firefox 3.5+, Chrome, Opera, IE9
官网主页: http://html2canvas.hertzen.com/
测试生成的图片效果 有些元素的样式没有完全展示出来
1 <html> 2 ????<head> 3 ????????<meta name="layout" content="main"> 4 ????????<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ??5 ????????<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 6 ????????<script type="text/javascript" src="http://html2canvas.hertzen.com/build/html2canvas.js"></script> 7 ??????????8 ????????<script ?type="text/javascript" > 9 ????????$(document).ready( function(){10 ????????????????$(".example1").on("click", function(event) {11 ????????????????????????event.preventDefault();12 ????????????????????????html2canvas(document.body, {13 ????????????????????????allowTaint: true,14 ????????????????????????taintTest: false,15 ????????????????????????onrendered: function(canvas) {16 ????????????????????????????canvas.id = "mycanvas";17 ????????????????????????????//document.body.appendChild(canvas);18 ????????????????????????????//生成base64图片数据19 ????????????????????????????var dataUrl = canvas.toDataURL();20 ????????????????????????????var newImg = document.createElement("img");21 ????????????????????????????newImg.src = ?dataUrl;22 ????????????????????????????document.body.appendChild(newImg);23 ????????????????????????}24 ????????????????????});25 ????????????????}); 26 ?????????????27 ????????});28 ?????????29 ????????</script>30 ????</head>31 ????<body>32 ?????????33 ????????Hello!34 ????????<div class="" style="background-color: #abc;">35 ????????????计算机天堂测试html5页面截图36 ????????????<br>jsjtt.com37 ????????</div>38 ?????????39 ????????<textArea id="textArea" col="20" rows="10" ></textArea>40 ????????<input class="example1" type="button" value="button">41 ????????生成界面如下:42 ????</body>43 </html>
说明在测试过程中出现的问题如果页面上引用跨域的图片文件调用toDataURL的时候会出错
SecurityError: The operation is insecure.
解决方法:在跨域的服务器上设置header设置为允许跨域请求
access-control-allow-origin: * ?access-control-allow-credentials: true
原始地址:计算机天堂---html5 实现网页截屏 页面生成图片 http://www.jsjtt.com/webkaifa/html5/2013-10-29/42.html
Html5 实现网页截屏 页面生成图片(图文)
原文地址:http://www.cnblogs.com/hxzblog/p/7509102.html