前言
最近在开发小程序,产品经理提了一个需求,要求微信小程序换头像,用户剪裁图片必须是圆形,也在github上看了一些例子,一般剪裁图片用的都是方形,所以自己打算写一个小组件,可以把图片剪裁成圆形,主要思路就是使用canvas绘图,把剪裁的图片绘制成圆形,另外剪裁图片的窗口还可以移动放大缩小,这个功能就用了微信组件movable-view,好了,该说的也说完了,下面咱们开始撸代码。
movable-view组件
可移动的视图容器,在页面中可以拖拽滑动 会有好多个属性,在这里不一一介绍,只说我们能用到的就可以。 我们用到的属性主要有:
- direction:movable-view的移动方向,属性值有all、vertical、horizontal、none
- scale:是否支持双指缩放,默认缩放手势生效区域是在movable-view内
- scale-min 定义缩放倍数最小值
- scale-max 定义缩放倍数最大值
- bindchange 拖动过程中触发的事件,event.detail = {x: x, y: y, source: source},其中source表示产生移动的原因,值可为touch(拖动)、touch-out-of-bounds(超出移动范围)、out-of-bounds(超出移动范围后的回弹)、friction(惯性)和空字符串(setData)
- bindscale 缩放过程中触发的事件,event.detail = {x: x, y: y, scale: scale},其中x和y字段在2.1.0之后开始支持返回
主要用到的就是这几个值
另外使用movable-view的时候必须在外边加一个movable-area的父元素,不然的话没有移动区域。
movable-view 的可移动区域,属性只有:
scale-area 当里面的movable-view设置为支持双指缩放时,设置此值可将缩放手势生效区域修改为整个movable-area,是个boolean值,默认false
截取区域的移动已经说完了,详情请看developers.weixin.qq.com/miniprogram…
canvas绘图
代码实现
index.wxml
Tip: 必须把canvas放到引入剪裁组件的wxml中,否则绘制不成功,因为canvas是原生组件脱离在 WebView 渲染流程外。
<view class="container"> <button wx:if="" bindtap="getImgurl"> 选择图片 button> <view class="clip-box" wx:if="{{imgSrc}}"> <ClipImg imgSrc="{{imgSrc}}">ClipImg> view> view> <canvas canvas-id="myCanvas" style="position:absolute; width:100%;height:100%;border: 1px solid red;left: -9999px; top: -9999px;">canvas> 复制代码
index.json引入截取图片的组件
{ "component": true, "usingComponents": { "ClipImg": "../../component/clipImg/clipImg" } } 复制代码
index.js上传图片显示
const app = getApp() Page({ data: { imgSrc: '' }, getImgurl: function () { wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: (res) => { const tempFilePaths = res.tempFilePaths; wx.showToast({ title: '正在上传...', icon: 'loading', mask: true, duration: 1000 }) this.setData({ imgSrc: res.tempFilePaths }) } }) }, onLoad: function () { } }) 复制代码
接下来就是剪裁图片组件的封装
首先是页面布局,也就是clipImg.wxml
<view class="clip"> <image class="head-img" style="width:{{cropperW}}rpx;height:{{cropperH}}rpx" src="{{imageUrl}}">image> <movable-area scale-area style="width:{{cropperW}}rpx;height:{{cropperH}}rpx"> <movable-view bindchange="move" bindscale="scale" direction="all" scale scale-min="0.5" scale-max="1.8"> movable-view> movable-area> <view class="btn"> <text bindtap="cancel">取消text> <text bindtap="getImageInfo">保存text> view> view> 复制代码
大概就是这个样子
上边的圆就是截取就是截取框。
然后就是clipImg.js文件主要就是对图片截取的一些操作
Component({ properties: { imgSrc: { type: 'String', value: '' } }, data: { imageUrl: '', cropperW: '', cropperH: '', img_ratio: '', IMG_W: '', IMG_H: '', left: '', top: '', clipW: 200 }, methods: { cancel: function () { var myEventDetail = {} var myEventOption = {} this.triggerEvent('myevent', myEventDetail, myEventOption) }, move: function ({ detail }) { this.setData({ left: detail.x * 2, top: detail.y * 2 }) }, scale: function ({ detail }) { console.log(detail.scale) this.setData({ clipW: 200 * detail.scale }) }, getImageInfo: function () { wx.showLoading({ title: '图片生成中...', }) const img_ratio = this.data.img_ratio; const canvasW = (this.data.clipW / this.data.cropperW) * this.data.IMG_W const canvasH = (this.data.clipW / this.data.cropperH) * this.data.IMG_H const canvasL = (this.data.left / this.data.cropperW) * this.data.IMG_W const canvasT = (this.data.top / this.data.cropperH) * this.data.IMG_H const ctx = wx.createCanvasContext('myCanvas'); ctx.save(); ctx.beginPath(); ctx.clearRect(0, 0, 1000, 1000) ctx.arc(this.data.clipW / 2, this.data.clipW / 2, this.data.clipW / 2, 0, 2 * Math.PI, false) ctx.clip(); ctx.drawImage(this.data.imageUrl, canvasL, canvasT, canvasW, canvasH, 0, 0, this.data.clipW, this.data.clipW); ctx.restore(); ctx.draw(true, () => { wx.canvasToTempFilePath({ x: 0, y: 0, width: this.data.clipW, height: this.data.clipW, destWidth: this.data.clipW, destHeight: this.data.clipW, quality: 0.5, canvasId: 'myCanvas', success: (res) => { wx.hideLoading() wx.previewImage({ current: '', urls: [res.tempFilePath] }) } }) }) } }, ready: function () { this.setData({ imageUrl: this.data.imgSrc[0] }) wx.getImageInfo({ src: this.data.imageUrl, success: (res) => { console.log('图片信息', res); const width = res.width; const height = res.height; const img_ratio = width / height this.setData({ img_ratio, IMG_W: width, IMG_H: height, }) if (img_ratio >= 1) { this.setData({ cropperW: 750, cropperH: 750 / img_ratio, }) } else { this.setData({ cropperW: 750 * img_ratio, cropperH: 750 }) } } }) } }) 复制代码
到现在为止一个截取图片就完成了,可能会有些问题,比如截取的图片的框没有居中,自己可以再次封装这个组件,因为现在已经适合我们公司自己项目了。我们来预览下。另外这个组件支持双指放大截取框来截取图片,不过微信开发者工具不能展示,自己可以把代码下载下来,在自己手机上扫码查看效果。
另外我把项目放到了github上边,希望小哥哥小姐姐们多多点赞,多多支持。使用的时候直接把component里边的组件直接引进去就行,有什么疑问可以在github底下留言问我,谢谢。点赞的小哥哥小姐姐最可爱,哈哈哈。。。
项目地址github.com/Mr-MengBo/i…