Baozhangchao 3 lat temu
rodzic
commit
bf82fc8ec9

+ 2
- 2
src/components/Counter/index.jsx Wyświetl plik

@@ -50,7 +50,7 @@ const ShakeCount = (props) => {
50 50
           canvas.width = width
51 51
           canvas.height = height
52 52
           ctx.scale(dpr, dpr)
53
-          
53
+
54 54
           canvasRef.current = canvas
55 55
           initS()
56 56
         })
@@ -68,7 +68,7 @@ const ShakeCount = (props) => {
68 68
           canvas.width = width
69 69
           canvas.height = height
70 70
           ctx.scale(dpr, dpr)
71
-          
71
+
72 72
           canvasShapeRef.current = canvas
73 73
           initS()
74 74
         })

+ 7
- 0
src/hooks/useInterval.jsx Wyświetl plik

@@ -1,4 +1,6 @@
1 1
 import { useCallback, useEffect, useRef } from "react"
2
+import useCountDown from './../pages/index/hooks/useCountDown';
3
+import useShake from './../pages/index/hooks/useShake';
2 4
 
3 5
 /**
4 6
  * 定时器
@@ -7,6 +9,8 @@ import { useCallback, useEffect, useRef } from "react"
7 9
  * @returns
8 10
  */
9 11
 export default function useInterval(interval) {
12
+  const [count, countFn, startCount] = useCountDown();
13
+  const [times, startShake, onShakeEnd] = useShake()
10 14
 
11 15
   // 待执行函数列表
12 16
   const fnListRef = useRef([])
@@ -25,8 +29,11 @@ export default function useInterval(interval) {
25 29
     const ticker = setInterval(() => {
26 30
       console.log('-----------interval---------')
27 31
       fnListRef.current.forEach(fn => fn())
32
+
33
+
28 34
     }, interval)
29 35
     return () => clearInterval(ticker)
36
+
30 37
   }, [])
31 38
 
32 39
   return [push]

+ 0
- 1
src/pages/index/components/TopBar/index.jsx Wyświetl plik

@@ -9,7 +9,6 @@ import './style.less'
9 9
 export default (props) => {
10 10
 
11 11
   const showTopList = () => {
12
-    console.log('----------------------')
13 12
     // Taro.navigateTo({ url: '/pages/topList/index' })
14 13
     Taro.navigateTo({ url: '/pages/topListCopy/index' })
15 14
   }

+ 1
- 1
src/pages/index/hooks/useCountDown.js Wyświetl plik

@@ -15,7 +15,7 @@ export default function useCountDown() {
15 15
 
16 16
   const start = useCallback((startNum) => {
17 17
     numRef.current = startNum
18
-    setVal(3)
18
+    setVal(startNum)
19 19
   }, [])
20 20
 
21 21
   return [val, countDown, start]

+ 1
- 1
src/pages/index/hooks/useShake.js Wyświetl plik

@@ -15,7 +15,7 @@ export default function useShake() {
15 15
   // eslint-disable-next-line react-hooks/exhaustive-deps
16 16
   const handleShake = useCallback(throttle((val) => {
17 17
     setTimes(val)
18
-  }, 1300, { trailing: true, immediate: true }), [])
18
+  }, 1000, { trailing: true, immediate: true }), [])
19 19
 
