有时我们需要实现在浏览器端直接对整个或部分页面进行截屏,比如移动端常见的“长按网页保存为图片”功能。这个借助 html2canvas 这个第三方 js 库即可实现,下面通过样例演示其如何使用。

一、基本介绍

1,什么是 html2canvas
html2canvas 可以通过获取 HTML 的某个元素,然后生成 Canvas,从而让用户保存为图片。
html2canvas 工作原理是将当页面渲染成一个 Canvas 图片,通过读取 DOM 并将不同的样式应用到这些元素上。
html2canvas 不需要来自服务器任何渲染,整张图片都是在客户端浏览器创建。
2,注意事项

当然并不是所有的页面元素都可以进行转换的,下面是不支持的情况:
不支持 iframe
不支持跨域图片(可以先将线上图片转换成 base64,然后用 base64 作为图片路径)
不支持 flash
不支持 transform、transition 过渡、animation 动画(备注:transform 初始布局是可以的,但是不能参与动画类的操作)

3,安装配置

(1)首先到官网将 html2canvas.js 下载到本地。

官网地址:http://html2canvas.hertzen.com/

(2)然后在页面中将其引用即可。

<script type="text/javascript" src="js/html2canvas.js"></script>

二、基本用法

1,将整个页面转成 Canvas

(1)效果图
点击“开始生成”按钮后,会将整个页面渲染成一个 canvas,并将这个 canvas 添加到页面尾部。
右键点击生成的 canvas,可以将其作为图片保存到本地。
原文:JS - 使用 html2canvas 将页面保存成图片(或对指定元素截图)
(2)样例代码

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>hangge.com</title></head><body style="margin:0px"><div id="capture" style="padding: 10px; background: #f5da55; width: 200px;"><h4 style="color: #000; ">欢迎访问 hangge.com</h4></div><button type="button" name="button" onclick="convert()">开始生成</button><br><script type="text/javascript" src="js/html2canvas.js"></script><script type="text/javascript">//开始转换function convert() {html2canvas(document.body).then(canvas => {document.body.appendChild(canvas)});}</script></body>
</html>
2,将指定 DOM 元素转成 Canvas

下面样例只将按钮上方的 div 渲染成 Canvas。
原文使用 html2canvas 将页面保存成图片(或对指定元素截图)

html2canvas(document.querySelector("#capture")).then(canvas => {document.body.appendChild(canvas)
});
3,将 Canvas 转换成 base64 形式

(1)上面样例生成 canvas 后,便直接显示在页面上进行预览。我们也可以将生成的 canvas 转成 base64 形式用于提交到后台,或者作为 元素的 src 属性来显示。
原文使用 html2canvas 将页面保存成图片(或对指定元素截图)

html2canvas(document.querySelector("#capture")).then(canvas => {var imgUrl = canvas.toDataURL("image/png"); // 将canvas转换成img的src流console.log("base64编码数据:", imgUrl);
});

(2)转换时可以设置截图质量(0~1)

html2canvas(document.querySelector("#capture")).then(canvas => {var imgUrl = canvas.toDataURL("image/png", 1); // 此方法可以设置截图质量(0-1)console.log("base64编码数据:", imgUrl);
});

三、进阶用法

1,设置生成的 Canvas 的高度和宽度

html2canvas 在使用时还有许多可选的配置参数,下面样例生成一个 75*75 的 canvas。
原文:JS - 使用 html2canvas 将页面保存成图片(或对指定元素截图)

html2canvas(document.querySelector("#capture"),{
width: 75,
height: 75
}).then(canvas => {
document.body.appendChild(canvas)
});
2,设置 Canvas 的背景色

