2018-07-12 14:32:36 417浏览
今天扣丁学堂HTML5培训给大家分享一个关于微信小程序图片剪裁组件,它可以通过手势控制旋转缩放移动,也可以点击旋转进行90度旋转,先看下效果吧:
<viewclass="container"> <!--剪裁框与初始图片,剪裁框监听用户手势,获取移动缩放旋转值,images通过css样式显示变化--> <viewclass="img"style="width:{{width}}px;height:{{height}}px"catchtouchstart="touchstartCallback"catchtouchmove="touchmoveCallback"catchtouchend="touchendCallback"> <imagestyle="transform:translate({{stv.offsetX}}px,{{stv.offsetY}}px)scale({{stv.scale}})rotate({{stv.rotate}}deg);width:{{originImg.width}}px;height:{{originImg.height}}px"src="{{originImg.url}}"></image> </view> <viewclass='footer'> <viewbindtap='uploadTap'>选择图片</view> <viewbindtap='rotate'>旋转</view> <viewbindtap='cropperImg'>剪裁</view> </view> <!--canvas长宽设为初始图片设置的长款的两倍,使剪裁得到的图片更清晰,也不至于过大--> <canvasclass='imgcrop'style="width:{{width*2}}px;height:{{height*2}}px;"canvas-id='imgcrop'></canvas> </view>
letctx=wx.createCanvasContext('imgcrop',this); letcropData=_this.data.stv; ctx.save(); //缩放偏移值 letx=(_this.data.originImg.width-_this.data.originImg.width*cropData.scale)/2; lety=(_this.data.originImg.height-_this.data.originImg.height*cropData.scale)/2; //画布中点坐标转移到图片中心 letmovex=(cropData.offsetX+x)*2+_this.data.originImg.width*cropData.scale; letmovey=(cropData.offsetY+y)*2+_this.data.originImg.height*cropData.scale; ctx.translate(movex,movey); ctx.rotate(cropData.rotate*Math.PI/180); ctx.translate(-movex,-movey); ctx.drawImage(_this.data.originImg.url,(cropData.offsetX+x)*2,(cropData.offsetY+y)*2,_this.data.originImg.width*2*cropData.scale,_this.data.originImg.height*2*cropData.scale); ctx.restore();
以上就是本文的全部内容,希望对大家的学习有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。扣丁学堂HTML5培训机构不仅有专业的老师和与时俱进的课程体系,还有大量的HTML5视频教程供学员观看学习哦。扣丁学堂H5技术交流群:559883758。
【关注微信公众号获取更多学习资料】