您现在的位置是:网站首页> 编程资料编程资料
html5使用html2canvas实现浏览器截图的示例HTML5中外部浏览器唤起微信分享功能的代码html5的pushstate以及监听浏览器返回事件的实现HTML5中外部浏览器唤起微信分享处理HTML5新标签的浏览器兼容版问题如何查看浏览器对html5的支持情况
2021-09-02
1089人已围观
简介 这篇文章主要介绍了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 = $("").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 = $("").attr("href", url).attr("download", getNowFormatDate()+"异常信息.png").appendTo("body"); triggerDownload[0].click(); triggerDownload.remove(); } }); });总结
通过前端插件即实现了浏览器全屏截图功能,不得不说H5功能越来越强大,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关内容
- 简单聊聊H5的pushState与replaceState的用法HTML5 history新特性pushState、replaceState及两者的区别
- 利用html5 canvas动态画饼状图的示例代码用纯CSS实现饼状Loading等待图效果
- HTML5 拖拽批量上传文件的示例代码html5拖拽排序多图片上传插件特效源码html5实现多图片预览上传及点击可拖拽控件html5使用Drag事件编辑器拖拽上传图片的示例代码HTML5+CSS3实现无插件拖拽上传图片(支持预览与批量)HTML5 canvas实现移动端上传头像拖拽裁剪效果结合html5+nodejs+express实现拖拽上传的功能HTML5拖拽文件到浏览器并实现文件上传下载功能代码html5 拖拽上传图片实例演示HTML5拖拽文件上传的示例代码
- canvas实现图片马赛克的示例代码html5实现点击弹出图片功能html5 录制mp3音频支持采样率和比特率设置html5表单的required属性使用html5调用摄像头实例代码HTML5页面音频自动播放的实现方式Html5大屏数据可视化开发的实现html实现弹窗的实例HTML5来实现本地文件读取和写入的实现方法HTML 罗盘式时钟的实现HTML5简单实现添加背景音乐的几种方法
- html5 分层屏幕适配的方法使用分层画布来优化HTML5渲染的教程Html分层的box-shadow效果的示例代码
- 利用html5 file api读取本地文件示例(如图片、PDF等)使用PDF.JS插件在HTML中预览PDF文件的方法HTML5在线预览PDF的示例代码html转换为pdf案例的一些总结(多图推荐)HTML里显示pdf、word、xls、ppt的方法示例
- 基于 HTML5 Canvas实现 的交互式地铁线路图html5 canvas实现的斯诺克桌球游戏源码html5基于canvas绘制的熊熊火焰燃烧文字动画特效源码HTML5 Canvas图像模糊完美解决办法html5 canvas+three.js绘制的水面下太阳倒影动画特效源码html5 canvas绘制陨石围绕天体运动的动画特效源码
- 详解Html5 Canvas画线有毛边解决方法HTML5 Canvas画线技巧——实现绘制一个像素宽的细线HTML5 Canvas——用路径描画线条实例介绍
- 详解移动端HTML5音频与视频问题及解决方案html5中audio支持音频格式的解决方法HTML5实现音频和视频嵌入的方法HTML5中视频音频的使用详解HTML5制作酷炫音频播放器插件图文教程HTML5自定义mp3播放器源码
- 详解Canvas 实现炫丽的粒子运动效果(粒子生成文字)canvas中普通动效与粒子动效的实现代码示例
