-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
163 lines (135 loc) · 5.42 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
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
// PIXEL SIZE SELECTION ----------------------------------------------------------------------------------------------------------------
// 0. Grab individual Pixel buttons
const smallPixels = document.getElementById('small-pixels');
const mediumPixels = document.getElementById('medium-pixels');
const largePixels = document.getElementById('large-pixels');
// 1. Toggle active selection for Pixel buttons
// Pull the Pixel size container element
const pixelSizeContainer = document.getElementById('pixel-size-container');
// Grab all the buttons inside the PixelSizeContainer
const pixelButtons = pixelSizeContainer.getElementsByTagName('button');
// Loop through all Pixel buttons and assign active class to selected button
for (let i = 0; i < pixelButtons.length; i++) {
pixelButtons[i].addEventListener('click', function() {
let current = document.getElementsByClassName('active-pixel');
current[0].className = current[0].className.replace('active-pixel', '');
pixelButtons[i].className += 'active-pixel';
});
}
// 2. Update grid based on user's selected Pixel size
// Grab grid container element
const gridContainer = document.getElementById('grid-container');
// Generates grid rows and cells
function generateGrid(rowCount, columnCount, pixelSize) {
// Create grid rows and add class 'row'
for (let i = 0; i < rowCount; i++) {
let row = document.createElement('div');
row.className = 'row';
// Create grid columns/cells and add class 'cell'
for (let j = 0; j < columnCount; j++) {
let cell = document.createElement('div');
cell.className = 'cell';
cell.style.width = `${pixelSize}px`;
cell.style.height = `${pixelSize}px`;
// Add each cell to the row
row.appendChild(cell);
}
// Add each row to the gridContainer
gridContainer.appendChild(row);
}
}
// Clears grid rows and cells (before generating new grid)
function clearGrid() {
gridContainer.querySelectorAll('div').forEach(e => e.remove());
}
// 3. Event handlers added to Pixel size buttons
// Clears current grid and replaces with respective grid based on button selected
// Calls the modeSelector function to select correct Mode for hovering over cells
smallPixels.addEventListener('click', function() {
clearGrid();
generateGrid(30, 40, 18);
modeSelector();
});
mediumPixels.addEventListener('click', function() {
clearGrid();
generateGrid(15, 20, 38);
modeSelector();
});
largePixels.addEventListener('click', function() {
clearGrid();
generateGrid(12, 16, 48);
modeSelector();
});
// MODE SELECTION ----------------------------------------------------------------------------------------------------------------
// 0. Grab individual buttons
const classicMode = document.getElementById('classic-mode');
const colorMode = document.getElementById('color-mode');
// 2. Toggle active selection for Mode buttons
// Pull the Mode container element
const modeContainer = document.getElementById('mode-container');
// Grab all the buttons inside the ModeContainer
const modeButtons = modeContainer.getElementsByTagName('button');
// Loop through all Mode buttons and assign active class to selected button
for (let i = 0; i < modeButtons.length; i++) {
modeButtons[i].addEventListener('click', function() {
let current = document.getElementsByClassName('active-mode');
current[0].className = current[0].className.replace('active-mode', '');
modeButtons[i].className += 'active-mode';
});
}
// 3. Update mousehovers to selected Mode (when cells are hovered over, change background color)
// Grab all the individual cells
const allCells = document.getElementsByClassName('cell');
// Determine whether cells should be colored Classic or Color
function modeSelector() {
if (classicMode.classList.contains('active-mode')) {
colorClassicCells();
} else {
colorColorCells();
}
}
// Color all cells Classic
function colorClassicCells() {
for (let i = 0; i < allCells.length; i++) {
allCells[i].addEventListener('mouseover', function() {
allCells[i].style.backgroundColor = '#424242';
});
}
}
// Color all cells Color
function colorColorCells() {
for (let i = 0; i < allCells.length; i++) {
allCells[i].addEventListener('mouseover', function() {
allCells[i].style.backgroundColor = `${randomColor()}`;
});
}
}
// Random color generator (for Colored cells)
function randomColor() {
const colorOptions = ['#fc5c65', '#fed330', '#26de81', '#2bcbba', '#45aaf2', '#4b6584'];
let color = colorOptions[Math.floor(Math.random() * colorOptions.length)];
return color;
}
// 4. Event handlers added to Mode buttons
classicMode.addEventListener('click', function() {
colorClassicCells();
});
colorMode.addEventListener('click', function() {
colorColorCells();
});
// CLEAR BUTTON --------------------------------------------------
// Grab individual button
const clearButton = document.getElementById('clear-grid');
// Add event listener to clear all cell colors when clicked
clearButton.addEventListener('click', function() {
clearCells();
});
// Clear colors in all cells
function clearCells() {
for (let i = 0; i < allCells.length; i++) {
allCells[i].style.backgroundColor = '';
}
}
// Call formulas immmediately and set default grid size to Small
generateGrid(30, 40, 18);
colorClassicCells();