需求:将网页生成图片,用户自行长按图片进行保存图片,再分享朋友圈。其中,都可识别图中的二维码。(二维码过小会识别不出)

首先,先来科普一下微信网页识别二维码原理:截屏识别,当客户端发现用户在网页的img标签内进行长按操作时,会立刻截屏并且启动二维码识别算法。https://www.cnblogs.com/daipi...

发现官网中的html2canvas.js插件存在一些bug:


1.截图不全,不完整
解决方案:
//修改源码,添加自定设置高度宽度

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;
}); 2.图片像素模糊:
解决方案:添加dpi参数 function CanvasRenderer(width, height) {
Renderer.apply(this, arguments);
this.canvas = this.options.canvas || this.document.createElement("canvas");
if (!this.options.canvas) {
if (this.options.dpi) {
this.options.scale = this.options.dpi / 96
}
this.canvas.width = width;
this.canvas.height = height
}
this.ctx = this.canvas.getContext("2d");
this.taintCtx = this.document.createElement("canvas").getContext("2d");
this.ctx.textBaseline = "bottom";
this.variables = {};
log("Initialized CanvasRenderer with size", width, "x", height)
}

最后写写html2canvas的使用方法:


var height = $('.teacher_page').scrollTop() + $('.teacher_page').outerHeight(true);
html2canvas($(".teacher_page"),{
height:height,
// window.devicePixelRatio是设备像素比
dpi: 192,//放大像素(2倍),以免图片模糊
}).then(function(canvas) {
var imgUri = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
$('.popBody').html('<img alt="二维码" src="'+imgUri+'" id="canvas"/>');
});

官网:http://html2canvas.hertzen.com/
下载地址:https://www.bootcdn.cn/html2c...

原文地址:https://segmentfault.com/a/1190000016876592

html2canvas.js网页截图功能的更多相关文章

  1. 使用html2canvas实现网页截图,并嵌入到PDF

    使用html2canvas实现网页截图并嵌入到PDF 以前我们只能通过截图工具进行截取图像.这使得在业务生产中,变得越来越不方便.目前的浏览器功能越来越强大,H5也逐渐普及,浏览器也可以实现截图了.这 ...

  2. html2canvas.js插件截图空白问题

    发现使用 html2canvas.js插件截图保存在前端很方便.学习过程中预计问题. 截图出现空白和截图不全. 问题原因: html2canvas.js插件截图是基于body标签的,如果body存在滚 ...

  3. JS 使用html2canvas实现页面截图功能

    html2canvas的官方文档地址:http://html2canvas.hertzen.com/ 实现原理:将需要截图的页面在canvas中进行重绘,这样将页面转换成图片的过程. 注意事项: 不支 ...

  4. html2canvas - 实现网页截图(+下载截图) 功能

    实现:html2canvas + canvas.toDataURL 首先,引入依赖插件: import { html2canvas } from './html2canvas'; html2canva ...

  5. 使用html2canvas实现网页截图并嵌入到PDF

    以前我们只能通过截图工具进行截取图像.这使得在业务生产中,变得越来越不方便.目前的浏览器功能越来越强大,H5也逐渐普及,浏览器也可以实现截图了.这里来聊下之前在工作中用到的html2canvas.这里 ...

  6. html2canvas 将网页截图为图片,上传base64 到服务端

    await html2canvas(getById("winyh"), { height:500, allowTaint: true, useCORS: true, }).then ...

  7. js网页倒计时功能(天,时,分,秒)

    给定任何一个时间,然后实现现在到那个时间的倒计时. 下面的例子是显示现在到2019年8月1号0时0分的倒计时: <div class="list"> <span ...

  8. html2canvas canvas webgl 截图透明空🤣

    1. React用这个插件html2canvas完成div截图功能,div里面嵌套canvas,返回base64是透明图片. html2canvas(document.getElementById(& ...

  9. JS 使用html2canvas实现截图功能的问题记录和解决方案

    在实现“截图”功能时,遇到几个bug,研究了一个上午,终于全部解决了: 下面给大家分享下: 1."图片资源跨域",导致无法截图. 浏览器会提示下面的错误 DOMException: ...

随机推荐

  1. python中的计时器:timeit模块

    python中的计时器:timeit模块 (1) timeit - 通常在一段程序的前后都用上time.time()然后进行相减就可以得到一段程序的运行时间,不过python提供了更强大的计时库:ti ...

  2. bzoj1492 [NOI2007]货币兑换Cash【cdq分治】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1492 推荐博客:http://www.cnblogs.com/zig-zag/archive ...

  3. Codeforces Round #396 (Div. 2) E

    Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirec ...

  4. FTP任务(重点看断点续传)

    一.FTP任务目录: 1. 多用户同时登陆:     socketserver 2. 用户登陆,加密认证: md5加密 3. 上传/下载文件,保证文件一致性:md5摘要 4. 传输过程中现实进度条 5 ...

  5. Elasticsearch之安装

    elasticsearch需要java8以上支持 java -version 二进制文件下载 www.elastic.co/downloads tar安装示例 1.下载tar文件 curl -L -O ...

  6. asp.net,监听输入框值的即时变化onpropertychange、oninput

    作者:自由天堂发布站点:WEB六零零 网页设计制作原文地址:http://www.web600.net/html/editor/JavaScript/201001131529.html 要达到的效果 ...

  7. Spring-打印机案例

    1.导包 <!--beans--><dependency> <groupId>org.springframework</groupId> <art ...

  8. 使用Intellij IDEA搭建一个简单的Maven项目

    IntelliJ IDEA是Java最优秀的开发工具,它功能全面,提示比较智能,开发界面炫酷,新技术支持的比较迅速. 我使用了Eclipse快10年了,IntelliJ IDEA这么好用必须要试一试. ...

  9. chrome浏览器之网络面板

    这篇指导向你展示怎样检测网络张状况或者在chrome开发工具的网络面板中尽可能的优化网页. 排列的或受阻的请求 症状:同时发出六个请求.之后有一系列的请求排队或受阻.一旦最先的六个请求中有一个响应结束 ...

  10. GCD 使用说明

    GCD提供的一些操作队列的方法 名称 说明 dispatch_set_target_queue 将多个队列添加到目标队列中 dispatch_group 将多个队列放入组中,监听所有任务完成状 dis ...