当前位置: 首页 > news >正文

工作手记之html2canvas使用概述

什么是html2canvas?

以前我们只能通过其他的截图工具来截取图像。现代浏览器的功能已经越来越强,随着H5的逐渐普及,浏览器本身就可以截图啦。html2canvas就是这样一款前端插件,它的原理是将Dom节点在Canvas里边画出来。虽然很方便,但有以下限制:

  • 不支持iframe
  • 不支持跨域图片
  • 不能在浏览器插件中使用
  • 部分浏览器上不支持SVG图片
  • 不支持Flash
  • 不支持古代浏览器和IE,如果你想确认是否支持某个浏览器,可以用它访问http://deerface.sinaapp.com/ 试试 :)

工作原理

Html2canvas加载后将会浏览页面上的所有元素,集合所有页面元素的信息,然后用户就可以通过Html2canvas把整个页面截图下来。

换句话说,Html2canvas不会实际上的截图,而是通过从DOM读取的足够信息去建立一个页面的展示镜像。

这就会导致Html2canvas只会渲染它认识的正确的DOM元素属性,还有很多CSS属性是不会生效的,也就渲染不出来了。

使用方法

html2canvas(element, options);

带有回掉函数的:

html2canvas(element, {
    onrendered: function(canvas) {
        // canvas 是最后一个渲染的<canvas> 元素
    }
});

可用参数

NameTypeDefaultDescription
allowTaintbooleanfalseWhether to allow cross-origin images to taint the canvas
backgroundstring#fffCanvas background color, if none is specified in DOM. Set undefined for transparent
heightnumbernullDefine the heigt of the canvas in pixels. If null, renders with full height of the window.
letterRenderingbooleanfalseWhether to render each letter seperately. Necessary ifletter-spacing is used.
loggingbooleanfalseWhether to log events in the console.
proxystringundefinedUrl to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.
taintTestbooleantrueWhether to test each image if it taints the canvas before drawing them
timeoutnumber0Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.
widthnumbernullDefine the width of the canvas in pixels. If null, renders with full width of the window.
useCORSbooleanfalseWhether to attempt to load cross-origin images as CORS served, before reverting back to proxy

常见问题

1.图片跨域导致截图失败

报错信息如下:
Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

解决方法:

- 在跨域的服务器上设置header设置为允许跨域请求。
- 借助代理脚本获得外域图片的 base64 编码后的字符串

总结

虽然在使用过程中会出现一些问题,但是就目前来看,html2canvas的确为前端截图提供了非常大的作用,让我们摆脱了后台的关联处理。

相关文章:

  • REGEXP 正则的实现两个字符串组的匹配。(regexp)
  • ImageLoader
  • 互联网盈利模式研习笔记之二:佣金与分成
  • usb_modeswitch使用详解
  • 能源项目xml文件 -- app-datasource.xml
  • 微小宝中实际支出是如何计算的?
  • python ipaddress模块使用
  • SQLAlchemy ORM高级查询之过滤,排序
  • 杰卡德距离
  • 利用beans.xml进行简单的Spring应用上下文创建与使用
  • javaScript的正则表达式 (一)
  • spark学习1(hadoop集群搭建)
  • 记录水电系统开发的心理【1】
  • MySQL -- 关闭 binlog
  • MySQL密码丢失,解决方法
  • JavaScript 如何正确处理 Unicode 编码问题!
  • #Java异常处理
  • ES6系统学习----从Apollo Client看解构赋值
  • HTTP请求重发
  • Js实现点击查看全文(类似今日头条、知乎日报效果)
  • Linux Process Manage
  • Map集合、散列表、红黑树介绍
  • React as a UI Runtime(五、列表)
  • spring boot 整合mybatis 无法输出sql的问题
  • XForms - 更强大的Form
  • 初识MongoDB分片
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 两列自适应布局方案整理
  • 模型微调
  • 如何胜任知名企业的商业数据分析师?
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 06-01 点餐小程序前台界面搭建
  • 如何正确理解,内页权重高于首页?
  • ​2020 年大前端技术趋势解读
  • #Linux(帮助手册)
  • #考研#计算机文化知识1(局域网及网络互联)
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • (3)(3.5) 遥测无线电区域条例
  • (4)Elastix图像配准:3D图像
  • (C语言)二分查找 超详细
  • (poj1.3.2)1791(构造法模拟)
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (定时器/计数器)中断系统(详解与使用)
  • (二)pulsar安装在独立的docker中,python测试
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (九)One-Wire总线-DS18B20
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (四)linux文件内容查看
  • (算法)求1到1亿间的质数或素数
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)Sql Server 保留几位小数的两种做法