其他分享
首页 > 其他分享> > 点击按钮保存当前页面为图片----html2canvas

点击按钮保存当前页面为图片----html2canvas

作者:互联网

npm install --save html2canvas

import html2canvas from 'html2canvas'

  

handleDownload (id) {
      let ele = document.getElementById(id)
      let ignoreElements = ele.querySelector('.chart-download')
      html2canvas(ele, {
        logging: false,
        ignoreElements: (elements) => { //  忽略的ele
          return elements === ignoreElements
        }
      }).then(function (canvas) {
        // canvas转为图片流
        let dataurl = canvas.toDataURL('image/png')
        console.log(dataurl)

        // 非ie下正常,主要原因是ie下a标签不支持download属性
        let donwLink = document.createElement('a')
        donwLink.href = dataurl
        donwLink.download = '统计分析' + new Date().getTime() // 图片名字
        donwLink.click()
        let event = new MouseEvent('click')
        donwLink.dispatchEvent(event)

        // 使用downloadjs兼容ie下下载,https://github.com/rndme/download
        // download(data, strFileName, strMimeType);
        // downloadjs(dataurl, `统计分析${new Date().getTime()}`, 'image/png')
      })
    },

  

标签:----,donwLink,dataurl,ele,html2canvas,let,download,页面
来源: https://www.cnblogs.com/cs122/p/15603904.html