-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
130 lines (111 loc) · 3.53 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
let size = 5;
// COLORIR O QUADRO
function paint(click) {
const selected = document.querySelector('.selected');
const color = window.getComputedStyle(selected, null).getPropertyValue('background-color');
const px = click.target;
px.style.backgroundColor = color;
}
// CRIAR O QUADRO
const board = document.getElementById('pixel-board');
function createPixelBoard() {
for (let index = 0; index < size; index += 1) {
const row = document.createElement('tr');
for (let index2 = 0; index2 < size; index2 += 1) {
const pixel = document.createElement('td');
pixel.classList.add('pixel');
pixel.addEventListener('click', paint);
row.appendChild(pixel);
}
board.appendChild(row);
/* document.getElementById('board-size').value = linesNumber; */
}
}
const input = document.getElementById('board-size');
function createBoardButton() {
size = input.value;
if (size < 5) {
size = 5;
alert('Opa!\nO menor número possível é 5!');
createPixelBoard();
/* document.getElementById('board-size').value = 5; */
} else if (size > 50) {
size = 50;
alert('Opa!\nO maior número possível é 50!');
createPixelBoard();
/* document.getElementById('board-size').value = 50; */
} else {
createPixelBoard();
}
}
function validBoard() {
if (input.value <= 0) {
alert('Board inválido!');
} else {
while (board.children.length > 0) {
board.removeChild(board.firstChild);
console.log(board.children.length);
}
createBoardButton();
}
}
const createButton = document.getElementById('generate-board');
createButton.addEventListener('click', validBoard);
// ENTER NO INPUT
function enter(key) {
if (key.keyCode === 13) {
createButton.click();
}
}
input.addEventListener('keyup', enter);
// SELECIONAR CORES
function selectColor(click) {
if (click.target.classList.contains('selected')) {
// empty
} else {
const deselect = document.querySelector('.selected');
deselect.classList.remove('selected');
click.target.classList.add('selected');
}
}
// GERAR NOVAS CORES
const palette = document.getElementById('color-palette');
function createPalette() {
const numberOfColors = 4;
for (let index = 0; index < numberOfColors; index += 1) {
const color = document.createElement('div');
color.classList.add('color');
color.addEventListener('click', selectColor);
palette.appendChild(color);
}
const black = palette.firstElementChild;
black.style.backgroundColor = 'black';
black.classList.add('selected');
for (let index = 1; index < numberOfColors; index += 1) {
const hue = Math.round(1000 * (Math.random() * 0.33));
const saturation = '100%';
const lightness = Math.round(100 * (Math.random() * (0.9 - 0.2) + 0.2));
const newColor = `hsl(${hue}, ${saturation}, ${lightness}%`;
palette.children[index].style.backgroundColor = newColor;
}
}
function paletteButton() {
while (palette.children.length > 0) {
palette.removeChild(palette.lastElementChild);
}
createPalette();
}
const randomButton = document.getElementById('random');
randomButton.addEventListener('click', paletteButton);
// ONLOAD
window.addEventListener('load', createPalette);
window.addEventListener('load', createPixelBoard);
// LIMPAR O QUADRO
function limpar() {
const px = document.getElementsByClassName('pixel');
for (let index = 0; index < px.length; index += 1) {
px[index].style.backgroundColor = 'white';
}
}
const clearButton = document.getElementById('clear-board');
clearButton.addEventListener('click', limpar);