html5使用html2canvas实现浏览器截图的示例


Posted in HTML / CSS onAugust 31, 2017

最近做项目为了解决全局异常信息记录,研究了一下浏览器全屏截图功能,方便用户发现异常时能够快速截图发给管理员。最终记录的异常信息如下,上面的【截图报告管理员】就是使用html2canvas前端插件实现的。

html5使用html2canvas实现浏览器截图的示例

html2canvas介绍

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

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

由于我的使用场景很简单,记录一下异常信息,并且异常页面也是由自己定义的,那么html2canvas 就足够使用了。

使用实例

引用jquery,html2canvas即可,使用代码也很简单。我这里使用的是 html2canvas 0.5.0 版本

html2canvas($("#tbl_exception"), {
         onrendered: function (canvas) {
             var url = canvas.toDataURL();
              //以下代码为下载此图片功能
             var triggerDownload = $("<a>").attr("href", url).attr("download", getNowFormatDate()+"异常信息.png").appendTo("body");
               triggerDownload[0].click();
               triggerDownload.remove();
           }
   });

第一个参数是要截图的Dom对象,第二个参数时渲染完成后回调的canvas对象。

Name Type Default Description
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
height number null Define the heigt of the canvas in pixels. If null, renders with full height of the window.
letterRendering boolean false Whether to render each letter seperately. Necessary ifletter-spacing is used.
logging boolean false Whether to log events in the console.
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.
width number null Define the width of the canvas in pixels. If null, renders with full width of the window.
useCORS boolean false Whether to attempt to load cross-origin images as CORS served, before reverting back to proxy

问题分析

介绍完使用之后,说说自己使用中遇到的问题,截图只能截取当前屏幕内的内容。在查看插件源码,进行调试之后找到了解决方案。下面贴出源码和修改后的代码

源码:

return renderDocument(node.ownerDocument, options, node.ownerDocument.defaultView.innerWidth, node.ownerDocument.defaultView.innerHeight, index).then(function(canvas) {
        if (typeof(options.onrendered) === "function") {
            log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
            options.onrendered(canvas);
        }
        return canvas;
    });

修改代码:

//2016-02-18修改源码,解决BUG 对于部分不能截屏不能全屏添加自定义宽高的参数以支持
    var width = options.width != null ? options.width : node.ownerDocument.defaultView.innerWidth;
    var height = options.height != null ? options.height : node.ownerDocument.defaultView.innerHeight;
    return renderDocument(node.ownerDocument, options, width, height, index).then(function (canvas) {
        if (typeof(options.onrendered) === "function") {
            log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
            options.onrendered(canvas);
        }
        return canvas;
    });

主要是让用户调用时能够自定义需要截取Dom对象的宽和高,现在调用方式如下

$("#btn_screen").on("click", function () {               
                html2canvas($("#tbl_exception"), {
                    height: $("#tbl_exception").outerHeight() + 20,
                    onrendered: function (canvas) {
                        var url = canvas.toDataURL();
                        //以下代码为下载此图片功能
                        var triggerDownload = $("<a>").attr("href", url).attr("download", getNowFormatDate()+"异常信息.png").appendTo("body");
                        triggerDownload[0].click();
                        triggerDownload.remove();
                    }
                });
            });

总结

通过前端插件即实现了浏览器全屏截图功能,不得不说H5功能越来越强大,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
 

