前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序引用we-cropper裁切图片

微信小程序引用we-cropper裁切图片

作者头像
明知山
发布2020-09-02 16:41:01
2.2K1
发布2020-09-02 16:41:01
举报
文章被收录于专栏:前端开发随笔前端开发随笔

文档地址 github下载源码直达链接 复制dist文件夹中的 .min.js.wxml文件到项目中

在这里插入图片描述
在这里插入图片描述

我引入的目录结构

在这里插入图片描述
在这里插入图片描述

page页面

代码语言:javascript
复制
<view>
    <view class='avatar'>
        <image mode='scaleToFill' src="{{avatar}}" bindtap='chooseimg'></image>
    </view>
    <view wx:if="{{showWeCropper}}">
        <import src='../../utils/weCropper/we-cropper.wxml' />
        <view class="cropperbg">
            <template is="we-cropper" data="{{...cropperOpt}}" />
            <view class="employ" bindtap="getCropperImage">使用</view>
        </view>
    </view>
</view>
代码语言:javascript
复制
.avatar {
    width: 200rpx;
    height: 200rpx;
    border-radius: 50%;
    overflow: hidden;
}

image {
    width: 100%;
    height: 100%;
}

.cropperbg {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: #000;
}

.cropper {
    background: #000;
}

.employ {
    width: 360rpx;
    height: 80rpx;
    background: #16BA98;
    border-radius: 45rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    margin: 0 auto;
}
代码语言:javascript
复制
import WeCropper from '../../utils/weCropper/we-cropper.min.js'
const device = wx.getSystemInfoSync() // 获取设备信息
const width = device.windowWidth
const height = device.windowHeight - 100;
Page({
  data: {
    showWeCropper: false,
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 300,
        height: 300
      }
    },
    avatar: 'https://sucai.suoluomei.cn/sucai_zs/images/20200516105950-2.png',
  },
  
  onLoad: function (options) {
    this.initWeCropper()
  },

  // 初始化weCropper插件图
  initWeCropper() {
    const {
      cropperOpt
    } = this.data
    this.cropper = new WeCropper(cropperOpt)
      .on('ready', (ctx) => {})
      .on('beforeImageLoad', (ctx) => {
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        wx.hideToast()
      })
  },

  //选择图片
  chooseimg() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          showWeCropper: true
        });
        this.cropper.pushOrign(res.tempFilePaths[0]);
      }
    })
  },

  touchStart(e) {
    this.cropper.touchStart(e)
  },
  touchMove(e) {
    this.cropper.touchMove(e)
  },
  touchEnd(e) {
    this.cropper.touchEnd(e)
  },

  // 获取图片链接
  getCropperImage() {
    this.cropper.getCropperImage((res) => {
      this.setData({
        showWeCropper: false,
        avatar: res,
      })
      console.log(res)
    })
  }
})
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档