-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesigns.js
60 lines (36 loc) · 1.53 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
//code must be wrapped to work properly.
$(document).ready(function() {
//global variables are defined.
const pixCanvas = document.getElementById('pixelCanvas');
let gridHeight = document.getElementById('inputHeight');
let gridWidth = document.getElementById('inputWeight');
//When submit button is pressed, makeGrid is called.
document.getElementById('submitBtn').addEventListener('click', makeGrid);
//preventDefault is also called when button is clicked.
document.getElementById('submitBtn').addEventListener('click', function(e) { e.preventDefault()});
//color picker is stored in a global variable.
let colorChoice = document.getElementById('colorPicker');
//makeGrid is created...
function makeGrid() {
pixCanvas.innerHTML = '';
//new local variables hold the value of height and width input.
let heightInput = gridHeight.value;
let widthInput = gridWidth.value;
//addColor will alter the color of the argument passed to it.
let addColor = function(pix) {
pix.addEventListener('click', function() {
pix.style.backgroundColor = colorChoice.value;
});
}
//Outer loop iterates of the height input variable and creates rows based on its value.
for (let i = 0; i < heightInput; i++) {
let rows = pixCanvas.insertRow(i);
//Nested loop iterates over width input variable and creates cells based on its value.
for (let j = 0; j < widthInput; j++) {
let cellBox = rows.insertCell(j);
//addColor function is called when a cellBox is clicked.
cellBox.addEventListener('click', addColor(cellBox));
}
}
}
});