github/download:https://github.com/niklasvh/html2canvas/releases

参考文章:基于html2canvas实现网页保存为图片及图片清晰度优化

html2canvas 可以将html页面保存为图片,相当于进行截图,可以应用于一些活动H5的海报生成。

可以下载好文件通过script标签引入,或者通过npm安装

npm install html2canvas

用法:

基于html2canvas.js可将一个元素渲染为canvas,只需要简单的调用html2canvas(element[, options]);即可。下列html2canvas方法会返回一个包含有<canvas>元素的promise

//targetEl 是需要截图的元素,可以是某一个DIV,也可以是当前整个document,options是参数项,可配置宽高之类的,也可省略不传,返回一个canvas的dom对象
html2canvas(targetEl,options).then(function(canvas) {
document.body.appendChild(canvas);
}); //之前做一个生成海报的H5的例子代码
var poster = document.querySelector('#wrap') //要生成海报的DIV,最外层的
var width = poster.offsetWidth;
var height = poster.offsetHeight;
var canvas = document.createElement("canvas");
var scale = 2;
canvas.width = width * scale;
canvas.height = height * scale;
canvas.getContext("2d").scale(scale, scale);
var opts = {
scale: scale,
canvas: canvas,
width: width,
height: height ,
backgroundColor:'transparent'
};
//生成页面截图
html2canvas(poster,opts).then(function(canvas) {
var context = canvas.getContext('2d');
var img = new Image();
img.src = canvas.toDataURL()
img.id = 'poster'
poster.appendChild(img); //生成海报插入body , 海报是透明的 ,层级最高, 盖在容器上面
});
 

转为图片: html2canvas是返回的一个canvas,要保存为图片的话就要转为img,可以通过canvas.toDataURL()方法将canvas转为base64

跨域设置:如果网页有跨域的图片会影响到截图图片的生成,可以将html2canvas 的 userCORS 改为true

var opts = {useCORS: true};

html2canvas(element, opts);

html2canvas JS截图插件的更多相关文章

  1. IText&Html2canvas js截图 绘制 导出PDF

    Html2canvas JS截图 HTML <div id="divPDF"> 需要截图的区域 </div> JS <script src=" ...

  2. js实现html转pdf+html2canvas.js截图不全的问题

    最近做项目中遇到要把整个页面保存为PDF文件,网上找了一下实现的方法都是 html2canvas.js+jsPdf.js 来实现.实现的过程是 先用html2canvas.js把html页面转成图片, ...

  3. html2canvas html截图插件

    以下我总结了一些注意事项,在代码中注释了,仅供参考. html2canvas.js点击付:完整使用的demo ,如下: <!DOCTYPE html><html lang=" ...

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

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

  5. html2canvas.js网页截图功能

    需求:将网页生成图片,用户自行长按图片进行保存图片,再分享朋友圈.其中,都可识别图中的二维码.(二维码过小会识别不出) 首先,先来科普一下微信网页识别二维码原理:截屏识别,当客户端发现用户在网页的im ...

  6. 使用html2canvas.js实现页面截图并显示或上传

    最近写项目有用到html2canvas.js,可以实现页面的截图功能,但遭遇了许多的坑,特此写一篇随笔记录一下. 在使用html2canvas时可能会遇到诸如只能截取可视化界面.截图没有背景色.svg ...

  7. js截图及绕过服务器图片保存至本地(html2canvas)

    今天要分享的是用html2canvas根据自己的需求生成截图,并且修复html2canvas截图模糊,以及绕过服务器图片保存至本地. 只需要短短的几行代码,就能根据所需的dom截图,是不是很方便,但是 ...

  8. 微信图片生成插件,页面截图插件 html2canvas,截图失真 问题的解决方案

    html2canvas 是一个相当不错的 JavaScript 类库,它使用了 html5 和 css3 的一些新功能特性,实现了在客户端对网页进行截图的功能.html2canvas 通过获取页面的 ...

  9. JS截图(html2canvas)

    JS截图(html2canvas) • 引入js <script type="text/javascript" src="js/html2canvas.js&quo ...

随机推荐

  1. 常用Oracle操作语句

    --常用的字段类型有:varchar2,char,nchar,date,long,number,float,BLOB,CLOB --添加表字段 ); --修改表字段 ); --删除表字段 alter ...

  2. h5判断是否为iphonex

    js移动端页面判断是否是iphoneX 转自https://blog.csdn.net/weixin_39924326/article/details/80352929 function isIPho ...

  3. java在jvm虚拟机中是如何实现多态的?

    原文地址:https://blog.csdn.net/huangrunqing/article/details/51996424 众所周知,多态是面向对象编程语言的重要特性,它允许基类的指针或引用指向 ...

  4. Failed to read artifact descriptor for org.springframework.cloud:spring-cloud-starter-config:jar:unk

    <dependencyManagement> <dependencies> <dependency> <groupId>org.springframew ...

  5. PAT甲级——A1139 First Contact【30】

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  6. USACO 2013 January Silver Painting the Fence /// oj23695

    题目大意: 输入n,k :n次操作 找到覆盖次数在k及以上的段的总长 一开始位置在0 左右活动范围为1-1000000000 接下来n行描述每次操作的步数和方向 Sample Input 6 22 R ...

  7. 13-1-return

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. js 获取指定字符串个数

    参考:https://blog.csdn.net/maqinqin/article/details/5323824 function getStrCount(scrstr,armstr) { //sc ...

  9. netty 使用Java序列化

    SubscribeReq package com.zhaowb.netty.ch7_1; import java.io.Serializable; public class SubscribeReq ...

  10. 2019-8-31-PowerShell-通过-WMI-获取系统安装软件

    title author date CreateTime categories PowerShell 通过 WMI 获取系统安装软件 lindexi 2019-08-31 16:55:58 +0800 ...