• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > html5使用html2canvas实现浏览器截图

html5使用html2canvas实现浏览器截图

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含html2canvas,截图,html2canvas,浏览器截图等相关知识,匿名希望在学习及工作中可以帮助到您
这篇文章主要介绍了html5使用html2canvas实现浏览器截图的示例,非常具有实用价值,需要的朋友可以参考下

最近做项目为了解决全局异常信息记录,研究了一下浏览器全屏截图功能,方便用户发现异常时能够快速截图发给管理员。最终记录的异常信息如下,上面的【截图报告管理员】就是使用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对象。

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

问题分析

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

源码:

 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();
                    }
                });
            });

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注微课江湖!

相关推荐:

html 基于 canvas 实现截图的介绍

HTML5实现留言和回复的页面样式

以上就是html5使用html2canvas实现浏览器截图的详细内容,更多请关注微课江湖其它相关文章!

您可能想查找下面的文章:

  • html5使用html2canvas实现浏览器截图
  • html 基于 canvas 实现截图的介绍

相关文章

  • 2018-12-03有人说基于成熟后的HTML5 移动web应用才是未来,因为省去了app移动应用在不同终端的开发时间。基于终端的移动应用也会走下舞台,各位怎么认为?
  • 2018-12-03 小强的HTML5移动开发之路(16)——神奇的拖放功能
  • 2018-12-03HTML5新增的表单元素和属性实例解析_html5教程技巧
  • 2017-08-06HTML5实现获取地理位置信息并定位功能
  • 2018-12-03H5移动端各种各样的列表的制作方法(六)
  • 2018-12-03html5制作工具怎么做屏幕自适应?
  • 2018-12-03HTML5实践-三步实现响应式设计的详细介绍
  • 2018-12-03H5的Drag与Drop详解
  • 2018-12-03小强的HTML5移动开发之路(40)——jqMobi中实践header定义的几种方式
  • 2018-12-03如何评价Hex FRVR?

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • html5的history API
    • HTML5教程 - HTML5 表单 2.0
    • 大一计算机学生,如何自学成为一名前端开发工程师?
    • HTML5标准学习-简介介绍
    • HTML5游戏开发引擎-初识CreateJS的详解(图文)
    • html5 Canvas绘制线条 closePath()实例代码 _html5教程技巧
    • 利用HTML5中的Canvas绘制笑脸的代码
    • Less与Sass框架如何使用?
    • Html5学习之旅-html5的留言记事本开发(17)
    • 14款HTML5时钟动画

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有