vue-cropper插件使用

需求拍照之后裁剪照片,然后搜到了vue-cropper功能还是很强大的

首先是使用方式

组件内使用
import { VueCropper }  from 'vue-cropper' 
components: {
  VueCropper,
},
全局使用
import VueCropper from 'vue-cropper' 
Vue.use(VueCropper)

html中

<vueCropper
          ref="cropper"
          :img="option.img"
          :outputSize="option.outputSize"
          :info="option.info"
          :infoTrue="option.infoTrue"
          :outputType="option.outputType"
          :centerBox="option.centerBox"
          :autoCrop="option.autoCrop"
          :autoCropWidth="option.autoCropWidth"
          :autoCropHeight="option.autoCropHeight"
          :fixedBox="option.fixedBox"
          :fixedNumber="option.fixedNumber"
          :canMoveBox="option.canMoveBox"
          :high="option.high"
          :enlarge="option.enlarge"
          :canMove="option.canMove"
          :canScale="option.canScale"
          @imgLoad="imgLoad" 
          @cropMoving="cropMoving"
        ></vueCropper>

上面的绑定属性基本不用说,npm上都有介绍,主要是讲一下我在开发中遇到的问题,拍完照之后插件在画图的时候会有一个时间,这个时间页面上会显示空白,这时候就需要一个loading,就用@imgLoad这个属性,这是在图片渲染出来的回调函数,在methods定义一下

imgLoad() {
      // cropper图片加载成功会触发
      this.mask = false;  //我这是弄了loading的flag,在渲染出来之后loading不显示
    },
拍完照之后要截图的代码
handleUse() {
      this.isLoading = true;
      this.$refs.cropper.startCrop(); //开始截图
      this.$refs.cropper.getCropData(res => { //得到截图的数据
        this.scanImg = res;
        let reg = new RegExp("data:image/jpeg;base64,");
        console.warn(res);
        this.clipImg = res.replace(reg, "");
        this.showImg = true;
        const data = {
          image: this.clipImg,
          coords: this.cropAxis, // 修改坐标格式[x1,y1, x2,y2]
          format: "jpeg",
          // func: "cloth",
          source: "1" // 1 洗衣机,2 护理机
        };
      }
 }

我是用了截图框可移动,后台还需要截图框的坐标,这里就用@cropMoving截图框移动后的回调函数,

// 截图框移动回调函数
    cropMoving(data) {
      // 截图框的左上角 x,y和右下角坐标x,y
      this.cropAxis = [data.axis.x1, data.axis.y1, data.axis.x2, data.axis.y2]
    },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值