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

首先,先来科普一下微信网页识别二维码原理:截屏识别,当客户端发现用户在网页的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. 洛谷 P4137 Rmq Problem / mex

    https://www.luogu.org/problemnew/show/P4137 只会log^2的带修主席树.. 看了题解,发现有高妙的一个log做法:权值线段树上,设数i对应的值ma[i]为数 ...

  2. 洛谷 P2623 物品选取

    https://www.luogu.org/problemnew/show/P2623 https://www.luogu.org/blog/test-1/solution-p2623 重点就是甲类物 ...

  3. python 生成器与迭代器

    #! /usr/bin/env python# -*- coding:utf-8 -*- def xrange(n): num = 0 while True: if num > n: retur ...

  4. python shutil

    #! /usr/bin/env python# -*- coding:utf-8 -*-# shutil 高级的文件.文件夹.压缩包处理模块import shutil #1.copyfileobj(源 ...

  5. python对文件的压缩解压

    python自带的zipfile的模块支持对文件的压缩和解压操作 zipfilp.ZipFile 表示创建一个zip对象 zipfile.ZipFile(file[, mode[, compressi ...

  6. 关于 ie8不兼容的一些方法

    ie8 不兼容的方法 $(function(){ //添加数组IndexOf方法 if (!Array.prototype.indexOf){ Array.prototype.indexOf = fu ...

  7. js实现对象或者数组深拷贝

    今天遇到个问题,就是vue绑定的数组在push中所有的数组都会跟着改变.这个主要是因为 JavaScript中对象或者数组等引用类型,直接拷贝,改变一个另外一个也会改变: 有个简单的方法就是先转换为字 ...

  8. 禁用和关闭ECSHOP缓存

    ECSHOP的缓存机制从一定程度上可以减少ECSHOP反复读取数据库的几率,从而一定程度上降低服务器负担,提高访问速度. 但是启用缓存机制,对一些新手站长也有不利的地方.我就遇到很多新手站长经常问,我 ...

  9. ubuntu 下 docker安装

    1移除以前安装docker sudo apt-get remove docker docker-engine docker-ce docker.io 2 安装包以允许apt通过HTTPS使用存储库 s ...

  10. 爬虫requests库的基本用法

    需要注意的几个点: 1.后面的s是一个虚拟目录 2.url后面不用加问号,发起请求的时候会自动帮你加上问号 get_url = 'http://www.baidu.com/s' 3. url的特性:u ...