forked from udacity/project-pixel-art-maker-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
designs.js
68 lines (67 loc) · 1.92 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
var pixelArt = {
// getHeight method is for getting the height input
getHeight: function() {
var field = document.getElementById('inputHeight');
var height;
if (field) {
height = field.value;
};
return parseInt(height, 10);
},
// getWidth method is for getting the width input
getWidth: function() {
var field = document.getElementById('inputWidth');
var width;
if (field) {
width = field.value;
};
return parseInt(width, 10);
},
// getColor method is for getting the color picker input
getColor: function() {
var field = document.getElementById('colorPicker');
var color;
if (field) {
color = field.value;
};
return color;
},
// addColor method is for adding color to table cells
addColor: function(event) {
if (event.target.nodeName != 'TD') {
return
}
event.target.style.backgroundColor = pixelArt.getColor();
},
// getTable is to get the table element
getTable: function() {
return document.getElementById('pixelCanvas');
},
// makeGrid method is for making the table (and clearing it on a new submit)
makeGrid: function(event) {
event.preventDefault();
var table = pixelArt.getTable();
table.innerHTML = "";
for (var h = 0; h < pixelArt.getHeight(); h++) {
var row = table.insertRow(-1);
for (var w = 0; w < pixelArt.getWidth(); w++) {
var cell = row.insertCell(0);
cell.innerHTML = "";
};
};
},
// addFormListener function adds the event listener to the Submit button
addFormListener: function() {
var form = document.getElementById('sizePicker');
form.addEventListener('submit', pixelArt.makeGrid, true);
},
// addClickListener adds the event listener to the color picker
addClickListener: function() {
pixelArt.getTable().addEventListener('click', pixelArt.addColor);
}
};
//loads pixel art/listeners after DOM is loaded
document.addEventListener('DOMContentLoaded', function(event) {
pixelArt.addFormListener();
pixelArt.addClickListener();
});