-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
191 lines (183 loc) · 5.77 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const colors = 4;
const colorPalette = document.getElementById('color-palette');
const pixelBoard = document.getElementById('pixel-board');
const buttonGenBoard = document.getElementById('generate-board');
const input = document.getElementById('board-size');
const clear = document.getElementById('clear-board');
const save = document.getElementById('save');
let pixels = 5;
if (localStorage.length === 0) {
localStorage.setItem('color', JSON.stringify([]));
localStorage.setItem('board', '');
}
const colorsSaved = JSON.parse(localStorage.getItem('color'));
// PALETA DE CORES
for (let i = 0; i < colors; i += 1) {
colorPalette.appendChild(document.createElement('div'));
colorPalette.children[i].className = 'color';
}
function verifyColorsSaved(index) {
if (colorsSaved[index - 1] !== undefined) {
colorPalette.children[index].style.backgroundColor = colorsSaved[index - 1];
colorPalette.children[index].classList.add('saved');
return true;
}
return false;
}
function randomColors() {
let R;
let G;
let B;
for (let i = 1; i < colorPalette.children.length; i += 1) {
if (!verifyColorsSaved(i)) {
R = parseInt(Math.random() * 256, 10);
G = parseInt(Math.random() * 256, 10);
B = parseInt(Math.random() * 256, 10);
colorPalette.children[i].style.backgroundColor = `rgb(${R}, ${G}, ${B})`;
}
}
}
function select(click) {
if (click.target !== colorPalette) {
document.querySelector('.selected').classList.remove('selected');
click.target.classList.add('selected');
}
}
function saveColor(color) {
if (color.target !== colorPalette && color.target !== colorPalette.children[0]) {
if (color.target.classList.contains('saved')) {
colorsSaved.splice(colorsSaved.indexOf(color.target.style.backgroundColor), 1);
localStorage.setItem('color', JSON.stringify(colorsSaved));
color.target.classList.remove('saved');
} else {
const savedColor = window.getComputedStyle(color.target).getPropertyValue('background-color');
colorsSaved.push(savedColor);
localStorage.setItem('color', JSON.stringify(colorsSaved));
color.target.classList.add('saved');
}
}
}
colorPalette.children[0].classList.add('selected');
randomColors();
colorPalette.onclick = select;
colorPalette.addEventListener('dblclick', saveColor);
// QUADRO DE PIXELS
function paintPixel(click) {
if (click.target.classList.contains('pixel')) {
const clicked = click;
/* if (click.target.style.backgroundColor !== '') {
clicked.target.style.backgroundColor = '';
return;
} */
const selectedHTML = document.getElementsByClassName('selected')[0];
const bgClrClicked = window.getComputedStyle(selectedHTML).getPropertyValue('background-color');
clicked.target.style.backgroundColor = bgClrClicked;
}
}
function clearBoard() {
for (let i = pixelBoard.children.length - 1; i >= 0; i -= 1) {
pixelBoard.removeChild(pixelBoard.children[i]);
}
}
function eventsBoardAllowed(event) {
if (event.type !== 'click' && event.key !== 'Enter' && event.type !== 'load') {
return false;
}
return true;
}
function rectifyInRange(value, min, max) {
if (value < min) {
return min;
}
if (value > max) {
return max;
}
return value;
}
function changePixelsSize(event) {
if (event.type === 'click' || event.key === 'Enter') {
if (input.value === '' || input.value < 1) {
return false;
}
pixels = rectifyInRange(input.value, 5, 50);
}
return true;
}
function generateBoard() {
for (let i = 0; i < pixels; i += 1) {
pixelBoard.appendChild(document.createElement('div'));
for (let j = 0; j < pixels; j += 1) {
pixelBoard.children[i].appendChild(document.createElement('div'));
pixelBoard.children[i].children[j].className = 'pixel';
}
}
}
function importBoard() {
const board = JSON.parse(localStorage.getItem('board'));
for (let i = 0; i < board.length; i += 1) {
pixelBoard.appendChild(document.createElement('div'));
for (let j = 0; j < board[i].length; j += 1) {
pixelBoard.children[i].appendChild(document.createElement('div'));
pixelBoard.children[i].children[j].className = 'pixel';
pixelBoard.children[i].children[j].style.backgroundColor = board[i][j];
}
}
localStorage.setItem('board', '');
}
function createBoard(event) {
if (!eventsBoardAllowed(event)) {
return;
}
if (!changePixelsSize(event)) {
alert('Board inválido!');
return;
}
if (localStorage.board === '') {
clearBoard();
generateBoard();
} else {
importBoard();
}
}
// CONFIGURAÇÕES
function clearPainting() {
/* for (const line of pixelBoard.children) {
for (const pixel of line.children) {
pixel.style.backgroundColor = 'white';
}
} */
for (let i = 0; i < pixelBoard.children.length; i += 1) {
for (let j = 0; j < pixelBoard.children[i].children.length; j += 1) {
pixelBoard.children[i].children[j].style.backgroundColor = 'white';
}
}
}
function storageBoard() {
const board = [];
for (let i = 0; i < pixelBoard.children.length; i += 1) {
const line = [];
for (let j = 0; j < pixelBoard.children.length; j += 1) {
line.push(pixelBoard.children[i].children[j].style.backgroundColor);
}
board.push(line);
}
localStorage.setItem('board', JSON.stringify(board));
}
function saveBoard(store) {
const storage = store;
if (store.target.classList.contains('board')) {
localStorage.setItem('board', '');
storage.target.innerText = 'Salvar';
store.target.classList.remove('board');
} else {
storageBoard();
storage.target.innerText = 'Salvo!';
store.target.classList.add('board');
}
}
window.onload = createBoard;
pixelBoard.onclick = paintPixel;
clear.onclick = clearPainting;
buttonGenBoard.onclick = createBoard;
input.onkeyup = createBoard;
save.onclick = saveBoard;