小程序?qū)崿F(xiàn)圖片預(yù)覽裁剪插件
最近在幫工作室做一個(gè)小程序,在換背景圖的時(shí)候需要預(yù)覽圖片,并且需要裁剪成固定的尺寸。因?yàn)樾〕绦虿⒉恢С衷膁om操作,導(dǎo)致很多現(xiàn)有的插件都無(wú)法使用,所以花了半天專門做了一個(gè)小程序的預(yù)覽裁剪插件。下面貼上代碼和效果圖。
wxml:
<canvas hidden='{{hide_canvas}}' id='cover-preview' bindtouchstart='canvas_start' bindtouchmove='canvas_move' bindtouchend='canvas_end' disable-scroll='true' canvas-id='cover-preview'>
<cover-view catchtap='upload_bg' id='croper-sure'>確定</cover-view>
<cover-view catchtap='cancel_croper' id='croper-cancel'>取消</cover-view>
<cover-view id='croper'></cover-view>
</canvas>
注意:canvas里面一定要用cover-view,否則無(wú)法覆蓋canvas
js:
const ctx = wx.createCanvasContext('cover-preview');
var start_position = {};//移動(dòng)圖片時(shí)手指起始坐標(biāo)
var tempFilePath;//圖片路徑
var tempWidth;//圖片初始寬度
var tempHeight;//圖片初始高度
var old_x = 0;//圖片初始x坐標(biāo)
var old_y = 0;//圖片初始y坐標(biāo)
var _touches = 1;//觸屏的手指數(shù)
var old_scale = 1;//原始放大倍數(shù)
var new_scale = 1;//新的放大倍數(shù)
var is_move = false;//是否移動(dòng)
var bg_url;
Page({
data: {
hide_canvas:true,//繪圖層顯示控制變量
},
//選擇并將圖片輸出到canvas
change_cover:function(){
var that = this;
wx.showModal({
title: '提示',
content: '更改我的封面',
confirmColor: '#39bae8',
success: function (res) {
if (res.confirm) {
wx.chooseImage({
count: 1, // 默認(rèn)9
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album', 'camera'], // 可以指定來(lái)源是相冊(cè)還是相機(jī),默認(rèn)二者都有
success: function (res0) {
tempFilePath = res0.tempFilePaths[0];
that.setData({
hide_canvas: false,
// edit_url: tempFilePath
})
wx.getImageInfo({
src: tempFilePath,
success: function (res) {
// console.log(res.width)
// console.log(res.height)
tempWidth = res.width;
tempHeight = res.height;
ctx.drawImage(tempFilePath,0, 0, 375,res.height/res.width*375);
ctx.draw();
}
})
}
})
} else if (res.cancel) {
console.log('用戶點(diǎn)擊取消')
}
}
})
},
//監(jiān)聽手指觸摸事件,并判斷是移動(dòng)還是縮放,并記錄初始狀態(tài)
canvas_start:function(e){
// console.log(e);
var touches = e.touches.length;
if(touches == 1){
_touches = 1;
start_position = { x: e.touches[0].x, y: e.touches[0].y, timeStamp:e.timeStamp}
}else if(touches == 2){
_touches = 2;
start_position = { x: e.touches[0].x, y: e.touches[0].y,x1: e.touches[1].x, y1: e.touches[1].y, timeStamp: e.timeStamp }
}else{
_touches = 1;
}
},
//監(jiān)聽手指移動(dòng)事件,并做出相應(yīng)調(diào)整
canvas_move: function (e) {
// console.log(e);
var touches = e.touches.length;
if (_touches == 1 && e.timeStamp - start_position.timeStamp > 150) {
ctx.drawImage(tempFilePath, old_x + e.touches[0].x - start_position.x, old_y + e.touches[0].y - start_position.y, 375 * new_scale, tempHeight / tempWidth * 375 * new_scale);
ctx.draw();
is_move = true;
} else if (_touches == 2 && e.timeStamp - start_position.timeStamp > 150) {
var change_x = Math.abs(Math.abs(e.touches[0].x - e.touches[1].x) - Math.abs(start_position.x - start_position.x1));
var change_y = Math.abs(Math.abs(e.touches[0].y - e.touches[1].y) - Math.abs(start_position.y - start_position.y1));
if(change_x - change_y > 10){
old_scale = Math.abs(e.touches[0].x - e.touches[1].x) / Math.abs(start_position.x - start_position.x1);
}else{
old_scale = Math.abs(e.touches[0].y - e.touches[1].y) / Math.abs(start_position.y - start_position.y1);
}
ctx.drawImage(tempFilePath, old_x, old_y, 375 * old_scale * new_scale, tempHeight / tempWidth * 375 * old_scale * new_scale);
ctx.draw();
is_move = true;
}else{
is_move = false;
}
},
//監(jiān)聽手指離開動(dòng)作,并保存當(dāng)前狀態(tài)數(shù)據(jù)
canvas_end: function (e) {
// console.log(e);
if (_touches == 1 && is_move) {
old_x = old_x + e.changedTouches[0].x - start_position.x;
old_y = old_y + e.changedTouches[0].y - start_position.y;
} else if (_touches == 2 && is_move) {
new_scale = old_scale * new_scale;
}
},
//確定并上傳背景圖
upload_bg:function(){
var that = this;
var screenWidth = wx.getSystemInfoSync().screenWidth;
// console.log(screenWidth);
wx.canvasToTempFilePath({
x: 0,
y: screenWidth / 750 * 400,
width: screenWidth,
height: screenWidth / 750 * 526,
destWidth: screenWidth,
screenHeight: screenWidth / 750 * 526,
quality:1,
canvasId: 'cover-preview',
success: function (res) {
that.setData({
hide_canvas: true,
})
//res.tempFilePath即為生成的圖片路徑
console.log(res.tempFilePath)
}
})
},
//取消圖片預(yù)覽編輯
cancel_croper:function(){
ctx.clearActions();
this.setData({
hide_canvas: true,
// edit_url: tempFilePath
})
},
})
wxss:
#cover-preview{
width: 100%;
height: 100%;
background-color: black;
}
#croper{
width: 750rpx;
height: 526rpx;
position: absolute;
top: 400rpx;
left: 0;
background-color: rgba(135,206,250,0.5);
}
#croper-sure{
width: 120rpx;
height: 50rpx;
border-radius: 10rpx;
color: black;
background-color: rgba(135,206,250,0.8);
font-size: 40rpx;
position: absolute;
top: 946rpx;
right: 10rpx;
text-align: center
}
#croper-cancel{
width: 120rpx;
height: 50rpx;
border-radius: 10rpx;
color: black;
background-color: rgba(135,206,250,0.8);
font-size: 40rpx;
position: absolute;
top: 946rpx;
right: 150rpx;
text-align: center
}
效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Javascript控制div屬性動(dòng)態(tài)變化實(shí)例分析
這篇文章主要介紹了Javascript控制div屬性動(dòng)態(tài)變化,以實(shí)例形式較為詳細(xì)的分析了JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素屬性的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
js select下拉聯(lián)動(dòng) 更具級(jí)聯(lián)性!
這篇文章主要為大家詳細(xì)介紹了js select下拉聯(lián)動(dòng)的相關(guān)資料,更具級(jí)聯(lián)性!文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
fw.qq.com/ipaddress已失效 javascript獲得客戶端IP的新方法
一直以來(lái),我都是通過http://fw.qq.com/ipaddress來(lái)獲得客戶端用戶的IP,這個(gè)方法簡(jiǎn)單、快速、實(shí)用2012-01-01
Layer.js實(shí)現(xiàn)表格溢出內(nèi)容省略號(hào)顯示,懸停顯示全部的方法
今天小編就為大家分享一篇Layer.js實(shí)現(xiàn)表格溢出內(nèi)容省略號(hào)顯示,懸停顯示全部的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-09-09
js遍歷對(duì)象key和value實(shí)戰(zhàn)舉例
這篇文章主要給大家介紹了關(guān)于js遍歷對(duì)象key和value的相關(guān)資料,隨著JavaScript在web應(yīng)用程序中的廣泛使用,遍歷對(duì)象的key和value成為了編寫復(fù)雜代碼所必需的技能,需要的朋友可以參考下2023-07-07

