Vue2 html2canvas实现截图

3 篇文章 0 订阅
1. 安装html2canvas
npm i html2canvas -S
2. ref 绑定截图区域
<template>
  <div class="save-container box clmcenter">
    <!-- 截图区域 -->
    <div class="savebox clmstart" ref="cutBox" v-show="!imgshow">
      <img src="../assets/img/logo2.png" class="sylogo2">
      <img :src="usercutimg" class="usercutimg" @load="cutimgloaded">
    </div>
    <!-- 最终生成的截图 -->
    <img :src="finalImg" class="finalimg" v-show="imgshow">    
    <p class="savetip">长按图片保存</p>
  </div>
</template>
3. 截图
<script>
import { Toast } from "vant";
import html2canvas from "html2canvas";
export default {
  data () {
    return {
      usercutimg: null,
      finalImg: null,
      imgshow: false,
    }
  },
  mounted () {
    this.usercutimg = this.$store.state.userimgH    
  },
  methods: {
    // 用户上传的图片加载完成后执行截图
    cutimgloaded () {
      this.$nextTick(() => {
        this.cutPicture(this.$refs.cutBox)
      })
    },
    //截图方法
    cutPicture (cutbox) {
      // 等待提示
      Toast.loading({
        message: "照片合成中,请稍后...",
        forbidClick: true,
        duration: 0, //值为 0 时,toast 不会消失
        overlay: true, //是否显示背景遮罩层
      });
      const shareContent = cutbox;
      const width = shareContent.offsetWidth;
      const height = shareContent.offsetHeight;
      const canvas = document.createElement("canvas");
      const scale = 2;
      // 宽高扩大 2 倍 处理图片模糊
      canvas.width = width * scale;
      canvas.height = height * scale;
      canvas.getContext("2d").scale(scale, scale);
      const options = {
        width: width,
        height: height,
        canvas: canvas,
        useCORS: true,
        logging: true,
        scale: 1,
        dpi: 300,
      };
      html2canvas(shareContent, options).then((canvas) => {
        const context = canvas.getContext("2d");
        context.scale(scale, scale)
        context.mozImageSmoothingEnabled = false;
        context.webkitImageSmoothingEnabled = false;
        context.msImageSmoothingEnabled = false;
        context.imageSmoothingEnabled = false;
        // 生成图片地址
        const imgUrl = canvas.toDataURL();
        this.finalImg = imgUrl;
        this.imgshow = true
        // 清除提示
        Toast.clear()
      });
    },
  }
}
</script>

3. 样式部分
<style lang="less" scoped>
img {
  pointer-events: none;
}
.save-container {
  .logo {
    width: 42%;
  }
  .savebox,
  .finalimg {
    position: relative;
    width: 100%;
    height: 421px;   
    .sylogo2 {
      position: absolute;
      bottom: 4rem;
      right: 6.5rem;
      width: 1.3%;
      // transform: rotate(90deg);
    }
    .usercutimg {
      width: 100%;
    }
  }
  .finalimg {
    pointer-events: auto;
  }
  .savetip {
    font-size: 1.8rem;
    color: #000000;
    margin: 1rem;
  }
}

</style>

html2canvas缺陷之一:无法捕捉css变量(场景:页面中的图片需要进行放大缩小移动旋转之后截图时)
替代方案:html-to-image

Demo:

// 1. 安装
npm i html-to-image -S
// 2. 引入
import { toPng } from "html-to-image";
// 使用
...
methods:{
  // 点击按钮
  toUpload(){
    // 调用截图方法 cutBox截图区域
    this.cutPicture(this.$refs.cutBox);
  },
  // html-to-image 截图
  cutPicture(cutbox) {
      Toast.loading({
        message: "照片合成中,请稍后...",
        forbidClick: true,
        duration: 0, //值为 0 时,toast 不会消失
        overlay: true, //是否显示背景遮罩层
      });
      return new Promise((resolve, reject) => {
        try {
          // 解决ios白屏问题 pixelRatio像素比率 值越大图片越清晰
          toPng(cutbox, { useCORS: true, pixelRatio: 3, quality: 1 }).then(
            () => {
              toPng(cutbox, { useCORS: true, pixelRatio: 3, quality: 1 }).then(
                () => {
                  toPng(cutbox, {
                    useCORS: true, // 跨域
                    pixelRatio: 3,
                    quality: 1,
                  })
                    .then((url) => {
                      this.finalImg = url;
                      this.btnshow = false;
                      this.savetipsflag = true;
                      this.imgshow = true;
                      Toast.clear();
                      resolve();
                    })
                    .catch((err) => {
                      Toast.clear();
                      Toast("截图生成失败,请稍后再试!");
                      reject(err);
                    });
                }
              );
            }
          );
        } catch (err) {
          reject(err);
        }
      });
    },
}
...
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值