@@ -7,13 +7,30 @@ const Circle = () => {
7
7
const [ circleClicked , setCircleClicked ] = useState ( 0 ) ;
8
8
const [ totalClicked , setTotalClicked ] = useState ( 0 ) ;
9
9
const [ timeLeft , setTimeLeft ] = useState ( 10 ) ;
10
- const screenWidth = window . innerWidth ;
11
- const screenHeight = window . innerHeight ;
10
+ const [ screenSize , setScreenSize ] = useState ( { width : 0 , height : 0 } ) ;
11
+
12
+ useEffect ( ( ) => {
13
+ const updateScreenSize = ( ) => {
14
+ setScreenSize ( {
15
+ width : window . innerWidth ,
16
+ height : window . innerHeight ,
17
+ } ) ;
18
+ } ;
19
+
20
+ updateScreenSize ( ) ;
21
+ window . addEventListener ( "resize" , updateScreenSize ) ;
22
+
23
+ return ( ) => {
24
+ window . removeEventListener ( "resize" , updateScreenSize ) ;
25
+ } ;
26
+ } , [ ] ) ;
27
+
12
28
const position = useRef ( {
13
- top : Math . floor ( Math . random ( ) * ( screenHeight - radius * 2 ) ) ,
14
- left : Math . floor ( Math . random ( ) * ( screenWidth - radius * 2 ) ) ,
29
+ top : Math . floor ( Math . random ( ) * ( screenSize . height - radius * 2 ) ) ,
30
+ left : Math . floor ( Math . random ( ) * ( screenSize . width - radius * 2 ) ) ,
15
31
transform : "translate(-50%, -50%)" ,
16
32
} ) ;
33
+
17
34
const clicked = ( ) => {
18
35
setCircleClicked ( ( prevCount ) => {
19
36
console . log ( "outside: " , circleClicked ) ;
@@ -26,11 +43,12 @@ const Circle = () => {
26
43
console . log ( "Total click:" , totalClicked ) ;
27
44
} ;
28
45
29
- const accuracy = ( circleClicked / totalClicked ) * 100 ;
46
+ const accuracy = totalClicked > 0 ? ( circleClicked / totalClicked ) * 100 : 0 ;
30
47
31
48
const circleClickedFunc = ( ) => {
32
49
setRadius ( 0 ) ;
33
50
} ;
51
+
34
52
useEffect ( ( ) => {
35
53
const interval = setInterval ( ( ) => {
36
54
if ( timeLeft > 0 ) {
@@ -63,16 +81,16 @@ const Circle = () => {
63
81
} , [ ] ) ;
64
82
65
83
useEffect ( ( ) => {
66
- if ( radius == 0 ) {
67
- const newTop = Math . floor ( Math . random ( ) * ( screenHeight - radius * 2 ) ) ;
68
- const newLeft = Math . floor ( Math . random ( ) * ( screenWidth - radius * 2 ) ) ;
84
+ if ( radius === 0 ) {
85
+ const newTop = Math . floor ( Math . random ( ) * ( screenSize . height - radius * 2 ) ) ;
86
+ const newLeft = Math . floor ( Math . random ( ) * ( screenSize . width - radius * 2 ) ) ;
69
87
position . current = {
70
88
...position . current ,
71
89
top : newTop ,
72
90
left : newLeft ,
73
91
} ;
74
92
}
75
- } , [ increasing , radius , screenHeight , screenWidth ] ) ;
93
+ } , [ increasing , radius , screenSize . height , screenSize . width ] ) ;
76
94
77
95
return (
78
96
< >
@@ -84,7 +102,6 @@ const Circle = () => {
84
102
style = { position . current }
85
103
>
86
104
< div
87
- // onClick={clicked()}
88
105
onClick = { circleClickedFunc }
89
106
className = "rounded-full bg-blue-500"
90
107
style = { { width : radius , height : radius } }
@@ -94,7 +111,7 @@ const Circle = () => {
94
111
) : (
95
112
< div className = "h-screen w-full flex flex-col justify-center items-center" >
96
113
< h1 > Time's up</ h1 >
97
- < h1 > Your accuray : { accuracy } </ h1 >
114
+ < h1 > Your accuracy : { accuracy . toFixed ( 2 ) } % </ h1 >
98
115
</ div >
99
116
) }
100
117
</ >
0 commit comments