-
Notifications
You must be signed in to change notification settings - Fork 5
/
designs.js
375 lines (330 loc) · 10.3 KB
/
designs.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Select size input
const createGridBtn = $('#create-grid');
// Select Reset button
const resetBtn = $('#reset');
// Select Clear Grid button
const clearBtn = $('#clear-grid')
// Select height and width input
const inputRows = $('#input-rows');
const inputColumns = $('#input-columns');
// Select color input
const colorInput = $('#color-picker');
// Select table
const pixelCanvas = $('#pixel-canvas');
// Rows and columns
const row = '<tr></tr>';
const column = '<td></td>';
// RESET page
function reset(){
removeGrid();
colorInput.val('#4286f4');
selectedColor = colorInput.val();
inputRows.val(10);
inputColumns.val(10);
currentGridRows = 0;
inputRows.css('color', '');
inputColumns.css('color', '');
}
resetBtn.click(reset);
// Disable context menu on grid
pixelCanvas.contextmenu(function() {
return false;
});
// GRID
////////////////////////////////////////////////
// Clear grid
function removeGrid(){
pixelCanvas.children().remove();
}
function clearGrid(){
countRows();
if (currentGridRows > 0){
removeGrid();
makeGrid();
}
}
clearBtn.click(clearGrid);
// GRID BUILDER
////////////////////
// Add/remove row/column buttons
const addRowBtn = $('#add-row');
const removeRowBtn = $('#remove-row');
const addColumnBtn = $('#add-column');
const removeColumnBtn = $('#remove-column');
// Event listeners for grid-building buttons
let clickAndHold;
addRowBtn.on('touchstart mousedown', function(){
// detect touch screen event
if (event.type === 'touchstart'){
gridBuilder(increment, inputRows, addRowBtnValue);
} else {
// Allow single click or click and hold to add multiple
clickAndHold = setInterval(function(){
gridBuilder(increment, inputRows, addRowBtnValue);
}, 80);
}
}).on('mouseup mouseleave', function(){
clearInterval(clickAndHold);
});
removeRowBtn.on('touchstart mousedown', function(){
if (event.type === 'touchstart'){
buildGrid(increment, inputRows);
gridBuilder(decrement, inputRows, removeRowBtnValue);
} else {
// builds missing rows before removing
buildGrid(increment, inputRows);
clickAndHold = setInterval(function(){
gridBuilder(decrement, inputRows, removeRowBtnValue);
}, 80);
}
}).on('mouseup mouseleave', function(){
clearInterval(clickAndHold);
});
addColumnBtn.on('touchstart mousedown', function(){
if (event.type === 'touchstart'){
buildGrid(increment, inputRows);
gridBuilder(increment, inputColumns, addColumnBtnValue);
} else {
// builds grid if it's not there yet
buildGrid(increment, inputRows);
clickAndHold = setInterval(function(){
gridBuilder(increment, inputColumns, addColumnBtnValue);
}, 80);
}
}).on('mouseup mouseleave', function(){
clearInterval(clickAndHold);
});
removeColumnBtn.on('touchstart mousedown', function(){
if (event.type === 'touchstart'){
buildGrid(increment, inputRows);
gridBuilder(decrement, inputColumns, removeColumnBtnValue);
} else {
// builds grid before removing column
buildGrid(increment, inputRows);
clickAndHold = setInterval(function(){
gridBuilder(decrement, inputColumns, removeColumnBtnValue);
}, 80);
}
}).on('mouseup mouseleave', function(){
clearInterval(clickAndHold);
});
// Add/remove rows/columns buttons value
let addRowBtnValue = addRowBtn.val();
let removeRowBtnValue = removeRowBtn.val();
let addColumnBtnValue = addColumnBtn.val();
let removeColumnBtnValue = removeColumnBtn.val();
// listen to change in input: invalid input alert, and update btn value,
inputRows.on('keyup mouseup', function(){
invalidRowInputAlert();
addRowBtnValue = inputRows.val();
removeRowBtnValue = inputRows.val();
});
inputColumns.on('keyup mouseup', function(){
invalidColumnInputAlert();
addColumnBtnValue = inputColumns.val();
removeColumnBtnValue = inputColumns.val();
});
// Listen to focus out or enter key on input and build grid
inputRows
.focusout(function(){
validateRowInput();
buildGrid(increment, inputRows);
})
.keypress(function(event){
const key = (event.keyCode ? event.keyCode : event.which);
if (key == '13'){
validateRowInput();
buildGrid(increment, inputRows);
}
});
inputColumns
.focusout(function(){
validateColumnInput();
countColumns();
if (currentGridColumns < 1){
buildGrid(increment, inputRows);
} else {
buildGrid(increment, inputColumns);
}
})
.keypress(function(event){
const key = (event.keyCode ? event.keyCode : event.which);
validateColumnInput();
countColumns();
if (key == '13'){
if (currentGridColumns < 1){
buildGrid(increment, inputRows);
} else {
buildGrid(increment, inputColumns);
}
}
});
// Create grid button
createGridBtn.click(makeGrid);
function makeGrid(){
countRows();
countColumns();
buildGrid(increment, inputRows);
}
// Build grid, update input, & update btn value.
// Param: scale = increment or decrement. axis = row or column. btn = add/remove rows/columns buttons
function gridBuilder (scale, axis, btn){
axis.val(scale);
buildGrid(scale, axis);
btn = axis.val();
}
// increment/decrement row and column input
function increment (i, val){
return +val +1;
}
function decrement (i, val){
return +val -1;
}
// Build grid, based on event, and difference between current grid and input value
function buildGrid(scale, axis){
validateRowInput();
validateColumnInput();
countRows();
countColumns();
// Find out which button triggered the function
if (scale === increment && axis === inputRows){
// Compare input to current grid to add or remove accordingly
currentGridRows < inputRows.val() ? constructRows() : eliminateRows();
} else if (scale === decrement && axis === inputRows){
currentGridRows > inputRows.val() ? eliminateRows() : constructRows();
} else if (scale === increment && axis === inputColumns){
currentGridColumns < inputColumns.val() ? constructColumns() : eliminateColumns();
} else {
currentGridColumns > inputColumns.val() ? eliminateColumns() : constructColumns();
}
}
// Limit grid size 1-150. Prevent input of other values, change number color warning for min/max and invalid values,
function validateRowInput(){
if (inputRows.val() < 1 ){
inputRows.val(1);
} else if (inputRows.val() > 150){
inputRows.val(150);
}
invalidRowInputAlert();
}
function invalidRowInputAlert(){
if (inputRows.val() < 1 || inputRows.val() > 150){
inputRows.css('color', 'red');
} else if (inputRows.val() == 1 || inputRows.val() == 150){
inputRows.css('color', '#DBA70D');
} else {
inputRows.css('color', '');
}
}
function validateColumnInput(){
if (inputColumns.val() < 1){
inputColumns.val(1);
} else if (inputColumns.val() > 150){
inputColumns.val(150);
}
invalidColumnInputAlert();
}
function invalidColumnInputAlert(){
if (inputColumns.val() < 1 || inputColumns.val() > 150){
inputColumns.css('color', 'red');
} else if (inputColumns.val() == 1 || inputColumns.val() == 150){
inputColumns.css('color', '#DBA70D');
} else {
inputColumns.css('color', '');
}
}
// Store difference between current rows/columns on screen and the input value
let rowsDiff = 0;
let columnsDiff = 0;
let currentGridRows = 0;
let currentGridColumns = 0;
function countRows(){
let gridRows = inputRows.val();
currentGridRows = pixelCanvas.children('tr').length;
rowsDiff = Math.abs(gridRows - currentGridRows);
}
function countColumns(){
let gridColumns = inputColumns.val();
currentGridColumns = pixelCanvas.children().first().children().length;
columnsDiff = Math.abs(gridColumns - currentGridColumns);
}
// Construct/eliminate rows/columns
function constructRows(){
for (let r = 1; r <= rowsDiff; r++){
const addRow = $("<tr></tr>");
pixelCanvas.append(addRow);
for(let c = 1; c <= inputColumns.val(); c++){
const addColumn = $("<td></td>");
addRow.append(addColumn);
}
}
}
function eliminateRows(){
for (let r = 1; r <= rowsDiff; r++){
pixelCanvas.children().last().remove();
}
}
function constructColumns(){
for (let c = 1; c <= columnsDiff; c++){
pixelCanvas.children().append(column);
}
}
function eliminateColumns(){
for (let c = 1; c <= columnsDiff; c++){
$('tr').each(function(){
$(this).find("td:last").remove();
});
}
}
//////////////////////////////////////////////// ^ GRID ^
// DRAW
////////////////////////////////////////////////
let defaultColor = colorInput.val('#4286f4');
// Convert hex to rgb
function hexToRgb(hex){
hex = hex.replace('#','');
r = parseInt(hex.substring(0, hex.length/3), 16);
g = parseInt(hex.substring(hex.length/3, 2*hex.length/3), 16);
b = parseInt(hex.substring(2*hex.length/3, 3*hex.length/3), 16);
result = `rgb(${r}, ${g}, ${b})`;
return result;
};
// Color on single click. If same color erase
function draw(){
let selectedColor = colorInput.val();
$(this).css("background-color") == hexToRgb(selectedColor) ?
$(this).css('background-color', '') : $(this).css('background-color', selectedColor);
};
// click and drag draw/erase function
function drag(){
let mouseIsDown = true;
let clicks = $(this).data('clicks');
pixelCanvas
.on('mouseleave', 'td', function(){
if (mouseIsDown){
if (!clicks){
// Change background color of cell
$(this).css('background-color', colorInput.val());
} else {
// On second click return color to default (erase)
$(this).css('background-color', '');
}
// Fire `if` event on odd clicks
$(this).data('clicks', !clicks);
}
})
.on('mousedown', 'td', function(){
event.preventDefault();
mouseIsDown = true;
})
.on('mouseup', 'td', function(){
mouseIsDown = false;
});
pixelCanvas.on('mouseleave', function(){
mouseIsDown = false;
});
}
// Event listener click delegated
pixelCanvas
.on('mousedown', 'td', draw)
.on('mousedown', 'td', drag);