之前写过一篇关于 html2canvas如何在元素隐藏的情况下生成截图 的文章,后面发现还有个坑在等着我,就是如果合成图片太大,超出了浏览器的可视区域,那么超出部分是无法截图的。在网上找到了以下方法,亲测有效~

源码:

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

如下图:

修改为(红色为改动部分):

//解决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;
});

如下图:

调用:

$(".js_show_promote").click(function () {
if(!$(this).hasClass("open")){
var thisCapture = $(this).find(".js_moneyCode_capture"); //需要捕获的区域
setTimeout(function(){imgToCanvas(thisCapture);},500);
}
}); function imgToCanvas(captureArea) {
var captureWidth = $(captureArea).outerWidth(),
captureHeight = $(captureArea).outerHeight();
html2canvas($(captureArea), {
height: captureHeight,
width: captureWidth,
onrendered: function (canvas) {
$(".js_moneyCode_picSmall").attr("src",canvas.toDataURL()).show();
$(".js_pic_loading").remove();
$(".js_down_moneyCode_pic").attr("href",canvas.toDataURL());
}
});
}

方法已介绍完毕~特别感谢@焰尾迭~提供的解决方案~关于其他更多的坑,可以看看html2canvas 踩坑总结

使用html2canvas实现超出浏览器部分截图的更多相关文章

  1. jquery easyui防止超出浏览器边界

    var easyuiPanelOnMove=function(left,top){ if(left<0){ $(this).window('move',{ left:1 }); } if(top ...

  2. jquery easyui中的dialog拖动超出浏览器问题解决办法

    juqery easyui当鼠标点着拖动超出浏览器后,就回不来了,拉不下的问题: //控制dialog超出浏览器回到原来的地方 var default_left; var default_top;// ...

  3. 解决jqueryeasyUI dialog 弹出窗体超出浏览器,导致不能关闭的bug

    使用panel的onMove事件攻克了panel,dialog以及window组件在被拖动时,会超出浏览器边界而无法拖回的情况. 当窗体被拖出浏览器有边界时.$(document).width();会 ...

  4. python操作浏览器及截图小结

    近期做网页自动化用到内容小结 1.打开浏览器1)打开默认配置的浏览器from selenium import webdriverdriver = webdriver.Firefox()"&q ...

  5. jqueryeasyUI dialog 弹出窗口超出浏览器,导致不能关闭的bug解决方案

    jqueryeasyUI dialog 弹出窗口超出浏览器,导致不能关闭的bug解决方案 2014年8月30日 3233次浏览 相信很多前端朋友都用过jqueryeasyUI,jqueryeasyUI ...

  6. 大型情感剧集Selenium:9_selenium配合Pillow完成浏览器局部截图

    网页截图 上次提到了selenium的四种截图方法,最终截图了整张网页.但很多时候,我们仅仅需要截图部分的内容.比如截取某个关键信息,或者现在已经不常见的截图验证码(现在都是各种按规则点击-).那么我 ...

  7. Chrome浏览器界面截图

    常常出现这么一个场景: 网页比较长,需要滚动屏幕才能看完整.这时候如需截屏,则比较麻烦. 如下为解决方法: 推荐Chrome浏览器: 按F12打开调试页面,同时按下ctrl + shift + p, ...

  8. html2canvas实现浏览器截图的原理(包含源码分析的通用方法)

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师. 官方网站:devui.design Ng组件库:ng-devui(欢 ...

  9. Cobalt Strike系列教程第五章:截图与浏览器代理

    Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...

随机推荐

  1. 60款与DevOps相关的开源工具

    原文地址:https://elasticbox.com/blog/de ... ools/ 你喜欢免费的东西吗?获得开发者社区支持的自动化,开源的工具是大家梦寐以求的.这里列举了 60+ 款最棒的开源 ...

  2. DataTime? 的 GetValueOrDefault() 方法

    DataTime? 转换为 DataTime类型 就可以调用 ToString()  自定义格式 @item.CreateDate.GetValueOrDefault().ToString(" ...

  3. PLSQL_标准游标类型的解析(概念)

    2014-06-02 Created By BaoXinjian

  4. HDU 5092 DP

    DP水题 求从上到下走完,使所取得权值最小,并输出路径,若有多个满足,则输出靠右的 #include "stdio.h" #include "string.h" ...

  5. C#实现WinForm下DataGridView控件的拷贝和粘贴

    DataGridView控件应该是数据库应用系统最常用的控件之一,其方便性不言而喻的.往往用户在使用过程中会提出"从DataGridView空间 中拷贝数据或是向某个DataGridView ...

  6. jpa 批量插入

    @Override @Transactional public <S extends E> List<S> save(Iterable<S> entities) { ...

  7. Ruby gem 更换国内源

    gem sources --add http://gems.ruby-china.org/ --remove https://rubygems.org/

  8. Windows上怎么安装ELK

    In this guide I will show that it is also possible to run Logstash on a Windows Server 2012 machine ...

  9. Model層資料驗證

    概述 上节我们学习了Model的数据在界面之间的传递,但是很多时候,我们在数据传递的时候为了确保数据的有效性,不得不给Model的相关属性做基本的数据验证. 本节我们就学习如何使用 System.Co ...

  10. t-sql的一些经验

    1.存储过程的3种传回值: 1.以return传回整数 2.以output格式传回参数 3.recordset 2.字符串类型的变量需要初始化后再使用,不然永远是空 DECLARE @FieldsSq ...