-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
68 lines (56 loc) · 1.86 KB
/
Copy pathmain.js
File metadata and controls
68 lines (56 loc) · 1.86 KB
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
let color = 'black';
let click = true;
function fillBoard(size){
let canvas = document.querySelector('.canvas');
let squares = canvas.querySelectorAll('div');
//remove squares from previous run
squares.forEach((div) => div.remove());
canvas.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
canvas.style.gridTemplateRows = `repeat(${size}, 1fr)`;
let amount = size*size;
for(let i = 0; i<amount; i++){
let square = document.createElement('div');
square.addEventListener('mouseover', colorSquare);
square.style.backgroundColor = 'white';
canvas.insertAdjacentElement('beforeend', square);
}
};
function changeSize(num){
let input = document.getElementById(num).value;
if(input >=2 && input <= 100){
fillBoard(input);
} else {
console.log('error');
}
};
//next attempt to make the HSL color transition smooth
function colorSquare(){
if(click){
// let square = document.querySelector('div');
// square.addEventListener('mouseover', ()=> {
if(color === 'random'){
const hue = Math.random() * 360;
const saturation = 100;
const lightness = 50;
this.style.backgroundColor = `hsl(${hue}, ${saturation}%, ${lightness}%)`;
console.log(this.style.backgroundColor);
} else {
this.style.backgroundColor = color;
}
// })
}
}
function changeColor(choice){
color = choice;
}
function resetBoard(){
let canvas = document.querySelector('.canvas');
let squares = canvas.querySelectorAll('div');
squares.forEach((div) => div.style.backgroundColor='white');
}
document.querySelector('body').addEventListener('click', (e) => {
if(e.target.tagname !== 'BUTTON'){
click = !click;
}
});
fillBoard(16);