vue 提升html2canvas渲染速度

提升dom转化成图片 主要是html2canvas里的ignoreElements属性

<div class="qrDiv" id="qrDiv" ref="contentToConvert" v-loading="qrLoading">
        <div>我是标题</div>
        <div class="tips">
          <span class="pr20">{{ 1111111 }}</span>
         </div>
        <canvas ref="qrCodeCanvas"></canvas>
      </div>
      <div class="flex-around button">
        <el-button class="fill"  round @click="customDownClick" :loading="savePicLoading" v-clickAgain
          >保存图片</el-button
        >
</div>

点击事件

async customDownClick(){
      this.savePicLoading=true;
      // 获取要转换的 div 元素
      const element = this.$refs.contentToConvert;
      let tags = ['HEAD','LINK','STYLE'] // 必须保留的节点,保持样式效果等
      try {
        // 使用 html2canvas 将 div 转换为 canvas
        const canvas = await html2canvas(element,{
          element:element,
          scale:1,
          useCORS:true,
          logging: false,
          ignoreElements:e=>{
            // 忽略带有 'ignore-me' 类名的元素.
           if(e.contains(element) || element.contains(e) || e.getAttribute('data-html2canvas') != null || tags.indexOf(e.tagName)!=-1){
                 return false
             }
             return true
          }
        })
          
        // 将 canvas 转换为图片 URL
        const image = canvas.toDataURL('image/png');
        // 创建一个临时链接元素
        const link = document.createElement('a');
        link.href = image;
        link.download = `${this.stationInfo.oilStationNameAbbr}${moment().format("YYYYMMDDHHmmss")}.png`; // 设置下载文件名
        // 触发下载
        link.click();
        this.savePicLoading=false;
        this.dialogVisible=false;
      } catch (error) {
        console.error('Error converting div to image:', error);
        this.savePicLoading=false;
      }
    }

速度将会得到很大优化…

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值