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