使用html2canvas将页面转成图,用canvas2image下载
之前在网上找到一个用 <a> 的 download属性下载的方法,确实很简单也很方便,但此方法不支持IE,无奈又找到另一方法。
使用html2canvas将页面转成图,用canvas2image下载
本例使用的版本:
[email protected][email protected]
安装
npm install html2canvas canvas2image --save
引入
import html2canvas from 'html2canvas'; require('./canvas2image.js');
canvas2image.js 需要改动一下,以便绑在window上
(function($){ Canvas2Image = function () { ... } }
_dataURL 可以用来回显,Canvas2Image.saveAsPNG(canvas) 会直接下载。
Canvas2Image提供了下面几种方法:
Canvas2Image.saveAsImage(canvasObj, width, height, type) Canvas2Image.saveAsPNG(canvasObj, width, height) Canvas2Image.saveAsJPEG(canvasObj, width, height) Canvas2Image.saveAsGIF(canvasObj, width, height) Canvas2Image.saveAsBMP(canvasObj, width, height) Canvas2Image.convertToImage(canvasObj, width, height, type) Canvas2Image.convertToPNG(canvasObj, width, height) Canvas2Image.convertToJPEG(canvasObj, width, height) Canvas2Image.convertToGIF(canvasObj, width, height) Canvas2Image.convertToBMP(canvasObj, width, height)
在vue中可以用$refs 确定DOM;
backgroundColor: null,此句可使转出的图没有白边
methods: { toImage() { let _this = this; html2canvas(this.$refs.index,{ backgroundColor: null }).then((canvas) => { let _dataURL = canvas.toDataURL("image/png"); // _this.dataURL = _dataURL; Canvas2Image.saveAsPNG(canvas) }); },
相关推荐
songfens 2020-07-04
MIKUScallion 2020-06-11
songfens 2020-03-20
northwindx 2020-01-04
jinxiutong 2019-12-27
大地飞鸿 2019-12-24
songfens 2019-12-06
飞翔的鱼 2019-11-17
大地飞鸿 2019-11-08
fanbih 2018-09-11
songfens 2019-10-21
yixiaof 2019-09-05
blair 2019-06-10
lanseguhui 2019-07-01
church 2019-07-01
lanseguhui 2019-07-01
yaosir 2019-07-01
yixiaof 2019-06-30