1
- import React , { useEffect , useRef , useState , PointerEvent } from "react" ;
1
+ import React , {
2
+ useEffect ,
3
+ useRef ,
4
+ useState ,
5
+ PointerEvent ,
6
+ useMemo ,
7
+ } from "react" ;
2
8
import {
3
9
ClockBackground ,
4
10
ClockCenter ,
5
11
ClockHandler ,
6
12
Container ,
7
- Graduation ,
8
13
MainClock ,
9
- } from "./Clock.style " ;
14
+ } from "./Clock.styled " ;
10
15
import {
11
16
getPointerPosFromCenter ,
12
17
getRotationDegree ,
@@ -29,6 +34,7 @@ import {
29
34
clockTimeUnitAtom as CTU ,
30
35
isIntroTimeoutedAtom as IIT ,
31
36
} from "../../shared/atom" ;
37
+ import Graduation from "./Graduation/Graduation" ;
32
38
33
39
let canSetClockDegree = false ;
34
40
let isOverLimited = false ;
@@ -38,17 +44,17 @@ export default function Clock() {
38
44
const backgroundRef = useRef < HTMLDivElement > ( null ) ;
39
45
const handlerRef = useRef < HTMLDivElement > ( null ) ;
40
46
const resizeRef = useRef < HTMLDivElement > ( null ) ;
41
- const [ moveAreaPos , setMoveAreaPos ] = useState < Vector2 > ( new Vector2 ( 0 , 0 ) ) ;
42
- const [ centerPos , setCenterPos ] = useState < Vector2 > ( new Vector2 ( 0 , 0 ) ) ;
43
- const [ prevRelPos , setPrevRelPos ] = useState < Vector2 | null > ( null ) ;
44
47
const isTimingNow = useRecoilValue ( ITN ) ;
45
- const [ isClockPointerDown , setIsClockPointerDown ] = useRecoilState ( ICPD ) ;
46
- const setClockSize = useSetRecoilState ( CS ) ;
47
- const [ clockDegree , setClockDegree ] = useRecoilState ( CD ) ;
48
48
const clockColor = useRecoilValue ( CCV ) ;
49
49
const maxClockTime = useRecoilValue ( MCT ) ;
50
50
const clockTimeUnit = useRecoilValue ( CTU ) ;
51
51
const isIntroTimeouted = useRecoilValue ( IIT ) ;
52
+ const setClockSize = useSetRecoilState ( CS ) ;
53
+ const [ moveAreaPos , setMoveAreaPos ] = useState < Vector2 > ( new Vector2 ( 0 , 0 ) ) ;
54
+ const [ centerPos , setCenterPos ] = useState < Vector2 > ( new Vector2 ( 0 , 0 ) ) ;
55
+ const [ prevRelPos , setPrevRelPos ] = useState < Vector2 | null > ( null ) ;
56
+ const [ clockDegree , setClockDegree ] = useRecoilState ( CD ) ;
57
+ const [ isClockPointerDown , setIsClockPointerDown ] = useRecoilState ( ICPD ) ;
52
58
53
59
const onPointerDown = ( e : PointerEvent ) => {
54
60
canSetClockDegree = true ;
@@ -96,29 +102,16 @@ export default function Clock() {
96
102
setIsClockPointerDown ( false ) ;
97
103
} ;
98
104
99
- const parseIndexToGraduationValue = ( index : number ) => {
100
- const gValue = Math . round ( ( index * maxClockTime ) / 6 ) / 10 ;
101
-
102
- // If gValue is float
103
- if ( gValue !== Math . floor ( gValue ) ) {
104
- const float = gValue - Math . floor ( gValue ) ;
105
- return (
106
- < span >
107
- { Math . floor ( gValue ) }
108
- < span className = "min" > /{ 60 * float } </ span >
109
- </ span >
110
- ) ;
111
- }
112
-
113
- return < span > { gValue } </ span > ;
114
- } ;
115
-
116
105
const rebaseDegree = ( ) => {
117
106
setClockDegree ( rebaseClockDegree ( clockDegree , maxClockTime ) ) ;
118
107
} ;
119
108
120
109
const isIntroedOnce = ( ) => isIntroTimeouted ;
121
110
111
+ const Graduations = useMemo ( ( ) => {
112
+ return range ( 60 ) . map ( ( i ) => < Graduation key = { i } index = { i } /> ) ;
113
+ } , [ ] ) ;
114
+
122
115
useEffect ( ( ) => {
123
116
if ( isClockPointerDown ) return ;
124
117
rebaseDegree ( ) ;
@@ -165,11 +158,16 @@ export default function Clock() {
165
158
const onResize = ( ) => {
166
159
const stageWidth = document . body . clientWidth ;
167
160
const stageHeight = document . body . clientHeight ;
161
+ const HORIZONTAL_PADDING_SIZE = 190 ;
162
+ const CLOCK_SIZE = 600 ;
168
163
169
- const resizedWidth = Math . min ( 600 , stageWidth ) ;
170
- const resizedHeight = Math . min ( 600 + 190 , stageHeight - 190 ) ;
164
+ const resizedWidth = Math . min ( CLOCK_SIZE , stageWidth ) ;
165
+ const resizedHeight = Math . min (
166
+ CLOCK_SIZE + HORIZONTAL_PADDING_SIZE ,
167
+ stageHeight - HORIZONTAL_PADDING_SIZE
168
+ ) ;
171
169
const resultSize = Math . min ( resizedWidth , resizedHeight ) ;
172
- const resultScaleRatio = Math . min ( 1 , resultSize / 600 ) ;
170
+ const resultScaleRatio = Math . min ( 1 , resultSize / CLOCK_SIZE ) ;
173
171
174
172
resizeRef . current ! . style . transform = `
175
173
scale(${ resultScaleRatio } )
@@ -203,21 +201,7 @@ export default function Clock() {
203
201
onPointerMove = { onPointerMove }
204
202
>
205
203
< ClockCenter >
206
- { range ( 60 ) . map ( ( i ) => (
207
- < Graduation
208
- rotate = { i * 6 }
209
- accent = { i % 5 == 0 }
210
- gap = { 24 }
211
- key = { i }
212
- spanAccent = { 360 - clockDegree >= i * 6 }
213
- >
214
- { i %
215
- ( maxClockTime >= 60 ? 5 : 5 * Math . round ( 30 / maxClockTime ) ) ==
216
- 0
217
- ? parseIndexToGraduationValue ( i )
218
- : null }
219
- </ Graduation >
220
- ) ) }
204
+ { Graduations }
221
205
< ClockBackground ref = { backgroundRef } />
222
206
< ClockHandler ref = { handlerRef } >
223
207
< div className = "pointer" > </ div >
0 commit comments