张延森 3 年前
父节点
当前提交
b573a7e28e
共有 4 个文件被更改,包括 108 次插入65 次删除
  1. 85
    0
      src/components/Counter/index.jsx
  2. 17
    0
      src/components/Counter/style.less
  3. 5
    64
      src/components/Shake/index.jsx
  4. 1
    1
      src/pages/index/index.jsx

+ 85
- 0
src/components/Counter/index.jsx 查看文件

@@ -0,0 +1,85 @@
1
+import { View, Canvas } from '@tarojs/components'
2
+import Taro from '@tarojs/taro'
3
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
4
+import S from '@/utils/shape-shifter'
5
+
6
+import './style.less'
7
+
8
+const ShakeCount = (props) => {
9
+  const { className, value } = props
10
+  const classNames = useMemo(() => [className, 'canvas-counter'].filter(Boolean).join(' '), [className])
11
+
12
+  const canvasId = useMemo(() => `f-${Math.random().toString(36).substring(2)}`, [])
13
+  const canvasRef = useRef()
14
+  const canvasShapeRef = useRef()
15
+  const isReady = useRef(false)
16
+
17
+  const initS = useCallback(() => {
18
+    if (canvasRef.current && canvasShapeRef.current) {
19
+
20
+      S.init({
21
+        canvas: canvasRef.current,
22
+        shape: canvasShapeRef.current,
23
+        width: canvasRef.current.width,
24
+        height: canvasRef.current.height
25
+      })
26
+
27
+      isReady.current = true
28
+    }
29
+  }, [])
30
+
31
+  useEffect(() => {
32
+    if (isReady.current) {
33
+      S.performAction(value)
34
+    }
35
+  }, [value])
36
+
37
+  useEffect(() => {
38
+    Taro.nextTick(() => {
39
+      Taro.createSelectorQuery()
40
+        .select(`#${canvasId}`)
41
+        .fields({ node: true, size: true })
42
+        .exec((res) => {
43
+          const canvas = res[0].node
44
+          const ctx = canvas.getContext('2d')
45
+
46
+          const dpr = Taro.getSystemInfoSync().pixelRatio
47
+          const width = res[0].width * dpr
48
+          const height = res[0].height * dpr
49
+          canvas.width = width
50
+          canvas.height = height
51
+          ctx.scale(dpr, dpr)
52
+          
53
+          canvasRef.current = canvas
54
+          initS()
55
+        })
56
+
57
+      Taro.createSelectorQuery()
58
+        .select(`#${canvasId}-canvas-shape`)
59
+        .fields({ node: true, size: true })
60
+        .exec((res) => {
61
+          const canvas = res[0].node
62
+          const ctx = canvas.getContext('2d')
63
+
64
+          const dpr = Taro.getSystemInfoSync().pixelRatio
65
+          const width = res[0].width * dpr
66
+          const height = res[0].height * dpr
67
+          canvas.width = width
68
+          canvas.height = height
69
+          ctx.scale(dpr, dpr)
70
+          
71
+          canvasShapeRef.current = canvas
72
+          initS()
73
+        })
74
+    })
75
+  }, [canvasId, initS])
76
+
77
+  return (
78
+    <View className={classNames}>
79
+      <Canvas id={`${canvasId}-canvas-shape`} className='canvas-shape' type='2d'></Canvas>
80
+      <Canvas id={canvasId} type='2d'></Canvas>
81
+    </View>
82
+  )
83
+}
84
+
85
+export default ShakeCount;

+ 17
- 0
src/components/Counter/style.less 查看文件

@@ -0,0 +1,17 @@
1
+.canvas-counter {
2
+  width: 100%;
3
+  height: 100%;
4
+
5
+  canvas {
6
+    width: 100%;
7
+    height: 100%;
8
+    position: absolute;
9
+    top: 0;
10
+    left: 0;
11
+
12
+    &.canvas-shape {
13
+      top: -200vw;
14
+      left: -200vh;
15
+    }
16
+  }
17
+}

+ 5
- 64
src/components/Shake/index.jsx 查看文件

@@ -1,6 +1,7 @@
1 1
 import { View, Canvas } from '@tarojs/components'
2 2
 import Taro, { useDidHide, useDidShow } from '@tarojs/taro'
3 3
 import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
4
+import Counter from '@/components/Counter'
4 5
 import { throttle } from '@/utils'
