vue2:实现微信截图(复制图片)在项目内可粘贴,且图片清晰度无损(代码可直接用)

功能描述:

1.在vue2中实现截图粘贴功能且粘贴的图片宽高自适应容器设置的宽高,只允许在容器内粘贴一张图片,若容器内有图片,粘贴其他图片则自动替代新粘贴的图片.

2.清除容器内图片

3.粘贴的图片预览,且可通过鼠标滚轮缩放预览图

<template>
  <div>
    <div id="container" @paste="onPaste" style="width: 300px; height: 200px; border: 1px solid black;">
      <input v-if="inputVal" style="height: 100%; width: 100%;" type="text">
      <img :src="imageURL" v-if="imageURL" style="width: 100%; height: 100%; object-fit: cover;">
    </div>
    <button @click="clearImage">清除图片</button>
    <button @click="openModal">预览原图</button>
    <!-- 预览页面可鼠标滚轮缩放预览图片 -->
    <div v-show="showModal" class="modal" @click="closeModal" @wheel="onMouseWheel">
      <img :src="imageURL" v-if="imageURL" :style="{ transform: `scale(${scale})` }">
      <button @click="closeModal">关闭</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageURL: null,
      showModal: false,
      scale: 1,
      inputVal: true,
    };
  },
  methods: {
    // 监听粘贴事件
    onPaste(e) {
      this.inputVal = false
      const items = (e.clipboardData || window.clipboardData).items;
      let blob = null;
      for (let i = 0; i < items.length; i++) {
        if (items[i].type.indexOf('image') === 0) {
          blob = items[i].getAsFile();
        }
      }
      console.log(blob);
      // 如果粘贴的内容不是图片
      if (blob == null) {
        alert('请先截图或复制图片')
        return
      }
      // 如果粘贴的内容是图片
      if (blob !== null) {
        const reader = new FileReader();
        reader.onload = (e) => {
          // 更新图片URL
          this.imageURL = e.target.result;
        };
        reader.readAsDataURL(blob);
      }
    },
    clearImage() {
      this.imageURL = null;
      this.inputVal = true
    },
    openModal() {
      this.showModal = true;
    },
    closeModal() {
      this.showModal = false;
    },
    onMouseWheel(e) {
      // 检测滚轮方向
      const delta = Math.sign(e.deltaY);
      // 根据滚轮方向调整缩放比例
      if (delta > 0) {
        this.scale -= 0.1;
      } else if (delta < 0) {
        this.scale += 0.1;
      }

      // 限制缩放比例的范围
      if (this.scale < 0.1) {
        this.scale = 0.1;
      } else if (this.scale > 3) {
        this.scale = 3;
      }
    }
  }
};
</script>

<style>
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.modal img {
  max-width: 100%;
  max-height: 100%;
  transition: transform 0.3s ease;
}

.modal button {
  position: absolute;
  top: 10px;
  right: 10px;
}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue H5中调用微信分享,首先需要引入微信官方的JS-SDK库。在Vue项目的index.html文件中,可以在<head>标签内添加如下代码: ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> ``` 然后,在Vue组件的created生命周期钩子函数中,使用Vue的axios或者其他网络请求库向后端请求获取微信配置信息。 ```javascript created() { this.fetchWechatConfig(); }, methods: { async fetchWechatConfig() { const response = await axios.get('/api/wechat/config'); // 向后端请求微信配置信息 const { appId, timestamp, nonceStr, signature } = response.data; // 将配置信息存储到全局变量中 this.$store.commit('setWechatConfig', { appId, timestamp, nonceStr, signature }); this.initWechatSDK(); }, initWechatSDK() { const { appId, timestamp, nonceStr, signature } = this.$store.state.wechatConfig; wx.config({ appId, timestamp, nonceStr, signature, jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'], // 配置需要使用的微信API }); // 进行微信SDK的初始化 wx.ready(() => { this.shareToWechat(); }); }, shareToWechat() { const shareData = { title: '分享标题', link: '分享链接', imgUrl: '分享图片链接', desc: '分享描述', }; // 配置分享的具体内容 wx.onMenuShareTimeline(shareData); // 分享到朋友圈 wx.onMenuShareAppMessage(shareData); // 分享给好友 }, }, ``` 上述代码中,我们通过axios库向后端请求了微信配置信息,并将其存储到Vuex的全局状态中。然后,我们使用微信JS-SDK中的`wx.config`方法进行微信验证和初始化配置。一旦初始化成功,`wx.ready`回调函数将会被触发,我们在该函数中调用`shareToWechat`方法来配置分享的具体内容。 在`shareData`对象中,我们可以自定义分享的标题、链接、图片和描述等信息,然后使用`wx.onMenuShareTimeline`方法配置分享给朋友圈的内容,使用`wx.onMenuShareAppMessage`方法配置分享给好友的内容。 最后,我们可以在Vue组件中调用`shareToWechat`方法来触发微信分享。 注意:在实际开发中,需要根据微信的API文档和后端提供的接口来进行相应的调整和配置。以上代码仅为示例,具体实现方式可能会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值