-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
140 lines (109 loc) · 3.5 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
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
const board = document.querySelector("#board");
let gridValue = parseInt(document.querySelector("#grid-value").textContent);
let colorBox = document.querySelector("#color");
let color = document.querySelector("#color").value;
let paintStyle = document.createElement('style');
let boardGridStyle = document.createElement('style');
const colorBtn = document.querySelector('#btn-color');
const rainbowBtn = document.querySelector('#btn-rainbow');
const clearBtn = document.querySelector('#btn-clear');
const eraserBtn = document.querySelector('#btn-eraser');
const slider = document.querySelector('.slider');
const gridValueSpan = document.querySelectorAll('#grid-value');
let pixels=[];
const GRID_SIZE = 600;
function CreatePixels(gridValue){
pixels = [];
const gridSize = GRID_SIZE / gridValue;
for(let i = 0; i < gridValue * gridValue; i++) {
const pixel = document.createElement('div');
pixel.id = 'pixel';
AddPaintStyle(color);
pixels.push(pixel);
}
}
function AddPaintStyle(color){
pixels.forEach(pixel => {
pixel.addEventListener('mouseover', ()=>{
pixel.style.backgroundColor = color;
});
});
}
function UpdateGrid(){
board.innerHTML ="";
UpdatePaintStyle();
UpdateBoardStyle();
pixels.forEach(pixel => {
board.appendChild(pixel);
});
}
CreatePixels(gridValue);
UpdateGrid(gridValue);
function UpdateColor(){
color = colorBox.value;
AddPaintStyle(color);
}
function UpdatePaintStyle() {
const gridSize = GRID_SIZE / gridValue;
paintStyle.innerHTML = ` #pixel {border: 1px solid var(--color-primary-light);
width = ${gridSize}px;
height = ${gridSize}px;
}`;
document.head.appendChild(paintStyle);
}
function UpdateBoardStyle(){
const gridSize = GRID_SIZE / gridValue;
boardGridStyle.innerHTML = `#board {
grid-template-columns: repeat(${gridValue}, ${gridSize}px);
grid-template-rows: repeat(${gridValue}, ${gridSize}px);
}
`;
document.head.appendChild(boardGridStyle);
}
// General Event Listeners
clearBtn.addEventListener('click', ClearBoard);
eraserBtn.addEventListener('click', Eraser);
colorBox.addEventListener('change', UpdateColor, false);
rainbowBtn.addEventListener('click', RainbowColorMode);
colorBtn.addEventListener('click', StandardColorMode);
slider.addEventListener('change', ChangeGrid);
function ChangeGrid(){
gridValue = parseInt(slider.value);
CreatePixels(parseInt(slider.value));
UpdateGrid(parseInt(slider.value));
console.log(pixels);
gridValueSpan.forEach((x)=>{
x.textContent = slider.value;
});
}
function ClearBoard(){
pixels.forEach(pixel =>{
pixel.style.backgroundColor = "#fff";
});
}
function Eraser(){
eraserBtn.classList.toggle('active');
if(eraserBtn.classList.contains('active')){
AddPaintStyle('#fff');
}
else {
AddPaintStyle(color);
}
}
function RainbowColorMode(){
colorBtn.classList.remove('active');
rainbowBtn.classList.add('active');
pixels.forEach(pixel => {
const randomR = Math.floor(Math.random()*255);
const randomG = Math.floor(Math.random()*255);
const randomB = Math.floor(Math.random()*255);
pixel.addEventListener('mouseover', ()=>{
pixel.style.backgroundColor = `rgb(${randomR}, ${randomG}, ${randomB})`;
});
});
}
function StandardColorMode(){
rainbowBtn.classList.remove('active');
colorBtn.classList.add('active');
AddPaintStyle(color);
}