Blame view

components/mpvueGestureLock/gestureLock.js 5.13 KB
4b045f7c   刘淇   江阴初始化项目
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  class GestureLock {
  
      constructor(containerWidth, cycleRadius) {
          this.containerWidth = containerWidth; // 容器宽度
          this.cycleRadius = cycleRadius; // 圆的半径
  
          this.circleArray = []; // 全部圆的对象数组
          this.checkPoints = []; // 选中的圆的对象数组
          this.lineArray = []; // 已激活锁之间的线段数组
          this.lastCheckPoint = 0; // 最后一个激活的锁
          this.offsetX = 0; // 容器的 X 偏移
          this.offsetY = 0; // 容器的 Y 偏移
          this.activeLine = {}; // 最后一个激活的锁与当前位置之间的线段
  
          this.windowWidth = wx.getSystemInfoSync().windowWidth; // 窗口大小(用于rpx 和 px 转换)
  
          this.initCircleArray();
      }
  
      // 初始化 画布上的 9个圆
      initCircleArray() {
          const cycleMargin = (this.containerWidth - 6 * this.cycleRadius) / 6;
          let count = 0;
          for (let i = 0; i < 3; i++) {
              for (let j = 0; j < 3; j++) {
                  count++;
                  this.circleArray.push({
                      count: count,
                      x: this.rpxTopx((cycleMargin + this.cycleRadius) * (j * 2 + 1)),
                      y: this.rpxTopx((cycleMargin + this.cycleRadius) * (i * 2 + 1)),
                      radius: this.rpxTopx(this.cycleRadius),
                      check: false,
                      style: {
                          left: (cycleMargin + this.cycleRadius) * (j * 2 + 1) - this.cycleRadius + 'rpx',
                          top: (cycleMargin + this.cycleRadius) * (i * 2 + 1) - this.cycleRadius + 'rpx',
                          width: this.cycleRadius * 2 + 'rpx',
                      }
                  });
              }
          }
      }
  
      onTouchStart(e) {
          this.setOffset(e);
          this.checkTouch({
              x: e.touches[0].pageX - this.offsetX,
              y: e.touches[0].pageY - this.offsetY
          });
      }
  
      onTouchMove(e) {
          this.moveDraw(e)
      }
  
      onTouchEnd(e) {
          const checkPoints = this.checkPoints;
          this.reset();
          return checkPoints;
      }
  
      // 初始化 偏移量
      setOffset(e) {
          this.offsetX = e.currentTarget.offsetLeft;
          this.offsetY = e.currentTarget.offsetTop;
      }
  
      // 检测当时 触摸位置是否位于 锁上
      checkTouch({
          x,
          y
      }) {
          for (let i = 0; i < this.circleArray.length; i++) {
              let point = this.circleArray[i];
              if (this.isPointInCycle(x, y, point.x, point.y, point.radius)) {
                  if (!point.check) {
                      this.checkPoints.push(point.count);
                      if (this.lastCheckPoint != 0) {
                          // 已激活锁之间的线段
                          const line = this.drawLine(this.lastCheckPoint, point);
                          this.lineArray.push(line);
                      }
                      this.lastCheckPoint = point;
                  }
                  point.check = true;
                  return;
              }
          }
      }
  
      // 画线 - 返回 样式 对象
      drawLine(start, end) {
          const width = this.getPointDis(start.x, start.y, end.x, end.y);
          const rotate = this.getAngle(start, end);
  
          return {
              activeLeft: start.x + 'px',
              activeTop: start.y + 'px',
              activeWidth: width + 'px',
              activeRotate: rotate + 'deg'
          }
  
      }
  
      // 获取 画线的 角度
      getAngle(start, end) {
          var diff_x = end.x - start.x,
              diff_y = end.y - start.y;
          if (diff_x >= 0) {
              return 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
          } else {
              return 180 + 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
          }
      }
  
      // 判断 当前点是否位于 锁内
      isPointInCycle(x, y, circleX, circleY, radius) {
          return (this.getPointDis(x, y, circleX, circleY) < radius) ? true : false;
      }
  
      // 获取两点之间距离
      getPointDis(ax, ay, bx, by) {
          return Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
      }
  
      // 移动 绘制
      moveDraw(e) {
          // 画经过的圆
          const x = e.touches[0].pageX - this.offsetX;
          const y = e.touches[0].pageY - this.offsetY;
          this.checkTouch({
              x,
              y
          });
  
          // 画 最后一个激活的锁与当前位置之间的线段
          this.activeLine = this.drawLine(this.lastCheckPoint, {
              x,
              y
          });
      }
  
      // 使 画布 恢复初始状态
      reset() {
          this.circleArray.forEach((item) => {
              item.check = false;
          });
          this.checkPoints = [];
          this.lineArray = [];
          this.activeLine = {};
          this.lastCheckPoint = 0;
      }
  
  
      // 获取 最后一个激活的锁与当前位置之间的线段
      getActiveLine() {
          return this.activeLine;
      }
  
      // 获取 圆对象数组
      getCycleArray() {
          return this.circleArray;
      }
  
      // 获取 已激活锁之间的线段
      getLineArray() {
          return this.lineArray;
      }
  
      // 将 RPX 转换成 PX
      rpxTopx(rpx) {
          return rpx / 750 * this.windowWidth;
      }
  }
  
  export default GestureLock;