微信小程序手指缩放图片实现代码详细展示与解读

why 50 2024-09-30

用手指缩放图片。其实在实现这个需求以前,并不知道,微信公众号以及微信小程序里面有一个原生的api就自带这个特效,而且微信朋友圈也是用的这个api。wx.previewimage,就是它。预览图片。除了不能预览开发环境的本地电脑的图片外,你手机真机的图片,以及http服务器上的图片都是可以预览的,而且缩放功能做得很流畅。本文主要和大家介绍了微信小程序中实现手指缩放图片的示例代码。

微信小程序手指缩放图片实现代码详细展示与解读

先上源码,然后在逐步剖析:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

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({

      &#39;touch.distance&#39;: distance,

      &#39;touch.scale&#39;: newScale,

      &#39;touch.scaleWidth&#39;: scaleWidth,

      &#39;touch.scaleHeight&#39;: scaleHeight,

      &#39;touch.diff&#39;: distanceDiff

    })

  },

  bindload: function(e) {

   // bindload 这个api是<image>组件的api类似<img  alt="微信小程序实现手指缩放图片代码分享" >的onload属性

   this.setData({

     &#39;touch.baseWidth&#39;: e.detail.width,

     &#39;touch.baseHeight&#39;: e.detail.height,

     &#39;touch.scaleWidth&#39;: e.detail.width,

     &#39;touch.scaleHeight&#39;: e.detail.height

   })

  }

})

wxml文件对应如下,就不做解释了:


1

2

3

4

5

<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>

写到这里发现,就算小程序用不了这个js,我的ht5页面也是可以用的,哈哈。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序 js 文件外部引用实例详细解析与展示
下一篇:微信支付接口调研分享之全面解析与经验总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~