vue实现将iframe保存图片--html2canvas

1、vue导入html2canvas

npm install --save html2canvas

2、vue配置

main.js

import html2canvas from 'html2canvas'

Vue.prototype.$html2canvas = html2canvas

3、iframe

  <iframe  ref="iframe"  src='.。。/index.html'> </iframe>  

4、组件vue代码

 download_iframeimg(){

      var     iframeHtml=this.$refs.iframe.contentWindow

      const iframeScrollY = iframeHtml.document.documentElement.scrollTop

     const iframeScrollX = iframeHtml.document.documentElement.scrollLeft

    var   iframeBody= iframeHtml.document.getElementsByTagName('body')[0]

     //利用 html2canvas库

     var that=this

    that.$html2canvas(iframeBody, {

        useCORS: true,

        width: 812, 

        height: 661,

        x: iframeScrollX,

        y: iframeScrollY

    }).then(function(canvas){        

        let canvasImg = canvas.toDataURL("image/png"); // 转化成 dataurL

        //生成图片并下载 

        var $a = document.createElement('a');

        var type = 'png';

        //图片名称

        $a.download = “aa” + '.' + type;

        $a.target = '_blank';

        $a.href = canvasImg;

        if (typeof MouseEvent === 'function') {

          var evt = new MouseEvent('click', {

            view: window,

            bubbles: true,

            cancelable: false

          });

          $a.dispatchEvent(evt);

        }

    })

   },

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值