-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (132 loc) Β· 2.68 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// COLOUR NAME ARRAY TO PICK FROM
const colourNames = [
'Aqua',
'Aquamarine',
'Blue',
'BlueViolet',
'Brown',
'BurlyWood',
'CadetBlue',
'Chartreuse',
'Chocolate',
'Coral',
'CornflowerBlue',
'Crimson',
'Cyan',
'DarkBlue',
'DarkCyan',
'DarkGoldenRod',
'DarkGreen',
'DarkMagenta',
'DarkOliveGreen',
'DarkOrange',
'DarkOrchid',
'DarkRed',
'DarkSalmon',
'DarkSlateBlue',
'DarkTurquoise',
'DarkViolet',
'DeepPink',
'DeepSkyBlue',
'DodgerBlue',
'FireBrick',
'ForestGreen',
'Fuchsia',
'Gold',
'GoldenRod',
'Green',
'GreenYellow',
'HotPink',
'IndianRed',
'Indigo',
'Lavender',
'LawnGreen',
'Lime',
'LimeGreen',
'Magenta',
'Maroon',
'MediumAquaMarine',
'MediumBlue',
'MediumOrchid',
'MediumPurple',
'MediumSeaGreen',
'MediumSlateBlue',
'MediumSpringGreen',
'MediumTurquoise',
'MediumVioletRed',
'MidnightBlue',
'NavajoWhite',
'Navy',
'Olive',
'OliveDrab',
'Orange',
'OrangeRed',
'Orchid',
'PaleGreen',
'PaleTurquoise',
'PaleVioletRed',
'PeachPuff',
'Peru',
'Pink',
'Plum',
'Purple',
'RebeccaPurple',
'Red',
'RosyBrown',
'RoyalBlue',
'SaddleBrown',
'Salmon',
'SandyBrown',
'SeaGreen',
'Sienna',
'SkyBlue',
'SlateBlue',
'SlateGray',
'SlateGrey',
'SpringGreen',
'SteelBlue',
'Tan',
'Teal',
'Thistle',
'Tomato',
'Turquoise',
'Violet',
'Wheat',
'Yellow',
];
const container = document.getElementById('container')
const template = document.getElementById('template--js');
const templateContent = template.content;
const root = document.documentElement;
// GENERATE GRID BETWEEN 10 - 200
let cols = randomNumber(10, 200);
let rows = randomNumber(10, 200);
let numberOfDivs = (cols * rows)
function setCSSVars(cols, rows){
root.style.setProperty('--cols', cols)
root.style.setProperty('--rows', rows)
}
setCSSVars(cols, rows)
for (let i = 0; i < numberOfDivs; i++) {
container.appendChild(templateContent.cloneNode(true))
}
let squares = [];
// WAIT UNTIL DOCUMENT HAS LOADED BEFORE COLLECTING SQUARE ELMEENTS
setTimeout(function() {
console.log('run');
squares = [...document.getElementsByClassName('square')]
},0);
setTimeout(function(){
squares.forEach((sq) => {
sq.style.backgroundColor = randomColour();
})
},0)
// Returns a random positive whole number between two values (min, max)
// Used throughout to select random array elements
function randomNumber(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
// Return random colour name from colourNames array using randomNumber()
function randomColour() {
return colourNames[randomNumber(0, colourNames.length - 1)];
}