Vue<移动端截屏保存当前图片>

功能实现看图:

1.点击保存本地

IMG_20190515_162920.jpg

2.长按选择保存

IMG_20190515_162834.jpg

3.保存

Screenshot_2019-05-15-16-26-26-632_com.tencent.mm.png

🔥解决html2canvas截图不全的问题

首先:

npm install --s html2canvas

代码如下:

<template>
<div >
<!-- 此处是要截取的图片 -->
   <div id="imgDownload" ref="QRcodeSrcImg"  @click="maskShow=false">
        <div class="box">
          <canvas id="QRCode"></canvas>
          <p class="text">扫描二维码</p>
        </div>
      </div>
<!-- 此处是储存截取后的图片 -->
    <div class="photoShow" v-if="photoShow">
         <div id="photo">
            <p>长按图片保存</p>
            <img :src="photoUrl" id="img" alt="">
           <!-- <span>长按图片保存和分享</span> -->
         </div>
     </div>
<!-- 此处是保存到本地的方法 -->
      <div class="bottom" @click="imgDownload()">
        <div style="text-align: center;">
          <p style="font-size:1.4rem;margin-top:10px;color:#a5a4a4;;">保存到本地</p>
        </div>
      </div>

</div>
</template>
<script>
import html2canvas from "html2canvas"
export default {
data(){
   return{
       photoUrl:"",
      photoShow:false
    }
  },
methods: {
     // 图片保存方法
    imgDownload() {
      let _this = this;
      _this.photoShow = true;
      // 声明一个画布元素,存储需下载图片范围
      let canvas = document.createElement("canvas");
      // 获取需下载范围元素
      let img = this.$refs["QRcodeSrcImg"];
      // 图片高度
      var w = parseInt(window.getComputedStyle(img).width);
      // 图片宽度
      var h = parseInt(window.getComputedStyle(img).height);
      // 声明画布宽高
      canvas.width = w;
      canvas.height = h;
      canvas.style.width = w + "px";
      canvas.style.height = h + "px";
      // console.log(w, h);
      // let context = canvas.getContext("2d");
      // context.scale(2, 2);
      // 利用 html2canvas 下载 canvas
      html2canvas(img, { canvas: canvas }).then(function(canvas) {
             _this.photoUrl = canvas.toDataURL()
      });
    }
  }
}
</script>
<style lang="scss" scoped>
.photoShow{
    background: rgba(0, 0, 0, 0.616);
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    #photo{
     //保存图片容器大小
        width: 250px;
        img{
            display: block;
            width: 100%;
        }
    }
}
#imgDownload{
    width: 300px;
    height: 300px;
    background: red;
}
</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,554评论 0 17
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,822评论 0 10
  • NPM NPM 是随同 Node 一起安装的包管理工具,能解决 Node 代码部署上的很多问题,常见的使用场景有以...
    heyi_let阅读 2,607评论 0 2
  • 33、JS中的本地存储 把一些信息存储在当前浏览器指定域下的某一个地方(存储到物理硬盘中)1、不能跨浏览器传输:在...
    萌妹撒阅读 2,112评论 0 2
  • 什么是 NPM npm之于Node,就像pip之于Python,gem之于Ruby,composer之于PHP。 ...
    ihoey阅读 6,268评论 2 36