1
+ const boxGrid = document . getElementById ( "boxgrid" ) ;
2
+ const button = document . getElementById ( "resolution" ) ;
3
+ const colorChooser = document . getElementById ( "colorChooser" ) ;
4
+
5
+ const gridWidth = 960 ;
6
+
7
+ var random = function ( number ) {
8
+ // random number from 0 to number
9
+ return Math . floor ( Math . random ( ) * ( number + 1 ) ) ;
10
+ }
11
+
12
+
13
+ var drawCanvas = function ( boxesPerSide ) {
14
+ for ( let i = 1 ; i <= boxesPerSide ; i ++ ) {
15
+ for ( let j = 1 ; j <= boxesPerSide ; j ++ ) {
16
+ const box = document . createElement ( "div" ) ;
17
+ box . classList . add ( "col" + j . toString ( ) ) ;
18
+ box . classList . add ( "row" + i . toString ( ) ) ;
19
+ box . classList . add ( "l100" ) ;
20
+
21
+ boxGrid . appendChild ( box ) ;
22
+
23
+ box . addEventListener ( "mouseenter" , ( ) => {
24
+ if ( colorChooser . value === "cl" ) {
25
+ box . style . backgroundColor = `hsl(${ random ( 360 ) } , ${ random ( 100 ) } %, ${ random ( 100 ) } %)` ;
26
+ } else {
27
+ // pick lXX for lightness value
28
+ let className = String ( box . classList ) ;
29
+ let lightness = + className . slice ( className . lastIndexOf ( "l" ) + 1 , className . length ) ;
30
+ if ( lightness > 0 ) {
31
+ box . classList . remove ( `l${ lightness } ` ) ;
32
+ lightness -= 10 ;
33
+ box . classList . add ( `l${ lightness } ` ) ;
34
+ }
35
+ box . style . backgroundColor = `hsl(0, 0%, ${ lightness } %)` ;
36
+ }
37
+ } ) ;
38
+ }
39
+ }
40
+ const boxWidth = gridWidth / boxesPerSide ;
41
+
42
+ boxGrid . style . display = "grid" ;
43
+ boxGrid . style . gridTemplate = `repeat(${ boxesPerSide } , ${ boxWidth } px) / repeat(${ boxesPerSide } , ${ boxWidth } px)` ;
44
+ }
45
+
46
+ var clearCanvas = function ( ) {
47
+ boxGrid . textContent = "" ;
48
+ boxGrid . removeAttribute ( "style" ) ;
49
+ }
50
+
51
+ var askResolution = ( ) => {
52
+ const res = prompt ( "Enter resolution size" , "10" ) ;
53
+ if ( res === null ) return ;
54
+ if ( res > 100 ) {
55
+ alert ( "Number too big, max: 100" ) ;
56
+ askResolution ( ) ;
57
+ return ;
58
+ }
59
+ clearCanvas ( ) ;
60
+ drawCanvas ( res ) ;
61
+ }
62
+
63
+
64
+ button . addEventListener ( "click" , askResolution ) ;
65
+
66
+
67
+ drawCanvas ( 16 ) ;
0 commit comments