HTML / CSS 相关文章推荐
手把手教你用纯css3实现轮播图效果实例
May 04 HTML / CSS
CSS3之transition实现下划线的示例代码
May 30 HTML / CSS
详解HTML5通讯录获取指定多个人的信息
Dec 20 HTML / CSS
HTML5-WebSocket实现聊天室示例
Dec 15 HTML / CSS
HTML5 Canvas实现文本对齐的方法总结
Mar 24 HTML / CSS
利用html5 canvas动态画饼状图的示例代码
Apr 02 HTML / CSS
HTML5+lufylegend实现游戏中的卷轴
Feb 29 HTML / CSS
详解HTML5中表单验证的8种方法介绍
Dec 19 HTML / CSS
前端实现弹幕效果的方法总结(包含css3和canvas的实现方式)
Jul 12 HTML / CSS
webView加载html图片遇到的问题解决
Oct 08 HTML / CSS
Html5与App的通讯方式详解
Oct 24 HTML / CSS
详解CSS3.0(Cascading Style Sheet) 层叠级联样式表
Jul 16 HTML / CSS
前端面试必备之html5的新特性
Sep 05 #HTML / CSS
html5仿支付宝密码框的实现代码
Sep 06 #HTML / CSS
HTML5 本地存储实现购物车功能
Sep 07 #HTML / CSS
HTML5 FormData 方法介绍以及实现文件上传示例
Sep 12 #HTML / CSS
html5自带表单验证体验优化及提示气泡修改功能
Sep 12 #HTML / CSS
html5利用canvas绘画二级树形结构图的示例
Sep 27 #HTML / CSS
html5新增的定时器requestAnimationFrame实现进度条功能
Dec 13 #HTML / CSS
You might like
Discuz!下Memcache缓存实现方法
2010/05/28 PHP
PHP生成随机用户名和密码的实现代码
2013/02/27 PHP
php获取网页中图片、DIV内容的简单方法
2014/06/19 PHP
php使用NumberFormatter格式化货币的方法
2015/03/21 PHP
利用php输出不同的心形图案
2016/04/22 PHP
Laravel 5.3 学习笔记之 配置
2016/08/28 PHP
在网页中使用document.write时遭遇的奇怪问题
2010/08/24 Javascript
JQUBar 基于JQUERY的柱状图插件
2010/11/23 Javascript
基于mootools插件实现遮罩层新手引导
2012/05/24 Javascript
Knockoutjs快速入门(经典)
2012/12/24 Javascript
把jQuery的类、插件封装成seajs的模块的方法
2014/03/12 Javascript
jQuery添加删除DOM元素方法详解
2016/01/18 Javascript
利用jQuery的动画函数animate实现豌豆发射效果
2016/08/28 Javascript
微信小程序 Canvas增强组件实例详解及源码分享
2017/01/04 Javascript
Bootstrap模态框插件使用详解
2017/05/11 Javascript
node.js中cluster的使用教程
2017/06/09 Javascript
vue中当图片地址无效的时候,显示默认图片的方法
2018/09/18 Javascript
详解vue引入子组件方法
2019/02/12 Javascript
使用Node.js实现base64和png文件相互转换的方法
2020/03/11 Javascript
autojs 蚂蚁森林能量自动拾取即给指定好友浇水的实现方法
2020/05/03 Javascript
Vue和React有哪些区别
2020/09/12 Javascript
Javascript中window.name属性详解
2020/11/19 Javascript
python获取url的返回信息方法
2018/12/17 Python
Python 编程速成(推荐)
2019/04/15 Python
python多线程下信号处理程序示例
2019/05/31 Python
浅谈Python3实现两个矩形的交并比(IoU)
2020/01/18 Python
阿玛尼美妆俄罗斯官网:Giorgio Armani Beauty RU
2020/07/19 全球购物
水污染治理专业毕业生推荐信
2013/11/14 职场文书
大学生职业生涯规划范文
2014/01/08 职场文书
邹越感恩父母演讲稿
2014/08/28 职场文书
2014年机关工会工作总结
2014/12/19 职场文书
经费申请报告
2015/05/15 职场文书
本科毕业论文答辩稿
2015/06/23 职场文书
2015年小学远程教育工作总结
2015/07/28 职场文书
《亲亲我的妈妈》观后感(3篇)
2019/09/26 职场文书
Vue组件更新数据v-model不生效的解决
2022/04/02 Vue.js