-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
288 lines (240 loc) · 8.75 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// GLOBAL VARIABLES
let gridLinesPresent = false;
let gridWidth = 500;
let defaultGridLinesClr = "#92d6e1";
let defaultPenClr = "#6adcf0";
// FUNCTIONS
function removeGridEventListeners(grid) {
// Remove all grid event listeners that might be active
grid.removeEventListener('mousedown', grid.fnMouseDown);
grid.removeEventListener('mouseup', grid.fnMouseUp);
grid.removeEventListener('mouseover', grid.fnMouseOverDrag);
grid.removeEventListener('mouseover', grid.fnMouseOverHover);
grid.removeEventListener('click', grid.fnMouseDownRelease);
grid.removeEventListener('mouseover', grid.fnMouseOverRelease);
}
function setDrawModeToClickDragRelease() {
let grid = document.querySelector('div.grid');
removeGridEventListeners(grid);
// Does not matter where these are declared, only where and when they are updated
let sustainColoring = false;
// Add unique event listeners
grid.addEventListener('mousedown', grid.fnMouseDownRelease = function (e) {
e.preventDefault();
if (sustainColoring === false) {
sustainColoring = true;
} else {
sustainColoring = false;
}
colorAccordingToPenMode(e);
});
grid.addEventListener('mouseover', grid.fnMouseOverRelease = function (e) {
e.preventDefault();
if (sustainColoring === true) {
colorAccordingToPenMode(e);
}
});
}
function setDrawModeToHover() {
let grid = document.querySelector('div.grid');
removeGridEventListeners(grid);
// Add unique event listeners
grid.addEventListener('mouseover', grid.fnMouseOverHover = function (e) {
e.preventDefault();
colorAccordingToPenMode(e);
});
}
function setDrawModeToClickDragHold() {
// Does not matter where these are declared, only where and when they are updated
let isMouseDownTriggered = false;
let isFirstInBoxSeries = true;
let grid = document.querySelector('div.grid');
let body = document.querySelector('body');
removeGridEventListeners(grid);
// Add unique event listeners
grid.addEventListener('mousedown', grid.fnMouseDown = function (e) {
e.preventDefault();
if (isFirstInBoxSeries) {
colorAccordingToPenMode(e);
isFirstInBoxSeries = false;
}
isMouseDownTriggered = true;
});
body.addEventListener('mouseup', grid.fnMouseUp = function (e) {
isMouseDownTriggered = false;
isFirstInBoxSeries = true;
});
grid.addEventListener('mouseover', grid.fnMouseOverDrag = function (e) {
e.preventDefault();
if (isMouseDownTriggered === true) {
colorAccordingToPenMode(e);
}
});
}
function getRandomizedColor() {
// Math.pow is slow, use constant instead.
let color = Math.floor(Math.random() * 16777216).toString(16);
// Avoid loops.
return '#000000'.slice(0, -color.length) + color;
}
function getGridBackgroundColor() {
let grid = document.getElementById('grid');
return grid.style.backgroundColor;
}
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function colorAccordingToPenMode(e) {
if (document.getElementById("colored-pen").checked) {
let color = document.getElementById("penColorWell").value;
e.target.style.backgroundColor = color;
e.target.classList.add('colored');
}
else if (document.getElementById("randomized-pen").checked) {
let color = getRandomizedColor();
e.target.style.backgroundColor = color;
e.target.classList.add('colored');
}
else if (document.getElementById("eraser-pen").checked) {
if (e.target.classList.contains('colored')) {
e.target.style.backgroundColor = "";
e.target.classList.remove('colored');
}
}
}
function setDrawMode(e) {
if (e.target.value === "click-drag-hold") {
setDrawModeToClickDragHold();
} else if (e.target.value === "click-drag-release") {
setDrawModeToClickDragRelease();
}
else if (e.target.value === "hover-over") {
setDrawModeToHover();
}
}
function getGridDimensionsFromInput() {
let gridSizeInput = document.querySelector('input#grid-size');
if (gridSizeInput.value > 100) {
gridSizeInput.value = 100;
}
else if (gridSizeInput.value > 100){
gridSizeInput.value = 0;
}
return gridSizeInput.value;
}
function getDividedDivSize(numberOfDivs, gridDimensions) {
return gridDimensions / numberOfDivs;
}
function addDiv(grid, calculatedSize) {
let div = document.createElement('div');
div.style.height = calculatedSize + "px";
div.style.width = calculatedSize + "px";
let gridLinesColorWell = document.getElementById('grid-lines-color-well');
let gridLinesToggle = document.getElementById('grid-lines-toggle');
if (gridLinesToggle.checked === true) {
div.style.border = "1px solid " + gridLinesColorWell.value;
}
else {
gridLinesToggle.checked = false;
gridLinesPresent = false;
}
grid.appendChild(div);
return grid;
}
function clearGrid(grid) {
// if grid is empty, return, else remove all children until it is
let childrenNodesList = document.querySelectorAll('div.grid div')
if (childrenNodesList.length === 0) {
return;
}
while (grid.firstChild) {
grid.removeChild(grid.lastChild);
}
}
function populateGridCells(grid) {
clearGrid(grid);
let numberOfDivs = getGridDimensionsFromInput();
const gridDimensions = gridWidth;
for (let i = 0; i < numberOfDivs; i++) {
for (let j = 0; j < numberOfDivs; j++) {
let calculatedSize = getDividedDivSize(numberOfDivs, gridDimensions);
grid = addDiv(grid, calculatedSize);
}
}
}
function removeGridLines() {
let gridDivs = document.querySelectorAll('.grid div');
gridDivs.forEach(div => div.style.border = '0px');
gridLinesPresent = false;
}
function addGridLines() {
let gridLinesColorWell = document.getElementById('grid-lines-color-well');
let gridDivs = document.querySelectorAll('.grid div');
gridDivs.forEach(div => div.style.border = '1px solid ' + gridLinesColorWell.value);
gridLinesPresent = true;
}
function reset() {
eraseGrid();
removeGridLines();
let gridLinesToggle = document.getElementById('grid-lines-toggle');
gridLinesToggle.checked = false;
let drawModeInput = document.getElementById('click-drag-hold-mode');
drawModeInput.checked = true;
setDrawModeToClickDragHold();
let drawToolInput = document.getElementById('colored-pen');
drawToolInput.checked = true;
let penColorWell = document.getElementById('penColorWell');
penColorWell.value = defaultPenClr;
let gridLinesColorWell = document.getElementById('grid-lines-color-well');
gridLinesColorWell.value = defaultGridLinesClr;
let gridSizeInput = document.querySelector('input#grid-size');
gridSizeInput.value = 25;
populateGridCells(grid);
}
function eraseGrid() {
let gridDivs = document.querySelectorAll('.grid div');
gridDivs.forEach(div => {
div.style.backgroundColor = "";
div.classList.remove('colored');
});
}
// DECLARATIONS
let grid = document.querySelector('.grid');
let gridSizeButton = document.querySelector('button.grid-size');
let drawModeOptionsList = document.querySelectorAll('input[name="draw-mode"]');
let gridLinesColorWell = document.getElementById('grid-lines-color-well');
let gridLinesToggle = document.getElementById('grid-lines-toggle');
let eraseGridButton = document.getElementById('erase-grid');
let resetButton = document.getElementById('reset');
let radioButtons = document.querySelectorAll('input[type="radio"], input[type="checkbox"]');
// LOGIC
// Set default behavior before drawing can take place
populateGridCells(grid);
setDrawModeToClickDragHold();
// EVENT LISTENERS
gridLinesToggle.addEventListener('click', function () {
if (gridLinesPresent) {
removeGridLines();
}
else {
addGridLines();
}
});
eraseGridButton.addEventListener('click', eraseGrid);
resetButton.addEventListener('click', reset);
gridSizeButton.addEventListener('click', () => populateGridCells(grid));
drawModeOptionsList.forEach(drawModeOption => drawModeOption.addEventListener('click', setDrawMode));
gridLinesColorWell.addEventListener('input', function (e) {
if (gridLinesToggle.checked === true) {
let gridDivs = document.querySelectorAll('.grid div');
gridDivs.forEach(div => div.style.border = '1px solid ' + gridLinesColorWell.value);
}
});
radioButtons.forEach(radioButton => {
radioButton.addEventListener('onchange', (radioButton, e) => {
if (radioButton.checked) {
console.log(e);
} else {
}
});
});