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

2020-04-21 23:25:05易采站长站整理
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();
}
});
});

总结

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