@@ -3,7 +3,7 @@ import {AlgorithInterface, BFSAlgorithm, AstarAlgorithm, Point} from './Algorith
3
3
4
4
type CellType = 'start' | 'end' | 'disabled' | 'used' | 'default'
5
5
6
- const TIMEOUT = 30 ;
6
+ const TIMEOUT = 50 ;
7
7
const CELLSIZE = 48 ;
8
8
const colorType : { disabled : string ; start : string ; end : string ; used : string ; default : string ; } = {
9
9
disabled : 'bg-slate-400' ,
@@ -16,6 +16,12 @@ const algorithms = [
16
16
BFSAlgorithm ,
17
17
AstarAlgorithm ,
18
18
] ;
19
+ const calculateWidthHeight = ( e : HTMLDivElement ) : [ number , number ] => {
20
+ let width = ( e . offsetWidth - 24 ) / CELLSIZE ;
21
+ width = width % 10 === 0 ? width - 1 : Math . floor ( width ) ;
22
+ const height = Math . floor ( e . offsetHeight / CELLSIZE ) ;
23
+ return [ width , height ] ;
24
+ } ;
19
25
const cellTypes : CellType [ ] = [ 'start' , 'end' , 'disabled' , 'used' , 'default' ] ;
20
26
21
27
function App ( ) {
@@ -31,6 +37,7 @@ function App() {
31
37
const [ endPoint , setEndPoint ] = useState < number > ( 1 ) ;
32
38
const [ disabledCells , setDisabledCells ] = useState < Set < number > > ( new Set ( ) ) ;
33
39
const [ usedCells , setUsedCells ] = useState < Set < number > > ( new Set ( ) ) ;
40
+ const windowRef = useRef < HTMLDivElement > ( null ) ;
34
41
const fieldRef = useRef < HTMLDivElement > ( null ) ;
35
42
const selectRef = useRef < HTMLSelectElement > ( null ) ;
36
43
@@ -107,16 +114,6 @@ function App() {
107
114
setDisabledCells ( new Set ( ) ) ;
108
115
} ;
109
116
110
- useEffect ( ( ) => {
111
- if ( ! fieldRef . current ) return ;
112
- let width = fieldRef . current . offsetWidth / CELLSIZE ;
113
- width = width % 10 === 0 ? width - 1 : Math . floor ( width ) ;
114
- const height = Math . floor ( fieldRef . current . offsetHeight / CELLSIZE ) ;
115
- setWidth ( width ) ;
116
- setHeight ( height ) ;
117
- setAmount ( width * height ) ;
118
- } , [ fieldRef ] ) ;
119
-
120
117
useEffect ( ( ) => {
121
118
if ( ! fill ) return ;
122
119
const mouseEvent = ( event : MouseEvent ) => {
@@ -133,16 +130,39 @@ function App() {
133
130
useEffect ( ( ) => {
134
131
const mouseDown = ( event : MouseEvent ) => setFill ( true ) ;
135
132
const mouseUp = ( event : MouseEvent ) => setFill ( false ) ;
133
+ const resize = ( event : Event ) => {
134
+ if ( ! windowRef . current || ! fieldRef . current ) return ;
135
+ windowRef . current . style . height = `${ window . innerHeight } px` ;
136
+ setStarted ( false ) ;
137
+ setCount ( 1 ) ;
138
+ setUsedCells ( new Set ( ) ) ;
139
+ setDisabledCells ( new Set ( ) ) ;
140
+ const [ width , height ] = calculateWidthHeight ( fieldRef . current ) ;
141
+ setWidth ( width ) ;
142
+ setHeight ( height ) ;
143
+ setAmount ( width * height ) ;
144
+ } ;
136
145
146
+ addEventListener ( 'resize' , resize ) ;
137
147
addEventListener ( 'mousedown' , mouseDown ) ;
138
148
addEventListener ( 'mouseup' , mouseUp ) ;
139
149
140
150
return ( ) => {
151
+ removeEventListener ( 'resize' , resize ) ;
141
152
removeEventListener ( 'mousedown' , mouseDown ) ;
142
153
removeEventListener ( 'mouseup' , mouseUp ) ;
143
154
} ;
144
155
} ) ;
145
156
157
+ useEffect ( ( ) => {
158
+ if ( ! windowRef . current || ! fieldRef . current ) return ;
159
+ windowRef . current . style . height = `${ window . innerHeight } px` ;
160
+ const [ width , height ] = calculateWidthHeight ( fieldRef . current ) ;
161
+ setWidth ( width ) ;
162
+ setHeight ( height ) ;
163
+ setAmount ( width * height ) ;
164
+ } , [ windowRef , fieldRef ] ) ;
165
+
146
166
useEffect ( ( ) => {
147
167
if ( ! started || ! algo ) return ;
148
168
const timeout = setTimeout ( ( ) => {
@@ -156,10 +176,10 @@ function App() {
156
176
} , [ count , started , algo ] ) ;
157
177
158
178
return (
159
- < div className = 'w-screen h -screen flex flex-col overflow-hidden' >
179
+ < div ref = { windowRef } className = 'w-screen flex flex-col overflow-hidden' >
160
180
< div className = 'h=[30px] flex flex-row items-center justify-center' >
161
181
< div className = 'h-10 m-1 px-3 text-white font-bold flex items-center' >
162
- < span className = 'h-10 bg-cyan-400 p-2' > Algorithm</ span >
182
+ < span className = 'h-10 bg-cyan-400 p-2 hidden md:block ' > Algorithm</ span >
163
183
< select ref = { selectRef } className = 'h-10 bg-cyan-400' name = "algorithms" id = "algorithms" >
164
184
{ algorithms . map ( ( a , i ) => < option key = { a . title } value = { i } > { a . title } </ option > ) }
165
185
</ select >
@@ -186,7 +206,7 @@ function App() {
186
206
< span > { count } </ span >
187
207
</ div >
188
208
</ div >
189
- < div ref = { fieldRef } className = 'flex-grow h-full flex flex-row flex-wrap p-3' >
209
+ < div ref = { fieldRef } className = 'flex-grow h-full flex justify-center flex-row flex-wrap p-3' >
190
210
{ cells }
191
211
</ div >
192
212
</ div >
0 commit comments