您好,登錄后才能下訂單哦!
這篇文章主要講解了“微信小程序如何實現照片裁剪”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“微信小程序如何實現照片裁剪”吧!
組件代碼
1.cut_photo.json
{ "component": true }
2.cut_photo.wxml
<view> <canvas class="fyj_canvas" canvas-id="myCanvas" > <movable-area class="fyj_movable_area text-center hidden" > <movable-view wx:if="{{src}}" class="fyj_movable_view" x="{{x}}" y="{{y}}" direction="all" bindchange="movableChange" ></movable-view> <image class="fyj_photo" id="fyj_photo" src="{{src}}" mode="widthFix"></image> </movable-area> </canvas> <view > <button class="pull-left" type="warn" size="mini" bindtap="getPhoto">選擇照片/拍照</button> <button class="pull-right" type="primary" size="mini" bindtap="cut">裁剪</button> <view class="clearfix"></view> </view> </view>
3.cut_photo.js
const app = getApp() Component({ options: { //multipleSlots: true // 在組件定義時的選項中啟用多slot支持 }, properties: { // 這里定義了innerText屬性,屬性值可以在組件使用時指定 //寬高比 aspectRatio: { type: Number, value: 5/7, } }, data: { screenWidth: wx.getSystemInfoSync().windowWidth, canvasHeight: 300, x: 0, y: 0, src: '', cut_src: '', cutWidth: 0, cutHeight: 0 }, attached: function () { }, methods: { // 這里是一個自定義方法 //選擇照片 getPhoto: function () { const $this = this; const ctx = wx.createCanvasContext('myCanvas',this) var obj = wx.createSelectorQuery(); wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success(res) { //清空之前的剪切圖 $this.triggerEvent('getTempFilePath', { cut_src: '', cutWidth: $this.data.cutWidth, cutHeight: $this.data.cutHeight }) // tempFilePath可以作為img標簽的src屬性顯示圖片 const tempFilePaths = res.tempFilePaths[0]; $this.setData({ src: tempFilePaths, cut_src: '', }); setTimeout(function () { wx.createSelectorQuery().in($this).select('#fyj_photo').boundingClientRect(function (rect) { console.log(rect); console.log(rect.height); $this.setData({ canvasHeight: rect.height }) ctx.drawImage(tempFilePaths, 0, 0, $this.data.screenWidth, $this.data.canvasHeight) ctx.draw(); $this.setCut(); //確保不同大小的圖片,切圖不會變形 $this.setData({ x: 0, y: 0 }); }).exec() }, 100) } }) }, //獲取圖片高度 // getHeight:function(){ // const query = wx.createSelectorQuery().in(this) // query.selectAll('#fyj_photo').boundingClientRect() // query.exec(function (rect) { // console.log(rect); // console.log(rect[0].height); // $this.setData({ // canvasHeight: rect[0].height // }) // ctx.drawImage(tempFilePaths[0], 0, 0, $this.data.screenWidth, $this.data.canvasHeight) // ctx.draw(); // $this.setCut(); // }) // }, //裁剪框移動事件 movableChange: function (e) { console.log(e.detail); this.setData({ x: e.detail.x, y: e.detail.y }) }, //截圖 cut: function () { const $this = this; console.log($this.data.cutHeight); wx.canvasToTempFilePath({ x: $this.data.x, y: $this.data.y, width: $this.data.cutWidth, height: $this.data.cutHeight, destWidth: $this.data.cutWidth, destHeight: $this.data.cutHeight, canvasId: 'myCanvas', success(res) { console.log(res.tempFilePath); $this.setData({ cut_src: res.tempFilePath }) $this.triggerEvent('getTempFilePath', { cut_src: $this.data.cut_src, cutWidth: $this.data.cutWidth, cutHeight: $this.data.cutHeight}) } },this) }, //動態設置裁剪框大小,確定高度不得超過canvas的高度 setCut: function () { const $this = this; this.setData({ cutWidth: wx.getSystemInfoSync().windowWidth * 0.8, cutHeight: wx.getSystemInfoSync().windowWidth * 0.8/this.data.aspectRatio }) if (this.data.cutHeight - 4 > this.data.canvasHeight) { console.log($this.data.cutHeight); console.log($this.data.canvasHeight); this.setData({ cutHeight: this.data.canvasHeight - 4, cutWidth: (this.data.canvasHeight - 4)*this.data.aspectRatio }) } else { this.setData({ cutWidth: wx.getSystemInfoSync().windowWidth * 0.8, cutHeight: wx.getSystemInfoSync().windowWidth * 0.8/this.data.aspectRatio }) } console.log($this.data.cutWidth); console.log($this.data.cutHeight); }, } })
4.cut_photo.wxss
.fyj_movable_area{width:100%;height:auto;position: relative;background:rgba(0,0,0,0.3)} .fyj_movable_view{border:2px dashed #fff} .fyj_photo{width:100%;} .fyj_footer{margin-top:20rpx 0;} .fyj_footerBtn{width:100%;display: inline-block;color:#fff;border-radius: 0;font-size:32rpx;} .fyj_sure{background: #fc6b47;} .pull-left{float:left;} .pull-right{float:right} .clearfix{clear:both} .text-center{text-align: center}
引用頁代碼
1.page.json
{ "navigationBarTitleText": "選擇照片", "usingComponents": { "cut-photo": "/pages/cut_photo/cut_photo" } }
2.page.wxml
<view> <!-- aspectRatio 剪裁圖片的寬高比 --> <cut-photo aspectRatio="0.5" bindgetTempFilePath="getCutsrc"></cut-photo> <view wx:if="{{cut_src}}" class="fyj_cutDiv text-center"> <image class="fyj_cut_photo" src="{{cut_src}}" mode="widthFix"></image> </view> <view wx:if="{{cut_src}}" class="fyj_footer text-center"> <button class="fyj_footerBtn fyj_sure" bindtap='sure'>確定</button> </view> </view>
3.page.js
const app = getApp() Page({ /** * 頁面的初始數據 */ data: { cut_src:'', cutWidth:0, cutHeight:0, }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { }, getCutsrc:function(e){ console.log(e); this.setData({ cut_src: e.detail.cut_src, cutWidth: e.detail.cutWidth, cutHeight: e.detail.cutHeight }) } })
4.page.wxss
.fyj_footer{margin-top:20rpx 0;} .fyj_footerBtn{width:100%;display: inline-block;color:#fff;border-radius: 0;font-size:32rpx;} .fyj_sure{background: #fc6b47;} .fyj_cutDiv{margin:20rpx 0;}
大概思路
將canvas跟movable-area重合,通過movable-view來確定裁剪區域。為了確保圖片加載不變形,選擇完圖片后,需要動態設置canvas、movable-area的高度及movable-view的寬高。
感謝各位的閱讀,以上就是“微信小程序如何實現照片裁剪”的內容了,經過本文的學習后,相信大家對微信小程序如何實現照片裁剪這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。