-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (97 loc) · 3.11 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
const colors = document.getElementsByClassName('color');
colors[0].style.backgroundColor = 'black';
let pixels = document.getElementsByClassName('pixel');
let selected = document.querySelector('.selected');
let cor = document.querySelector('.selected').style.backgroundColor;
let button = document.getElementById('clear-board');
let generateBoard = document.getElementById('generate-board');
function whitePixel() {
for (let i of pixels) {
i.style.backgroundColor = 'white';
}
}
function removeElemento(board) {
for (let i = pixels.length; i > pixels.length; i -= 1) {
board.removeChild(pixels[i]);
}
}
function tamPaleta() {
let input = document.getElementById('board-size').value;
let board = document.getElementById('pixel-board');
if (input === '' || input < 1) {
console.log(input);
alert('Board inválido!');
} else if (input >= 5 && input <= 50) {
for (let i = pixels.length - 1; i >= 0; i -= 1) {
board.removeChild(pixels[i]);
}
for (i = 0; i < input * input; i += 1) {
let elemento = document.createElement('div');
elemento.className = 'pixel';
elemento.style.backgroundColor = 'white';
board.appendChild(elemento);
}
board.style.gridTemplateColumns = 'repeat(' + input + ',40px)';
pixels = document.getElementsByClassName('pixel');
} else if (input < 5) {
input = 5;
for (let i = pixels.length - 1; i >= 0; i -= 1) {
board.removeChild(pixels[i]);
}
for (i = 0; i < input * input; i += 1) {
let elemento = document.createElement('div');
elemento.className = 'pixel';
elemento.style.backgroundColor = 'white';
board.appendChild(elemento);
}
board.style.gridTemplateColumns = 'repeat(' + input + ',40px)';
pixels = document.getElementsByClassName('pixel');
} else if (input > 50) {
input = 50;
for (let i = pixels.length - 1; i >= 0; i -= 1) {
board.removeChild(pixels[i]);
}
for (i = 0; i < input * input; i += 1) {
let elemento = document.createElement('div');
elemento.className = 'pixel';
elemento.style.backgroundColor = 'white';
board.appendChild(elemento);
}
board.style.gridTemplateColumns = 'repeat(' + input + ',40px)';
pixels = document.getElementsByClassName('pixel');
}
clickPixels();
}
function corRandon() {
for (let i = 1; i < colors.length; i += 1) {
const color =
'rgb(' +
Math.random() * 255 +
',' +
Math.random() * 255 +
',' +
Math.random() * 255 +
')';
colors[i].style.backgroundColor = color;
}
}
function clickPixels() {
for (let i of pixels) {
i.addEventListener('click', function (event) {
event.target.style.backgroundColor = cor;
});
}
}
corRandon();
whitePixel();
clickPixels();
for (let x of colors) {
x.addEventListener('click', function (event) {
selected = document.querySelector('.selected');
selected.className = 'color';
event.target.classList = 'selected color';
cor = document.querySelector('.selected').style.backgroundColor;
});
}
button.addEventListener('click', whitePixel);
generateBoard.addEventListener('click', tamPaleta);