5 6
 import newShake from '@/utils/shake'
6 7
 import S from '@/utils/shape-shifter'
@@ -13,37 +14,18 @@ const ShakeCount = (props) => {
13 14
   const { className, onTimes } = props
14 15
   const classNames = useMemo(() => [className, 'shake-count'].filter(Boolean).join(' '), [className])
15 16
 
16
-  // const [times, setTimes] = useState(0)
17
-  const timesRef = useRef(0)
18
-  // timesRef.current = times
19
-
20
-  const canvasId = useMemo(() => `f-${Math.random().toString(36).substring(2)}`, [])
21
-  const canvasRef = useRef()
22
-  const canvasShapeRef = useRef()
17
+  const [times, setTimes] = useState(0)
18
+  const timesRef = useRef(times)
23 19
 
24 20
   // eslint-disable-next-line react-hooks/exhaustive-deps
25 21
   const handleShake = useCallback(throttle((times) => {
26
-    S.performAction(times)
22
+    setTimes(times)
27 23
 
28 24
     if (onTimes) {
29 25
       onTimes(times)
30 26
     }
31 27
   }, 500, { trailing: true, immediate: true }), [])
32 28
 
33
-  const initS = useCallback(() => {
34
-    if (canvasRef.current && canvasShapeRef.current) {
35
-
36
-      S.init({
37
-        canvas: canvasRef.current,
38
-        shape: canvasShapeRef.current,
39
-        width: canvasRef.current.width,
40
-        height: canvasRef.current.height
41
-      })
42
-
43
-      S.performAction(0)
44
-    }
45
-  }, [])
46
-
47 29
   useDidShow(() => {
48 30
     Taro.startAccelerometer()
49 31
 
@@ -61,50 +43,9 @@ const ShakeCount = (props) => {
61 43
     Taro.startAccelerometer()
62 44
   })
63 45
 
64
-  useEffect(() => {
65
-    Taro.nextTick(() => {
66
-      Taro.createSelectorQuery()
67
-        .select(`#${canvasId}`)
68
-        .fields({ node: true, size: true })
69
-        .exec((res) => {
70
-          const canvas = res[0].node
71
-          const ctx = canvas.getContext('2d')
72
-
73
-          const dpr = Taro.getSystemInfoSync().pixelRatio
74
-          const width = res[0].width * dpr
75
-          const height = res[0].height * dpr
76
-          canvas.width = width
77
-          canvas.height = height
78
-          ctx.scale(dpr, dpr)
79
-          
80
-          canvasRef.current = canvas
81
-          initS()
82
-        })
83
-
84
-      Taro.createSelectorQuery()
85
-        .select(`#${canvasId}-canvas-shape`)
86
-        .fields({ node: true, size: true })
87
-        .exec((res) => {
88
-          const canvas = res[0].node
89
-          const ctx = canvas.getContext('2d')
90
-
91
-          const dpr = Taro.getSystemInfoSync().pixelRatio
92
-          const width = res[0].width * dpr
93
-          const height = res[0].height * dpr
94
-          canvas.width = width
95
-          canvas.height = height
96
-          ctx.scale(dpr, dpr)
97
-          
98
-          canvasShapeRef.current = canvas
99
-          initS()
100
-        })
101
-    })
102
-  }, [canvasId, initS])
103
-
104 46
   return (
105 47
     <View className={classNames}>
106
-      <Canvas id={`${canvasId}-canvas-shape`} className='canvas-shape' type='2d'></Canvas>
107
-      <Canvas id={canvasId} type='2d'></Canvas>
48
+      <Counter value={times} />
108 49
     </View>
109 50
   )
110 51
 }

+ 1
- 1
src/pages/index/index.jsx 查看文件

@@ -12,7 +12,7 @@ const IndexPage = (props) => {
12 12
       <ShakeCount className='shake-bg' />
13 13
       <TopBar />
14 14
       <View style={{ width: '80vw', position: 'fixed', bottom: '2em', left: '10vw' }} >
15
-        <Button block round color='linear-gradient(to right, #4bb0ff, #6149f6)'>开 始</Button>
15
+        <Button block round color='linear-gradient(to right, rgb(255, 96, 52), rgb(238, 10, 36))'>开 始</Button>
16 16
       </View>      
17 17
     </View>
18 18
   )