20 20
   const start = useCallback(() => {
21 21
     isReadyRef.current = true

+ 55
- 32
src/pages/index/index.jsx Wyświetl plik

@@ -33,10 +33,11 @@ const IndexPage = (props) => {
33 33
   useMemo(() => add2TickerList(countFn), [])
34 34
 
35 35
 
36
+  const [state, setstate] = useState(true)
36 37
   const { run } = useThrottleFn(
37 38
     () => {
38 39
       setTimeout(() => {
39
-        Taro.vibrateLong()
40
+        state ? Taro.vibrateLong() : ''
40 41
 
41 42
       }, 30000);
42 43
     },
@@ -45,46 +46,59 @@ const IndexPage = (props) => {
45 46
 
46 47
 
47 48
 
49
+  // 摇一摇
50
+  const [times, startShake, onShakeEnd] = useShake()
48 51
 
52
+  console.log("🚀 ~ file: index.jsx ~ line 49 ~ IndexPage ~ times", times)
53
+  useEffect(() => {
49 54
 
50 55
 
51 56
 
57
+    if (times >= 2) {
58
+      setTimeout(() => {
52 59
 
60
+        run()
61
+
62
+        //添加音效
63
+        const innerAudioContext = Taro.createInnerAudioContext()
64
+        innerAudioContext.autoplay = true// 是否自动开始播放,默认为 false
65
+        innerAudioContext.loop = false  // 是否循环播放,默认为 false
66
+        Taro.setInnerAudioOption({ // ios在静音状态下能够正常播放音效
67
+          obeyMuteSwitch: false,   // 即使用户打开了静音开关,也能继续发出声音。
68
+          success: function (e) {
69
+            console.log(e)
70
+            console.log('play success')
71
+          },
72
+          fail: function (e) {
73
+            console.log(e)
74
+            console.log('play fail')
75
+          }
76
+        })
77
+        innerAudioContext.src = wexMP3
78
+        innerAudioContext.onPlay(() => {
79
+          console.log('开始播放')
80
+        })
81
+        innerAudioContext.onError((res) => {
82
+          console.log(res.errMsg)
83
+          console.log(res.errCode)
84
+        })
85
+        setTimeout(() => {
86
+          //定时清理destroy这个context。不然这个context就算stop了,内存还是被占用。
87
+          innerAudioContext.stop();
88
+
89
+          innerAudioContext.destroy();
90
+
91
+          innerAudioContext = null;
92
+
93
+        }, 10000);
53 94
 
54
-  // 摇一摇
55
-  const [times, startShake, onShakeEnd] = useShake()
56
-  useEffect(() => {
57
-    if (times >= 1) {
58
-      run()
59
-
60
-      //添加音效
61
-      const innerAudioContext = Taro.createInnerAudioContext()
62
-      innerAudioContext.autoplay = true// 是否自动开始播放,默认为 false
63
-      innerAudioContext.loop = false  // 是否循环播放,默认为 false
64
-      Taro.setInnerAudioOption({ // ios在静音状态下能够正常播放音效
65
-        obeyMuteSwitch: false,   // 即使用户打开了静音开关,也能继续发出声音。
66
-        success: function (e) {
67
-          console.log(e)
68
-          console.log('play success')
69
-        },
70
-        fail: function (e) {
71
-          console.log(e)
72
-          console.log('play fail')
73
-        }
74
-      })
75
-      innerAudioContext.src = wexMP3
76
-      innerAudioContext.onPlay(() => {
77
-        console.log('开始播放')
78
-      })
79
-      innerAudioContext.onError((res) => {
80
-        console.log(res.errMsg)
81
-        console.log(res.errCode)
82
-      })
95
+
96
+      }, 2000)
83 97
 
84 98
 
85 99
     }
86 100
 
87
-  }, [times])
101
+  }, [state, times])
88 102
 
89 103
   onShakeEnd(() => {
90 104
     // 摇一摇结束
@@ -92,7 +106,7 @@ const IndexPage = (props) => {
92 106
     Taro.showToast({
93 107
       title: '游戏结束',
94 108
       icon: 'none',
95
-      duration: 2000
109
+      duration: 5000
96 110
 
97 111
 
98 112
     }).then(e => {
@@ -101,6 +115,7 @@ const IndexPage = (props) => {
101 115
         setBtnDisable(false)
102 116
         console.log(" 摇一摇结束")
103 117
         setbuttonTitle(false)
118
+        setstate(false)
104 119
 
105 120
 
106 121
       })
@@ -152,6 +167,11 @@ const IndexPage = (props) => {
152 167
         modRef.current = 'countdown'
153 168
         startCount(3)
154 169
         setBtnDisable(true)
170
+
171
+        // startShake(() => {
172
+        //   console.log('kaissssssssssssssssss',);
173
+        // })
174
+
155 175
       }
156 176
     }
157 177
 
@@ -178,10 +198,13 @@ const IndexPage = (props) => {
178 198
       if (count === 0) {
179 199
         modRef.current = 'shake'
180 200
         startShake()
201
+        setNum('Go!')
202
+
181 203
       }
182 204
     }
183 205
   }, [count])
184 206
 
207
+
185 208
   return (
186 209
     <View className='page-wrapper page-index'>
187 210
       <Counter className='shake-bg' value={num} />

+ 12
- 12
src/utils/shake.js Wyświetl plik

@@ -19,21 +19,21 @@ export default function Shake() {
19 19
     //如果这次摇的时间距离上次摇的时间有一定间隔 才执行
20 20
     const diff = 20;
21 21
     if (nowTime - shakeData.lastTime > diff) {
22
-     var diffTime = nowTime - shakeData.lastTime; //记录时间段
23
-     shakeData.lastTime = nowTime; //记录本次摇动时间,为下次计算摇动时间做准备
24
-     shakeData.x = acceleration.x; //获取 x 轴数值,x 轴为垂直于北轴,向东为正
25
-     shakeData.y = acceleration.y; //获取 y 轴数值,y 轴向正北为正
26
-     shakeData.z = acceleration.z; //获取 z 轴数值,z 轴垂直于地面,向上为正
27
-     //计算 公式的意思是 单位时间内运动的路程,即为我们想要的速度
28
-     var speed = Math.abs(shakeData.x + shakeData.y + shakeData.z - shakeData.lastX - shakeData.lastY - shakeData.lastZ) / diffTime * 10000;
29
-     //console.log(speed)
30
-     if (speed > shakeData.shakeSpeed) { //如果计算出来的速度超过了阈值,那么就算作用户成功摇一摇
22
+      var diffTime = nowTime - shakeData.lastTime; //记录时间段
23
+      shakeData.lastTime = nowTime; //记录本次摇动时间,为下次计算摇动时间做准备
24
+      shakeData.x = acceleration.x; //获取 x 轴数值,x 轴为垂直于北轴,向东为正
25
+      shakeData.y = acceleration.y; //获取 y 轴数值,y 轴向正北为正
26
+      shakeData.z = acceleration.z; //获取 z 轴数值,z 轴垂直于地面,向上为正
27
+      //计算 公式的意思是 单位时间内运动的路程,即为我们想要的速度
28
+      var speed = Math.abs(shakeData.x + shakeData.y + shakeData.z - shakeData.lastX - shakeData.lastY - shakeData.lastZ) / diffTime * 10000;
29
+      //console.log(speed)
30
+      if (speed > shakeData.shakeSpeed) { //如果计算出来的速度超过了阈值,那么就算作用户成功摇一摇
31 31
         next();
32 32
       }
33 33
 
34
-     shakeData.lastX = shakeData.x; //赋值,为下一次计算做准备
35
-     shakeData.lastY = shakeData.y; //赋值,为下一次计算做准备
36
-     shakeData.lastZ = shakeData.z; //赋值,为下一次计算做准备
34
+      shakeData.lastX = shakeData.x; //赋值,为下一次计算做准备
35
+      shakeData.lastY = shakeData.y; //赋值,为下一次计算做准备
36
+      shakeData.lastZ = shakeData.z; //赋值,为下一次计算做准备
37 37
     }
38 38
   }
39 39
 

+ 108
- 34
src/utils/shape-shifter.js Wyświetl plik

@@ -14,6 +14,7 @@
14 14
 
15 15
 
16 16
 const S = {
17
+
17 18
   init: function (conf) {
18 19
     S.Drawing.init(conf);
19 20
     S.ShapeBuilder.init(conf);
@@ -23,18 +24,64 @@ const S = {
23 24
     });
24 25
   },
25 26
   performAction: function (value) {
27
+    console.log("🚀 ~ file: shape-shifter.js ~ line 27 ~ value", value)
28
+    const a = new Date()
29
+    console.log(a);
26 30
     const fast = true;
27 31
     S.Shape.switchShape(S.ShapeBuilder.letter(value), fast);
32
+    // let currentAction
33
+    // let interval
34
+
35
+    // function timedAction(fn, delay, max, reverse) {
36
+    //   clearInterval(interval);
37
+    //   currentAction = max;
38
+    //   fn(currentAction);
39
+
40
+    //     interval = setInterval(function () {
41
+    //       currentAction = currentAction - 1;
42
+    //       fn(currentAction);
43
+
44
+    //       if ((!reverse && max && currentAction === max) || (reverse && currentAction === 0)) {
45
+    //         clearInterval(interval);
46
+    //       }
47
+    //     }, delay);
48
+    // }
49
+    // switch ('countdown') {
50
+    //   case 'countdown':
51
+    //     value = parseInt(value) || 10;
52
+    //     value = value > 0 ? value : 10;
53
+
54
+    //     timedAction(function (index) {
55
+    //       if (index === 0) {
56
+    //         if (sequence.length === 0) {
57
+    //           S.Shape.switchShape(S.ShapeBuilder.letter(''));
58
+    //         } else {
59
+    //           performAction(sequence);
60
+    //         }
61
+    //       } else {
62
+    //         S.Shape.switchShape(S.ShapeBuilder.letter(index), true);
63
+    //       }
64
+    //     }, 1000, value, true);
65
+    //     break;
66
+
67
+    //   default:
68
+    // }
28 69
   }
70
+
71
+
29 72
 };
30 73
 
31 74
 S.Drawing = (function () {
32 75
   let canvas,
33
-      context,
34
-      renderFn,
35
-      requestFrame = function(callback) {
36
-                      setTimeout(callback, 1000 / 60);
37
-                    };
76
+    context,
77
+    renderFn,
78
+    requestFrame = function (callback) {
79
+      setTimeout(callback, 1000 / 100); //气泡分散集合 用到的时间
80
+    };
81
+
82
+
83
+
84
+
38 85
 
39 86
   return {
40 87
     init: function (conf) {
@@ -70,9 +117,29 @@ S.Drawing = (function () {
70 117
       context.closePath();
71 118
       context.fill();
72 119
     }
120
+
121
+
73 122
   }
74 123
 }());
75 124
 
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
76 143
 S.Point = function (args) {
77 144
   this.x = args.x;
78 145
   this.y = args.y;
@@ -91,7 +158,7 @@ S.Color = function (r, g, b, a) {
91 158
 
92 159
 S.Color.prototype = {
93 160
   render: function () {
94
-    return 'rgba(' + this.r + ',' +  + this.g + ',' + this.b + ',' + this.a + ')';
161
+    return 'rgba(' + this.r + ',' + + this.g + ',' + this.b + ',' + this.a + ')';
95 162
   }
96 163
 };
97 164
 
@@ -132,10 +199,10 @@ S.Dot.prototype = {
132 199
 
133 200
   _moveTowards: function (n) {
134 201
     let details = this.distanceTo(n, true),
135
-        dx = details[0],
136
-        dy = details[1],
137
-        d = details[2],
138
-        e = this.e * d;
202
+      dx = details[0],
203
+      dy = details[1],
204
+      d = details[2],
205
+      e = this.e * d;
139 206
 
140 207
     if (this.p.h === -1) {
141 208
       this.p.x = n.x;
@@ -186,10 +253,12 @@ S.Dot.prototype = {
186 253
     this.p.z = Math.max(1, this.p.z - (d * 0.05));
187 254
   },
188 255
 
256
+
257
+
189 258
   distanceTo: function (n, details) {
190 259
     let dx = this.p.x - n.x,
191
-        dy = this.p.y - n.y,
192
-        d = Math.sqrt(dx * dx + dy * dy);
260
+      dy = this.p.y - n.y,
261
+      d = Math.sqrt(dx * dx + dy * dy);
193 262
 
194 263
     return details ? [dx, dy, d] : d;
195 264
   },
@@ -209,10 +278,10 @@ S.Dot.prototype = {
209 278
 
210 279
 S.ShapeBuilder = (function () {
211 280
   let gap = 13,
212
-      shapeCanvas,
213
-      shapeContext,
214
-      fontSize = 500,
215
-      fontFamily = 'Avenir, Helvetica Neue, Helvetica, Arial, sans-serif';
281
+    shapeCanvas,
282
+    shapeContext,
283
+    fontSize = 500,
284
+    fontFamily = 'Avenir, Helvetica Neue, Helvetica, Arial, sans-serif';
216 285
 
217 286
   function fit({ width, height }) {
218 287
     shapeCanvas.width = Math.floor(width / gap) * gap;
@@ -224,13 +293,13 @@ S.ShapeBuilder = (function () {
224 293
 
225 294
   function processCanvas() {
226 295
     let pixels = shapeContext.getImageData(0, 0, shapeCanvas.width, shapeCanvas.height).data,
227
-        dots = [],
228
-        x = 0,
229
-        y = 0,
230
-        fx = shapeCanvas.width,
231
-        fy = shapeCanvas.height,
232
-        w = 0,
233
-        h = 0;
296
+      dots = [],
297
+      x = 0,
298
+      y = 0,
299
+      fx = shapeCanvas.width,
300
+      fy = shapeCanvas.height,
301
+      w = 0,
302
+      h = 0;
234 303
 
235 304
     for (let p = 0; p < pixels.length; p += (4 * gap)) {
236 305
       if (pixels[p + 3] > 0) {
@@ -250,7 +319,7 @@ S.ShapeBuilder = (function () {
250 319
       if (x >= shapeCanvas.width) {
251 320
         x = 0;
252 321
         y += gap;
253
-        p += gap * 4 * shapeCanvas.width;
322
+        p += gap * 4 * shapeCanvas.width; //数字分散程度
254 323
       }
255 324
     }
256 325
 
@@ -276,12 +345,12 @@ S.ShapeBuilder = (function () {
276 345
   return {
277 346
     init,
278 347
     letter: function (l) {
279
-      let s = 0;
348
+      let s = 0.;
280 349
 
281 350
       setFontSize(fontSize);
282 351
       s = Math.min(fontSize,
283
-                  (shapeCanvas.width / shapeContext.measureText(l).width) * 0.8 * fontSize, 
284
-                  (shapeCanvas.height / fontSize) * (isNumber(l) ? 1 : 0.45) * fontSize);
352
+        (shapeCanvas.width / shapeContext.measureText(l).width) * 0.8 * fontSize,
353
+        (shapeCanvas.height / fontSize) * (isNumber(l) ? 1 : 0.45) * fontSize);
285 354
       setFontSize(s);
286 355
 
287 356
       shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
@@ -295,10 +364,10 @@ S.ShapeBuilder = (function () {
295 364
 
296 365
 S.Shape = (function () {
297 366
   let dots = [],
298
-      width = 0,
299
-      height = 0,
300
-      cx = 0,
301
-      cy = 0;
367
+    width = 0,
368
+    height = 0,
369
+    cx = 0,
370
+    cy = 0;
302 371
 
303 372
   function compensate() {
304 373
     let a = S.Drawing.getArea();
@@ -323,7 +392,7 @@ S.Shape = (function () {
323 392
 
324 393
     switchShape: function (n, fast) {
325 394
       let size,
326
-          a = S.Drawing.getArea();
395
+        a = S.Drawing.getArea();
327 396
 
328 397
       width = n.w;
329 398
       height = n.h;
@@ -338,7 +407,7 @@ S.Shape = (function () {
338 407
       }
339 408
 
340 409
       let d = 0,
341
-          i = 0;
410
+        i = 0;
342 411
 
343 412
       while (n.dots.length > 0) {
344 413
         i = Math.floor(Math.random() * n.dots.length);
@@ -380,7 +449,7 @@ S.Shape = (function () {
380 449
 
381 450
           dots[i].s = false;
382 451
           dots[i].e = 0.04;
383
-          dots[i].move(new S.Point({ 
452
+          dots[i].move(new S.Point({
384 453
             x: Math.random() * a.w,
385 454
             y: Math.random() * a.h,
386 455
             a: 0.3, //.4
@@ -400,3 +469,8 @@ S.Shape = (function () {
400 469
 }());
401 470
 
402 471
 export default S;
472
+
473
+
474
+
475
+
476
+