-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawingboard.js
48 lines (42 loc) · 1.64 KB
/
drawingboard.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
//here we select the div from our HTML file
const container = document.querySelector('.container');
let color = 'black';
//creating our grid takes in a size and creates SIZE amount of divs in the container class we called
function createGrid(size) {
const gridSize = size * size;
container.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
container.style.gridTemplateRows = `repeat(${size}, 1fr)`;
for (let i = 0; i < gridSize; i++) {
//creates divs
const div = document.createElement('div');
//adds them to css class called 'cell'
div.classList.add('cell');
//add an eventListenter that listens for a mouse enter on event it changes the background color to black
div.addEventListener('mouseenter', function( event ) {
event.target.style.backgroundColor = color;
})
container.appendChild(div);
}
};
createGrid(16);
//function to prompt the user to change grid size and go through node list and remove elements
function changeGridSize() {
const message = Number(prompt('Enter a number for your grid!'));
if (message > 100) {
return alert('Please enter number less than 100!');
} else if (message !== null) {
const nodeList = document.querySelectorAll('.cell');
nodeList.forEach(element => element.remove());
const size = message;
createGrid(size);
};
};
//function for color change runs through HTML onclick
function changeColor(newColor) {
color = newColor;
};
function getColor() {
const colorPicker = document.getElementById('color_picker');
const color = colorPicker.value;
changeColor(color);
};