-
Notifications
You must be signed in to change notification settings - Fork 2
/
pixel.js
169 lines (136 loc) · 3.71 KB
/
pixel.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
/*
* Table Manipulation Script
*/
/* FUNCTIONS */
/* Drawing Functions */
/**
* access each cell in array of cells
* listen for mouse events, modify cell color property for
* mousedown and mousemove
* */
let beginDraw = () => {
colorCollector();
for(let cell of cells){
cell.addEventListener('mousedown',event => {
isDrawing = true;
console.log(cell);
console.log(cells.length);
modColor(cell,myColor);
});
cell.addEventListener('mousemove',event => {
if(isDrawing){
modColor(cell,myColor);
}
});
cell.addEventListener('mouseup',event => {
if(isDrawing){
isDrawing = false;
}
});
}
}
/* Color Mod Functions */
/* set myColor to the current selected color from the dropdown menu */
let colorCollector = () =>
{
myColor = document.getElementById("color").value;
}
/* change color of cell based on input */
let modColor = (loc,clr) => {
loc.style.backgroundColor = clr;
}
/* helper to grab current color value */
let allColorHelper = () => {
//get current color
allColor(myColor);
}
/* change color of all cells based on input */
let allColor = (clr) => {
for(let cell of cells){
modColor(cell,clr);
}
}
/* helper to grab current color value */
let remColorHelper = () => {
//get current color
remColor(myColor);
}
/* change color of all cells based on input */
let remColor = (clr) => {
for(let cell of cells){
if(cell.style.backgroundColor == "gainsboro"){
modColor(cell,clr);
}
}
}
let initialFill = () => {
for(let cell of cells){
if(cell.style.backgroundColor == ""){
modColor(cell,"gainsboro");
}
}
}
/* Table Expansion and Contraction Functions */
/* add 1 column at the end of the table */
let addColumn = () => {
let table = document.getElementById("myTable");
let rows = document.getElementsByTagName('tr');
let newRow = document.createElement('tr');
if(rows[0] != undefined){
for(let row of rows){
row.insertCell(0);
initialFill();
}
}else {/*Edge case, add first Node */
newRow.appendChild(document.createElement('td'));
table.appendChild(newRow);
initialFill();
}
}
/* remove 1 column at the end of the table */
let deleteColumn = () => {
let rows = Array.from(document.getElementsByTagName('tr'));
if(rows[0].childElementCount > 0){
for(let row of rows){
row.deleteCell(0);
}
}
}
/* add 1 row at the end of the table */
let addRow = () => {
let table = document.getElementById("myTable");
let rows = Array.from(document.getElementsByTagName('tr'));
let newRow = document.createElement('tr');
// goes up until the length of the rows
if(rows[0] != undefined){
for(let i = 0; i < rows[0].childElementCount; i++){
newRow.appendChild(document.createElement('td'));
table.appendChild(newRow);
initialFill();
}
}else{ // Edge case, add first Node
newRow.appendChild(document.createElement('td'));
table.appendChild(newRow);
initialFill();
}
}
/* delete 1 row at the end of the table */
let deleteRow = () => {
let rows = Array.from(document.getElementsByTagName('tr'));
let table = document.getElementById("myTable");
if(rows[0] != undefined){
table.deleteRow(0);
}
}
/* MAIN */
let initCells = false;
let myColor; /* need to get current color from dropdown */
let isDrawing = false;
let i = 0;
/* gather array of all cells */
const cells = document.getElementsByTagName("TD");
console.log(cells);
/* listen for mouse event */
document.onmousemove = function(){
beginDraw();
};