VUE3 原生JS上传图片(支持多张上传)并实现预览删除功能

<template>
  <!-- 上传后显示 -->
  <div id="look">
    <div id="look_img" v-for="(item, index) in imgSrc" :key="index">
      <img :src="item" alt="">
      <div id="look_event">
        <img src="https://s1.ax1x.com/2022/08/08/vMEtPO.png" alt="" title="点击查看" @click="look(index)">
        <img src="https://s1.ax1x.com/2022/08/08/vMEaxH.png" alt="" title="点击删除" @click="deletes(index)">
      </div>
    </div>
    <!-- 上传图片按钮 -->
    <div id="demo" v-show="uploadnum">
      <input type="file" id="demo_file" accept="image/png,image/gif,image/jpeg" multiple @change="update($event)">
      <img src="https://s1.ax1x.com/2022/08/08/vMEwMd.png" alt="" id="demo_img">
    </div>
  </div>


  <!-- 图片预览 -->
  <div id="preview" v-if="show" @click="() => { show = false }">
    <div id="preview_close">
      <img src="https://s1.ax1x.com/2022/08/08/vMEURe.png" alt="" title="关闭" @click="() => { show = false }">
    </div>
    <div id="preview_last" v-if="pvwWhere != 0" @click.stop="previewLast()">
      <img src="https://s1.ax1x.com/2022/08/08/vMEBqI.png" alt="" title="上一张">
    </div>
    <div id="preview_next" v-if="pvwWhere != imgSrc.length - 1" @click.stop="previewNext()">
      <img src="https://s1.ax1x.com/2022/08/08/vMErZt.png" alt="" title="下一张">
    </div>
    <img :src="pvwSrc" alt="">
  </div>






</template>

<script>

import { ref } from "vue";
import { reactive } from "vue";


export default {
  setup() {

    const imgSrc = ref([]);//已上传图片数组
    const arrLength = ref(9);//上传图片数量
    const uploadnum = ref(true);//控制上传按钮的显示隐藏
    const show = ref(false);//控制预览图片遮罩层显示隐藏
    const pvwSrc = ref(null);//预览图片地址
    const pvwWhere = ref(0);//选择哪一张进行预览以及控制上一张下一张



    const update = (e) => {
      let file = e.target.files;
      let filesLength = arrLength.value - imgSrc.value.length;
      for (let i = 0; i < filesLength; i++) {
        let img = new FileReader();
        img.readAsDataURL(file[i]);
        img.onload = ({ target }) => {
          imgSrc.value.push(target.result); //将img转化为二进制数据
          panduan();
        };
      };
    };
    //判断照片数量是否满足规定数量;满足则隐藏上传按钮
    const panduan = () => {
      if (imgSrc.value.length >= arrLength.value) {
        uploadnum.value = false;
      } else {
        uploadnum.value = true;
      }
    };
    panduan();
    //删除图片
    const deletes = (i) => {
      imgSrc.value.splice(i, 1);
      panduan();
    };
    //图片预览
    const look = (i) => {
      console.log(imgSrc.value);
      pvwWhere.value = i;
      show.value = true;
      pvwSrc.value = imgSrc.value[pvwWhere.value]
    };
    //预览:上一张功能
    const previewLast = () => {
      pvwWhere.value--;
      pvwSrc.value = imgSrc.value[pvwWhere.value]
    };
    //预览:下一张功能
    const previewNext = () => {
      pvwWhere.value++;
      pvwSrc.value = imgSrc.value[pvwWhere.value]
    }

    return {
      update,
      imgSrc,
      arrLength,
      uploadnum,
      deletes,
      look,
      show,
      pvwSrc,
      pvwWhere,
      previewLast,
      previewNext,
    }
  }
}
</script>


<style>
#demo {
  width: 20vh;
  height: 20vh;
  position: relative;
  border: 3px dashed #dcdcdc;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 1em;
  margin-top: 1em;
}

#demo_file {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

#demo_img {
  display: block;
  width: 50%;
  height: 50%;
}

#look {
  width: 70vh;
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
  align-items: center;
}