(1)使用 backgroundColor 这个属性可以设置 canvas 的背景色:
默认值为白色(#ffffff)
如果想要背景透明,可以将其设为 null

(2)下面将背景色改成绿色
原文:JS - 使用 html2canvas 将页面保存成图片(或对指定元素截图)

html2canvas(document.querySelector("#capture"),{width: 240,height: 120,backgroundColor: "#00ff00"
}).then(canvas => {document.body.appendChild(canvas)
});
3,设置放大倍数

(1)使用 scale 属性可以修改渲染时的放大倍数(默认为 1),将其调大可以解决低分辨率设备下生成的图片模糊问题。
(2)下面是当 scale 设置为2时,生成的图片。
原文:JS - 使用 html2canvas 将页面保存成图片(或对指定元素截图)

html2canvas(document.querySelector("#capture"),{scale: 2
}).then(canvas => {document.body.appendChild(canvas)
});
4,指定渲染的 Canvas

如果页面上原先就有个 canvas 元素,我们希望可以将图片绘制在它上面,可以使用 canvas 属性设置。
原文:JS - 使用 html2canvas 将页面保存成图片(或对指定元素截图)

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>hangge.com</title></head><body style="margin:0px"><div id="capture" style="padding: 10px; background: #f5da55; width: 200px;"><h4 style="color: #000; ">欢迎访问 hangge.com</h4></div><button type="button" name="button" onclick="convert()">开始生成</button><br><canvas id="myCanvas" width="220" height="84"></canvas><script type="text/javascript" src="js/html2canvas.js"></script><script type="text/javascript">//开始转换function convert() {html2canvas(document.querySelector("#capture"),{canvas: document.querySelector("#myCanvas")}).then(canvas => {});}</script></body>
</html>
附:自动保存为图片并下载

(1)上面样例如果要将 DOM 元素变为图片保存下来,都是先将其转成 canvas,然后再手动在 canvas 上点击右键,通过“另存为”功能下载到本地。
(2)如果想要实现图片自动生成、自动下载的话可以借助 FileSaver.js 这个第三方库。其具体安装配置可以参考我之前的写的文章:
JS - 使用 FileSaver.js 实现浏览器文件导出

(3)下面是一个简单样例,点击“开始生成”按钮后,可以看到浏览器直接自动下载文件。
原文使用 html2canvas 将页面保存成图片(或对指定元素截图)

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>hangge.com</title></head><body style="margin:0px"><div id="capture" style="padding: 10px; background: #f5da55; width: 200px;"><h4 style="color: #000; ">欢迎访问 hangge.com</h4></div><button type="button" name="button" onclick="convert()">开始生成</button><br><script type="text/javascript" src="js/html2canvas.js"></script><script type="text/javascript" src="js/FileSaver.js"></script><script type="text/javascript">//开始转换function convert() {html2canvas(document.querySelector("#capture")).then(canvas => {//将canvas内容保存为文件并下载canvas.toBlob(function(blob) {saveAs(blob, "hangge.png");});});}</script></body>
</html>

参考链接:http://www.hangge.com/blog/cache/detail_2211.html#

如果有跨域的图片,那么js报错,报错信息如下:

Uncaught SecurityError: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported.
解决方法:
1、利用https://tinypng.com/将图片压缩一下;
2、将图片放在服务器下就不会产生跨域问题了;
公众号:

微信:

使用 html2canvas 将页面保存成图片相关推荐

  1. html保存当前页面为图片,将html页面保存成图片,图片写入pdf的实现方法(推荐)

    需求是一个导出pdf的功能,多方奔走终于实现了,走了不少弯路,而且怀疑现在这个方法仍是弯的. 有个jsPDF 插件可以在前端直接生成pdf,很简便,但不支持IE. 前端: 首先引入  html2can ...

  2. 使用html2canvas,将页面转换成图片的采坑记录(Web/Taro h5)

    使用html2canvas将页面转换成图片的采坑记录 "html2canvas": "^1.4.1","@tarojs/taro": &qu ...

  3. vue 签署文件两张图合并成一张图 h5页面转换成图片并长按保存在本地

    在做让用户在页面签名并把当前页面保存为图片,附上完整代码 (直接复制代码,更改接口地址或删除接口逻辑可直接复用) **准备工作:安装   npm install vue-esign --save 全局 ...

  4. html页面转换成图片的三种方法——canvas、dom-to-image、html2canvas

    html页面转换成图片的三种方法--canvas.dom-to-image.html2canvas canvas绘制网络图片报错(跨域) 使用canvas将html页面转成图片 dom-to-imag ...

  5. 基于html2canvas实现网页保存为图片及图片清晰度优化

    一.实现HTML页面保存为图片 1.1 已知可行方案 现有已知能够实现网页保存为图片的方案包括: **方案1:**将DOM改写为canvas,然后利用canvas的toDataURL方法实现将DOM输 ...

  6. 把html页面保存为图片格式

    把HTML页面保存为图片 这里用到html2canvas库 npm install html2canvas import html2canvas from 'html2canvas'//可以保存整个页 ...

  7. 实现HTML页面保存为图片

    一.实现HTML页面保存为图片 1.1 已知可行方案 现有已知能够实现网页保存为图片的方案包括: 方案1:将DOM改写为canvas,然后利用canvas的toDataURL方法实现将DOM输出为包含 ...

  8. 后台java 使用PhantomJS把echart保存成图片

    后台java 使用PhantomJS把echart保存成图片 项目是在浏览器展示折线,柱状图,使用echarts效果不错.希望能把echarts图形通过后台程序保存成图片或者保存到word中,供客户做 ...

  9. php 文字图片怎么保存为图片,php技术实现加载字体并保存成图片

    下面通过一段代码给大家详解介绍下php技术实现加载字体并保存成图片. // Set the content-type header("Content-type: image/png" ...

最新文章

  1. 其他算法-比例风险回归模型
  2. iOS进阶之底层原理-isa与对象
  3. Linux下Json库的编译及代码测试
  4. 用了虚拟机Linux不能上网,虚拟机Linux不能上网怎么办
  5. Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意
  6. python request timeout_Python - aiohttp请求不断超时(Python - aiohttp requests continuously time out)...
  7. 全栈工程师已经过时?
  8. 【转】简单易懂的Android ROM定制与修改教程
  9. DMTF Redfish 概念介绍
  10. DWM1000模块简介
  11. matlab快速原型开发c语言,快速原型开发
  12. 计算机软件被放进回收站,电脑不能把文件放入回收站只能永久删除怎么办?
  13. 倒水问题python实现
  14. 阿里巴巴短信验证码使用
  15. SaaS订阅收费模式的精髓是:预充值
  16. 'pip' 不是内部或外部命令,也不是可运行的程序或批处理文件。
  17. UVA1595-对称轴
  18. 2021-1-22初学JAVA
  19. IoTeX 对话 高校区块链技术社区:物联网区块链的超级落地应用在哪里?
  20. 把深山红叶装进U盘的方法[转载]

热门文章

  1. WSN final fighting 12.05
  2. Dota2参议院-贪心649-python
  3. 拉结尔6月21日服务器维护,《拉结尔》6月17日更新公告,2周年庆典开启
  4. CAD图纸的版本格式可以进行转换吗?CAD版本转换怎么操作呢?
  5. 一个Linux狂人的语录 (这篇文章值得推广)
  6. android平板装虚荣windows,纠结!两款优秀的二合一平板的艰难选择
  7. 有道获取单词读音api,获取经纬度api以及地图展示
  8. MATLABA数字水印处理技术实现
  9. linux+pci+显卡,PCI、PCI-x,PCI-E兼容以及他们之间的区别详细图解
  10. matlab 画y 1.05 x,Matlab怎么画已知x,y,z的散点图?