-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
123 lines (99 loc) · 3.36 KB
/
app.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
const container = document.querySelector('#container');
const slider = document.querySelector('.ranger input ');
const clear = document.querySelector('.clear');
const sliderValue = document.querySelector('output');
const option = document.querySelectorAll('.option');
const colorSel = document.querySelector('#color');
const backgroundColor = '#FBFBFB';
let square = document.querySelectorAll('.square');
let isDrawing = false;
addInitialSetup();
createGrid();
/* Setup range slider and clear button*/
function addInitialSetup() {
slider.addEventListener('input', function () {
sliderValue.innerHTML = slider.value;
}, false);
slider.addEventListener('change', () => createGrid());
clear.addEventListener('click', () => {
createGrid();
isDrawing = false;
});
}
/* Clear grid */
function clearGrid() {
const grid = document.querySelectorAll('.square');
grid.forEach(item => item.remove());
}
/* Create grid */
function createGrid() {
clearGrid();
const gridSize = slider.value;
container.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
container.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
for (let i = 0; i < gridSize ** 2; i++) {
const squareDiv = document.createElement('div');
squareDiv.classList.add('square');
squareDiv.style.backgroundColor = backgroundColor;
container.appendChild(squareDiv);
}
container.addEventListener('click', addListeners);
}
/* Add event listeners */
function addListeners() {
square = document.querySelectorAll('.square');
if (!isDrawing) {
square.forEach(item => item.addEventListener('mouseover', paintColor));
isDrawing = true;
} else {
square.forEach(item => item.removeEventListener('mouseover', paintColor));
isDrawing = false;
}
}
/* Paint on grid */
function paintColor(e) {
let colorMode = setColorMode();
if (colorMode === 'darken') e.target.style.backgroundColor = darken(e);
if (colorMode === 'rainbow') e.target.style.backgroundColor = rainbow();
if (colorMode === 'color') {
square.forEach(item => item.classList.remove('first-time'));
e.target.style.backgroundColor = colorSel.value;
}
}
/* Return active Color Mode */
function setColorMode() {
let colorMode;
option.forEach(item => {
if (item.checked) colorMode = item.classList[0];
});
return colorMode;
}
/* convert RGB string to array */
function getRGB(rgb){
rgb = rgb.replace(/[^\d,]/g, '').split(',');
return rgb;
}
/* convert hex to RGB */
function hexToRgb(hex) {
return `rgb(${parseInt(hex.substr(1,2), 16)}, ${parseInt(hex.substr(3,2), 16)}, ${parseInt(hex.substr(5,2), 16)})`;
}
/* Create rainbow colors */
function rainbow() {
const rainbow = [];
for (let i = 0; i < 3; i++) {
rainbow[i] = Math.floor(Math.random() * 254);
}
return `rgb(${rainbow})`;
}
/* Create darken colors */
function darken(e) {
let exists = e.target.classList.contains('first-time');
if (exists) {
let currentColor = getRGB(e.target.style.backgroundColor);
const currentColorDarken = currentColor.map(rgb => rgb - rgb / 10);
return `rgb(${currentColorDarken})`;
} else {
e.target.classList.add('first-time');
if (e.target.style.backgroundColor === hexToRgb(backgroundColor)) return '#bfbfbf';
}
}