#look_img {
  width: 20vh;
  height: 20vh;
  margin-left: 1em;
  margin-top: 1em;
  display: flex;
  justify-content: space-around;
}

#look_img img {
  display: block;
  width: 20vh;
  height: 20vh;
  cursor: pointer;
}

#look_event {
  background: rgba(0, 0, 0, 0.6);
  width: 20vh;
  height: 0px;
  position: absolute;
  transition: 1s;
  display: flex;
  justify-content: center;
  align-items: center;
}

#look_event img {
  display: block;
  width: 2em;
  height: 0em;
  cursor: pointer;
}

#look_img:hover #look_event {
  height: 20vh;
  /* opacity: 50%; */
}

#look_img:hover #look_event>img {
  height: 2em;
}

#preview {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
}

#preview img {
  width: 40%;
}

#preview_close {
  position: absolute;
  top: 4vh;
  right: 0;
  display: flex;
  justify-content: center;
}

#preview_last {
  position: absolute;
  left: 0;
  top: 50%;
  display: flex;
  justify-content: center;
}

#preview_next {
  position: absolute;
  right: 0;
  top: 50%;
  display: flex;
  justify-content: center;
}
</style>

如有错误或问题,欢迎大佬在评论指出~

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是基于Vue3和原生Canvas实现抖音头像抠图和的代码示例: 1. 在Vue3中引入Canvas 在Vue3中,我们可以通过在template中添加canvas元素来引入Canvas,然后通过ref属性获取Canvas的实例。代码如下: ```html <template> <canvas ref="canvas"></canvas> </template> ``` 2. 实现头像抠图功能 首先,我们需要获取用户上的头像图片,并创建一个新的Canvas元素来处理图片。代码如下: ```javascript import { ref } from 'vue' export default { setup() { // 获取Canvas实例 const canvasRef = ref(null) // 处理上图片 const handleImageUpload = (event) => { const file = event.target.files[0] if (file) { const reader = new FileReader() reader.readAsDataURL(file) reader.onload = () => { const img = new Image() img.src = reader.result // 创建新的Canvas元素 const canvas = canvasRef.value const ctx = canvas.getContext('2d') img.onload = () => { const width = img.width const height = img.height canvas.width = width canvas.height = height // 将图片绘制到Canvas中 ctx.drawImage(img, 0, 0, width, height) // 实现头像抠图功能 // ... } } } } return { canvasRef, handleImageUpload } } } ``` 接下来,我们需要实现头像抠图的功能。首先,我们需要使用Canvas的getImageData方法获取Canvas中的像素数据。代码如下: ```javascript // 获取Canvas中的像素数据 const imageData = ctx.getImageData(0, 0, width, height) const pixels = imageData.data ``` 然后,我们可以遍历像素数据,判断每个像素的透明度是否大于某个阈值,如果大于阈值则将该像素的透明度设为255,否则将透明度设为0。这样,就可以把头像部分的像素点都变成不透明的,从而实现头像抠图的效果。代码如下: ```javascript // 实现头像抠图功能 const threshold = 200 // 阈值 for (let i = 0; i < pixels.length; i += 4) { const alpha = pixels[i + 3] if (alpha > threshold) { pixels[i + 3] = 255 } else { pixels[i + 3] = 0 } } ctx.putImageData(imageData, 0, 0) ``` 3. 实现头像功能 最后,我们需要实现头像功能。首先,我们需要在template中添加一个img元素,用于展示处理后的头像。代码如下: ```html <template> <div> <input type="file" accept="image/*" @change="handleImageUpload"> <canvas ref="canvas"></canvas> <img ref="preview"> </div> </template> ``` 然后,我们需要在handleImageUpload方法中,将处理后的Canvas转换成DataURL,然后将其赋值给img元素的src属性,从而实现头像的效果。代码如下: ```javascript // 将Canvas转换成DataURL const dataURL = canvas.toDataURL() // 将处理后的头像展示在img元素中 const imgPreview = previewRef.value imgPreview.src = dataURL ``` 至此,基于Vue3和原生Canvas实现抖音头像抠图和的代码示例就完成了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值