html2canvas.js网页截图功能
需求:将网页生成图片,用户自行长按图片进行保存图片,再分享朋友圈。其中,都可识别图中的二维码。(二维码过小会识别不出)
首先,先来科普一下微信网页识别二维码原理:截屏识别,当客户端发现用户在网页的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网页截图功能的更多相关文章
- 使用html2canvas实现网页截图,并嵌入到PDF
使用html2canvas实现网页截图并嵌入到PDF 以前我们只能通过截图工具进行截取图像.这使得在业务生产中,变得越来越不方便.目前的浏览器功能越来越强大,H5也逐渐普及,浏览器也可以实现截图了.这 ...
- html2canvas.js插件截图空白问题
发现使用 html2canvas.js插件截图保存在前端很方便.学习过程中预计问题. 截图出现空白和截图不全. 问题原因: html2canvas.js插件截图是基于body标签的,如果body存在滚 ...
- JS 使用html2canvas实现页面截图功能
html2canvas的官方文档地址:http://html2canvas.hertzen.com/ 实现原理:将需要截图的页面在canvas中进行重绘,这样将页面转换成图片的过程. 注意事项: 不支 ...
- html2canvas - 实现网页截图(+下载截图) 功能
实现:html2canvas + canvas.toDataURL 首先,引入依赖插件: import { html2canvas } from './html2canvas'; html2canva ...
- 使用html2canvas实现网页截图并嵌入到PDF
以前我们只能通过截图工具进行截取图像.这使得在业务生产中,变得越来越不方便.目前的浏览器功能越来越强大,H5也逐渐普及,浏览器也可以实现截图了.这里来聊下之前在工作中用到的html2canvas.这里 ...
- html2canvas 将网页截图为图片,上传base64 到服务端
await html2canvas(getById("winyh"), { height:500, allowTaint: true, useCORS: true, }).then ...
- js网页倒计时功能(天,时,分,秒)
给定任何一个时间,然后实现现在到那个时间的倒计时. 下面的例子是显示现在到2019年8月1号0时0分的倒计时: <div class="list"> <span ...
- html2canvas canvas webgl 截图透明空🤣
1. React用这个插件html2canvas完成div截图功能,div里面嵌套canvas,返回base64是透明图片. html2canvas(document.getElementById(& ...
- JS 使用html2canvas实现截图功能的问题记录和解决方案
在实现“截图”功能时,遇到几个bug,研究了一个上午,终于全部解决了: 下面给大家分享下: 1."图片资源跨域",导致无法截图. 浏览器会提示下面的错误 DOMException: ...
随机推荐
- java操作mongodb数据库实现新建数据库,新建集合,新建文档
*首先明确一点,要通过java代码创建mongodb数据库实例,需要同时创建集合和文档. 代码实现: /* 建立与mongodb数据库的连接,可指定参数,如:MongoClient client = ...
- collections 中 typing 中对象的引用
from typing import ( Callable as Callable, Container as Container, Hashable as Hashable, Iterable as ...
- Codeforces Round #542(Div. 2) B.Two Cakes
链接:https://codeforces.com/contest/1130/problem/B 题意: 给定n和 2 * n个数,表示i位置卖ai层蛋糕, 有两个人在1号,必须严格按照1-n的顺序买 ...
- 牛客练习赛42D(性质、数学)
题目传送 就像题解所说的,写几个可以发现有分成四段的性质:第一段是从n开始往下贪,第二段是个数字,第三段……卧槽好吧真难描述. 然后发现这个数据量可达1e9,所以考虑“二分确定序列+数学计算”的方式解 ...
- Codeforces Round #390 (Div. 2) A
One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into sev ...
- CentOS7下使用Docker容器化.net Core 2.2
一.使用 yum 安装(CentOS 7下) Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 una ...
- centos7安装iptables
使用CentOS 7时发现使用iptables防火墙时提示错误Unit iptables.service failed to load,意思是防火墙运行启动失败了,那么要如何处理呢. 一直用Cen ...
- 满足java对redis的所有操作,token,验证码过期时间等
很实用 链接在此 https://www.cnblogs.com/edisonfeng/p/3571870.html System.out.println("=============== ...
- IDEA Maven无法添加依赖到项目中
IDEA--------->File-------->Setting------------>Maven 勾上即可,OK啦! 完美解决了
- vagrant教程
http://blog.smdcn.net/article/1308.html http://ninghao.net/blog/1566 如何定制一个自己的 vagrant box: https:// ...