
公司开发微信小程序,,pm想实现如下需求:
用手指缩放图片。。。。着实在实现这个需求以前,,并不知道,,微信公众号以及微信小程序内里有一个原生的api就自带这个特效,,并且微信朋侪圈也是用的这个api。。。。wx.previewImage,,就是它。。。。预览图片。。。。除了不可预览开发情形的外地电脑的图片外,,你手机真机的图片,,以及http服务器上的图片都是可以预览的,,并且缩放功效做得很流通。。。。下面就说说怎样用js来实现这个功效吧。。。。
先上源码,,然后在逐步剖析:
Page({
data: {
touch: {
distance: 0,
scale: 1,
baseWidth: null,
baseHeight: null,
scaleWidth: null,
scaleHeight: null
}
},
touchstartCallback: function(e) {
// 单手指缩铺最先,,也不做任那里置
if(e.touches.length == 1) return
console.log('双手指触发最先')
// 注重touchstartCallback 真正代码的最先
// 一最先我并没有这个回调函数,,会泛起缩小的时间有瞬间被放大历程的bug
// 当两根手指放上去的时间,,就将distance 初始化。。。。
let xMove = e.touches[1].clientX - e.touches[0].clientX;
let yMove = e.touches[1].clientY - e.touches[0].clientY;
let distance = Math.sqrt(xMove * xMove + yMove * yMove);
this.setData({
'touch.distance': distance,
})
},
touchmoveCallback: function(e) {
let touch = this.data.touch
// 单手指缩放我们不做任何操作
if(e.touches.length == 1) return
console.log('双手指运动')
let xMove = e.touches[1].clientX - e.touches[0].clientX;
let yMove = e.touches[1].clientY - e.touches[0].clientY;
// 新的 ditance
let distance = Math.sqrt(xMove * xMove + yMove * yMove);
let distanceDiff = distance - touch.distance;
let newScale = touch.scale + 0.005 * distanceDiff
// 为了防止缩放得太大,,以是scale需要限制,,同理最小值也是
if(newScale >= 2) {
newScale = 2
}
if(newScale <= 0.6) {
newScale = 0.6
}
let scaleWidth = newScale * touch.baseWidth
let scaleHeight = newScale * touch.baseHeight
// 赋值 新的 => 旧的
this.setData({
'touch.distance': distance,
'touch.scale': newScale,
'touch.scaleWidth': scaleWidth,
'touch.scaleHeight': scaleHeight,
'touch.diff': distanceDiff
})
},
bindload: function(e) {
// bindload 这个api是
组件的api类似
的onload属性
this.setData({
'touch.baseWidth': e.detail.width,
'touch.baseHeight': e.detail.height,
'touch.scaleWidth': e.detail.width,
'touch.scaleHeight': e.detail.height
})
}
})
wxml文件对应如下,,就不做诠释了:
<view class="container"> <view bindtouchmove="touchmoveCallback" bindtouchstart="touchstartCallback"> <image src="../../resources/pic/cat.jpg" style="width: {{ touch.scaleWidth }}px;height: {{ touch.scaleHeight }}px" bindload="bindload"></image> </view> </view>
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。。。KESION 一直通过手艺立异,,提供产品和服务,,助力企业向数字化转型,,通过科技驱动商业刷新,,让商业变得更智慧!
小程序TodoList的使用,,下面用简朴的几个例子来介绍。。。。...
京东开普勒现在支持将京东店肆一键天生小程序商城,,发力小程序社交电商,,不过现在还不支持未入驻京东店肆的商家,,“小程序轻商城”上线以后,,将支持未入驻京东商家天生小程序...