WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

html2canvas 图片偏移+不全(vue,附有解决方法)

更新时间:2021-06-28 15:14 作者:雨落青城点击:

下面是我原来的写法,这个写法原来并没有问题,但后面出现图片不完整+ 白屏+ 偏移(下面附有解决方法)

要截图的区域(截取
          <div ref="imageToFile" id="imageToFile">
            <van-col :span="24" class="topPage" :style="{backgroundImage:'url(' + backImg + ')'}" >    
              <img class="vanimage" :src="img | getImgById">
              <div v-if="titleshow" class="redTitle">{{title}}</div>
            </van-col>
            <div style="clear:both;height:1px;width:100%; overflow:hidden; margin-top:-1px;"></div>
          </div>
事件(截取          
        setTimeout(()=>{
            html2canvas(this.$refs.imageToFile, {
              width: this.$refs.imageToFile.firstChild.offsetWidth,
              height: this.$refs.imageToFile.firstChild.offsetHeight,
              useCORS: true
            }).then((canvas) => {// 第一个参数是需要生成截图的元素,第二个是自己需要配置的参数,宽高等
              this.images[0] = canvas.toDataURL('image/png');
            })
          }, 200)    

错误图片:
在这里插入图片描述
——— 原因可能是html2canvas留的坑————
解决方法:在生成图片之前先进行操作

 		  window.scroll(0,0) // 首先先顶部
          const targetDom = document.querySelector("#imageToFile") // 获取要截图的元素
          const copyDom = targetDom.cloneNode(true) // 克隆一个
          copyDom.style.width = targetDom.scrollWidth + 'px'
          copyDom.style.height = targetDom.scrollHeight + 'px'
          document.body.appendChild(copyDom) // 添加

          setTimeout(()=>{
            html2canvas(copyDom, {
              width: targetDom.scrollHeight,
              height: targetDom.scrollHeight,
              useCORS: true
            }).then((canvas) => {
              document.body.removeChild(copyDom) // 用完要删除
              this.images[0] = canvas.toDataURL('image/png');
            })
          }, 200)

但做完之后发现样式变了,这是因为我把class写在大的class里,把class单独移动出来即可。
(class错误在大的class里时,完成图的东西没有样式,比如右边就没有红色背景等全部样式)
完成图:
在这里插入图片描述

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容