-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
87 lines (72 loc) · 2.39 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
function generatePalette(colors) {
const colorPalette = document.getElementById('color-palette');
for (let color of colors) {
let element = document.createElement('div');
element.className = 'color';
element.style.backgroundColor = color;
colorPalette.appendChild(element);
element.addEventListener('click', colorSelector);
}
colorPalette.firstChild.classList.add('selected');
}
function generateBoard(size) {
const pixelBoard = document.getElementById('pixel-board');
for (let line = 0; line < size; line++) {
pixelBoard.style.gridTemplateColumns += ' auto';
for (let pixel = 0; pixel < size; pixel++) {
let element = document.createElement('div');
element.className = 'pixel';
pixelBoard.appendChild(element);
element.addEventListener('click', pixelColorizer);
}
}
}
function colorSelector(event) {
let selectedColor = document.getElementsByClassName('selected')[0];
selectedColor.style.height = null;
selectedColor.style.width = null;
selectedColor.classList.remove('selected');
event.target.classList.add('selected');
event.target.style.height = '33px';
event.target.style.width = '33px';
}
function pixelColorizer(event) {
let selectedColor = document.getElementsByClassName('selected')[0];
event.target.style.backgroundColor = selectedColor.style.backgroundColor;
}
function boardReset() {
const pixels = document.getElementsByClassName('pixel');
for (let pixel of pixels) {
pixel.style.backgroundColor = null;
}
}
function customBoard(event) {
const pixelBoard = document.getElementById('pixel-board');
let size = document.getElementById('board-size').value;
if (size != '') {
if (size < 5) {
size = 5;
} else if (size > 50) {
size = 50;
}
pixelBoard.innerHTML = null;
pixelBoard.style.gridTemplateColumns = null;
generateBoard(size);
} else {
alert('Board inválido!');
}
}
function generateColor() {
let rgb = [];
for (i = 0; i < 3; i++) {
let color = Math.floor(Math.random() * 256);
rgb.push(color);
}
return 'rgb(' + rgb[0] + ', ' + rgb[1] + ', ' + rgb[2] + ')';
}
window.onload = function () {
generatePalette(['black', generateColor(), generateColor(), generateColor()]);
generateBoard(5);
document.querySelector('#clear-board').addEventListener('click', boardReset);
document.querySelector('form').addEventListener('submit', customBoard);
};