-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
98 lines (86 loc) · 3.15 KB
/
main.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
//Initializing all of the DOM elemenets that will be used
const slider = document.getElementById('slider');
const grid = document.getElementById('grid');
const clear = document.getElementById('clearbtn');
const colorPicker = document.getElementById('colorpicker');
const sliderText = document.getElementById('slidersize');
const pen = document.getElementById('penbtn');
const eraser = document.getElementById('eraserbtn');
const rainbow = document.getElementById('rainbowbtn');
//Pen is on when webpage is loaded
let penOn = true;
let eraserOn = false;
let rainbowOn = false;
//Sets pen to be active when button is clicked
const penIsClicked = pen.addEventListener('mousedown', function() {
penOn = true;
eraserOn = false;
rainbowOn = false;
pen.classList.add('active');
eraser.classList.remove('active');
rainbow.classList.remove('active');
})
//Sets eraser to be active when eraser is clicked
const eraserIsClicked = eraser.addEventListener('mousedown', function() {
penOn = false;
eraserOn = true;
rainbowOn = false;
pen.classList.remove('active');
eraser.classList.add('active');
rainbow.classList.remove('active');
})
//Sets rainbow to be active when rainbow is clicked
const rainbowIsClicked = rainbow.addEventListener('mousedown', function() {
penOn = false;
eraserOn = false;
rainbowOn = true;
pen.classList.remove('active');
eraser.classList.remove('active');
rainbow.classList.add('active');
})
//Randomizes and returns random color combination
const randomColorGenerator = () => {
let r = Math.floor(Math.random()*256);
let g = Math.floor(Math.random()*256);
let b = Math.floor(Math.random()*256);
return ('rgb(' + r + ',' + g +',' + b);
}
//changes the slider text when slider is moved
const changeSlideText = (value) => {
sliderText.innerText = value + ' x ' + value;
}
//clears the grid
const clearGrid = () => {
grid.innerHTML = '';
createGrid();
}
//clears and creates a new grid to the new selected size when hte slider is moved
let e = slider.addEventListener('mouseup', function() {
clearGrid();
createGrid();
changeSlideText(slider.value);
});
//listens for the clear button to be pressed and then clears the grid
const clearBtn = clear.addEventListener('click',clearGrid);
//creates grid and listens for mouse movement in the grid and changes the color according to which button is active
const createGrid = () => {
document.documentElement.style.setProperty('--columns-row', slider.value);
for(let i = 0; i<slider.value * slider.value; i++){
let div = document.createElement('div');
div.addEventListener('mouseenter', function() {
if(penOn){
this.style.backgroundColor = colorPicker.value;
}
else if(eraserOn){
this.style.backgroundColor = 'rgb(218, 210, 210)';
}
else if(rainbowOn){
let color= randomColorGenerator();
this.style.backgroundColor = color;
}
})
grid.appendChild(div);
}
}
//creates grid on website load
let start = window.addEventListener('start',createGrid());