html2canvas

作者: Rotten_Pencil | 来源:发表于2016-06-03 22:21 被阅读33534次

作用

  • html2canvas可以通过纯JS对浏览器端经行截屏,但截图的精确度还有待提高,部分css不可识别,所以在canvas中不能完美呈现原画面样式

支持的浏览器

  • Firefox 3.5+
  • Google Chrome
  • Opera 12+
  • IE9+
  • Safari 6+

基本语法

 //两个参数:所需要截图的元素id,截图后要执行的函数, canvas为截图后返回的最后一个canvas
 html2canvas(document.getElementById('id')).then(function(canvas) {document.body.appendChild(canvas);});

可用参数

参数名称 类型 默认值 描述
allowTaint boolean false Whether to allow cross-origin images to taint the canvas---允许跨域
background string #fff Canvas background color, if none is specified in DOM. Set undefined for transparent---canvas的背景颜色,如果没有设定默认透明
height number null Define the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度设定
letterRendering boolean false Whether to render each letter seperately. Necessary if letter-spacing is used.---在设置了字间距的时候有用
logging boolean false Whether to log events in the console.---在console.log()中输出信息
proxy string undefined Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.---代理地址
taintTest boolean true Whether to test each image if it taints the canvas before drawing them---是否在渲染前测试图片
timeout number 0 Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---图片加载延迟,默认延迟为0,单位毫秒
width number null Define the width of the canvas in pixels. If null, renders with full width of the window.---canvas宽度
useCORS boolean false Whether to attempt to load cross-origin images as CORS served, before reverting back to proxy--这个我也不知道是干嘛的

例子

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>html2canvas example</title>
    <script type="text/javascript" src="html2canvas.js"></script>
</head>
<script type="text/javascript">
function takeScreenshot() {
  console.log('test');
    html2canvas(document.getElementById('view'), {
        onrendered: function(canvas) {
            document.body.appendChild(canvas);
        },
      // width: 300,
    // height: 300
    });
}
</script>
<body>
    <div id="view" style="background:url(test.jpg) 50%; width: 700px; height: 500px;">
        <input type="button" value="截图" onclick="takeScreenshot()">
    </div>
</body>

</html>

效果图

Paste_Image.png
  • 截图后
Paste_Image.png

html2canvas官网

相关文章

网友评论

  • 6e2da733b280:在ie11中使用,报promise未定义的错误,你们是怎么解决的?
    274c8d1e6be4:https://www.babeljs.cn/docs/usage/polyfill/
    跟着引入这个就好了,ie11原生不支持es6的一些特性
    421d3c51d2b6:请问解决了吗?
    墨羽i:同样的错 你解决了么
  • f10f363f8a85:您好,为什么设置了useCors为true之后,canvas中获取到的跨域图片只是显示了部分呢,在地址栏多刷新几次以后,看控制台走缓存的话就可以截图到完整的图了,能帮忙回答一下吗?
  • color_小浣熊:楼主 为什么我的onrendered 函数进不去,都不进这个函数 ,我在网上百度了,但还是没找到解决问题的方法,望解答。
    a5475c02d2f9:我也是同样的情况,不使用onrendered回调函数而使用.then()就可以进行回调。我估计可能是1.0.0alpha版的问题,正常开发使用还是退回beta旧版比较好
  • 轻舟青箬笠:请问,我在ie11下使用html2canvas报错,提示“Promise”未定义,定位具体报错位置,是在html2canvas.js第586行“return new Promise(function(resolve)”处。网上有人说html2canvas根本不支持ie,但是在本文中看到楼主说支持IE9+,所以想咨询一下我这个错误是否有解决办法,感谢
    墨羽i:@Garriby 我也引了 不管用 求解决办法
    6e2da733b280:@Garriby 我引入bluebird.js没有起作用?你是怎么解决的?
    轻舟青箬笠:我引入了bluebird.js,增加了ie对promise的支持就可以了
  • Nic_ofh:跨域图片java怎么解决啊?图片地址和服务器地址是2个不一样的!
    天上天下_aa8f:参考
    https://www.toutiao.com/i6615074630862373390/
    https://blog.csdn.net/qq_32859565/article/details/83273248
    这两个都可以
    谭冉冉:useCORS: true, //(图片跨域相关)
    allowTaint: false, //允许跨域(图片跨域相关)
    加上这两个参数
  • Nic_ofh:楼主 切图之后怎么把图片传给后台?
    谭冉冉:base64,post给服务器
  • d6bdbdc11df8:为什么我的截图下载下来是透明的?
    谭冉冉:跨域的问题可以加上这两个参数就解决了
    useCORS: true, //(图片跨域相关)
    allowTaint: false, //允许跨域(图片跨域相关)
    html2canvas(document.getElementById('imgbg'), {
    useCORS: true, //(图片跨域相关)
    allowTaint: false, //允许跨域(图片跨域相关)
    taintTest: true, //是否在渲染前测试图片
    onrendered: function(canvas) {
    document.getElementById('imgResult').src = canvas.toDataURL();
    }
    });
    a959911cc09b:@Ongoingloves 跨与图片如何处理?node.js下的proxy的参数是什么
    c6d1e873e6cf:图片不能跨域而且必须运行在服务器模式下。。不能单纯的html
  • d319c8979ca2:为什么我使用这个插件截得的图片上面会有黑点存在
  • MatrixFK:在写react项目的时候使用了这一个插件,也觉得效果不是很好,而且div一大起来会有很多的空白
    MatrixFK: @MatrixFK 对了,我用的是dom-to-image,非常好用
    MatrixFK: @乐幽_0227 我的解决方案是生成图片前计算好div的大小,然后传入
    68370c9982ed:如何解决滚动图片太长,只能截取到高度为1399像素的问题呢?高度超过1400px,但是图片原本的内容会被空白代替,请问这个怎么解决呢?

本文标题:html2canvas

本文链接:https://www.haomeiwen.com/subject/eicldttx.html