xiaoyong931011
2021-03-24 c04ce1ba66b14135c92d6bf79450f78361a29336
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
176
177
178
179
180
181
/**
 * 贝塞尔曲线
 */
function bezier(points, times) {
  // 0、以3个控制点为例,点A,B,C,AB上设置点D,BC上设置点E,DE连线上设置点F,则最终的贝塞尔曲线是点F的坐标轨迹。
  // 1、计算相邻控制点间距。
  // 2、根据完成时间,计算每次执行时D在AB方向上移动的距离,E在BC方向上移动的距离。
  // 3、时间每递增100ms,则D,E在指定方向上发生位移, F在DE上的位移则可通过AD/AB = DF/DE得出。
  // 4、根据DE的正余弦值和DE的值计算出F的坐标。
  // 邻控制AB点间距
  var bezier_points = [];
  var points_D = [];
  var points_E = [];
  const DIST_AB = Math.sqrt(Math.pow(points[1]['x'] - points[0]['x'], 2) + Math.pow(points[1]['y'] - points[0]['y'], 2));
  // 邻控制BC点间距
  const DIST_BC = Math.sqrt(Math.pow(points[2]['x'] - points[1]['x'], 2) + Math.pow(points[2]['y'] - points[1]['y'], 2));
  // D每次在AB方向上移动的距离
  const EACH_MOVE_AD = DIST_AB / times;
  // E每次在BC方向上移动的距离 
  const EACH_MOVE_BE = DIST_BC / times;
  // 点AB的正切
  const TAN_AB = (points[1]['y'] - points[0]['y']) / (points[1]['x'] - points[0]['x']);
  // 点BC的正切
  const TAN_BC = (points[2]['y'] - points[1]['y']) / (points[2]['x'] - points[1]['x']);
  // 点AB的弧度值
  const RADIUS_AB = Math.atan(TAN_AB);
  // 点BC的弧度值
  const RADIUS_BC = Math.atan(TAN_BC);
  // 每次执行
  for (var i = 1; i <= times; i++) {
    // AD的距离
    var dist_AD = EACH_MOVE_AD * i;
    // BE的距离
    var dist_BE = EACH_MOVE_BE * i;
    // D点的坐标
    var point_D = {};
    point_D['x'] = dist_AD * Math.cos(RADIUS_AB) + points[0]['x'];
    point_D['y'] = dist_AD * Math.sin(RADIUS_AB) + points[0]['y'];
    points_D.push(point_D);
    // E点的坐标
    var point_E = {};
    point_E['x'] = dist_BE * Math.cos(RADIUS_BC) + points[1]['x'];
    point_E['y'] = dist_BE * Math.sin(RADIUS_BC) + points[1]['y'];
    points_E.push(point_E);
    // 此时线段DE的正切值
    var tan_DE = (point_E['y'] - point_D['y']) / (point_E['x'] - point_D['x']);
    // tan_DE的弧度值
    var radius_DE = Math.atan(tan_DE);
    // 地市DE的间距
    var dist_DE = Math.sqrt(Math.pow((point_E['x'] - point_D['x']), 2) + Math.pow((point_E['y'] - point_D['y']), 2));
    // 此时DF的距离
    var dist_DF = (dist_AD / DIST_AB) * dist_DE;
    // 此时DF点的坐标
    var point_F = {};
    point_F['x'] = dist_DF * Math.cos(radius_DE) + point_D['x'];
    point_F['y'] = dist_DF * Math.sin(radius_DE) + point_D['y'];
    bezier_points.push(point_F);
  }
  return {
    'bezier_points': bezier_points
  };
}
 
 
/**
 * 播放加入购物车动画
 */
function playAnimation(param, callBack) {
  console.log("playAnimation");
 
  //页面对象
  var page = param.page;
  //点击事件
  var event = param.event;
 
 
  if (!page.data.hide_good_box) return;
  page.finger = {};
  var topPoint = {};
  page.finger['y'] = event.touches["0"].clientY;
  page.finger['x'] = event.touches["0"].clientX;
 
  if (page.finger['x'] > page.busPos['x']) {
    page.isFingerXQtBusX = true;
  } else {
    page.isFingerXQtBusX = false;
  }
  if (page.finger['y'] < page.busPos['y']) {
    topPoint['y'] = page.finger['y'] - 150;
  } else {
    topPoint['y'] = page.busPos['y'] - 150;
  }
  topPoint['x'] = Math.abs(page.finger['x'] - page.busPos['x']) / 2;
  if (page.finger['x'] > page.busPos['x']) {
    topPoint['x'] = (page.finger['x'] - page.busPos['x']) / 2 + page.busPos['x'];
  } else {
    topPoint['x'] = (page.busPos['x'] - page.finger['x']) / 2 + page.finger['x'];
  }
 
 
  if (page.isFingerXQtBusX) {
    page.linePos = bezier([page.busPos, topPoint, page.finger], 30);
    //启动动画
    startAnimation(page,callBack);
 
  } else {
    page.linePos = bezier([page.finger, topPoint, page.busPos], 30);
    //启动动画
    startAnimationTow(page,callBack);
  }
 
 
 
}
 
function startAnimation(page, callBack) {
  var index = 29,
    bezier_points = page.linePos['bezier_points'];
  page.setData({
    hide_good_box: false,
    bus_x: page.finger['x'],
    bus_y: page.finger['y']
  })
  page.timer = setInterval(function () {
 
    page.setData({
      bus_x: bezier_points[index]['x'],
      bus_y: bezier_points[index]['y']
    })
    index--;
    if (index < 0) {
      clearInterval(page.timer);
      page.setData({
        hide_good_box: true,
        hideCount: false,
        count: page.data.count += 1
      })
      callBack();
    }
  }, 20);
}
 
 
 
function startAnimationTow(page, callBack) {
  var index = 0,
    bezier_points = page.linePos['bezier_points'];
 
  page.setData({
    hide_good_box: false,
    bus_x: page.finger['x'],
    bus_y: page.finger['y']
  })
  page.timer = setInterval(function () {
 
    page.setData({
      bus_x: bezier_points[index]['x'],
      bus_y: bezier_points[index]['y']
    })
    index++;
    if (index > 28) {
      clearInterval(page.timer);
      page.setData({
        hide_good_box: true,
        hideCount: false,
        count: page.data.count += 1
      })
 
      callBack();
    }
  }, 20);
 
}
 
 
module.exports = {
 
  bezier: bezier,
  playAnimation: playAnimation,
 
}