-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathgridOutput.js
More file actions
166 lines (158 loc) · 4.92 KB
/
gridOutput.js
File metadata and controls
166 lines (158 loc) · 4.92 KB
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
/**
* @module Environment
* @submodule Environment
* @for p5
*/
function gridOutput(p5, fn){
//the functions in this file support updating the grid output
//updates gridOutput
fn._updateGridOutput = function(idT) {
if (this._renderer && this._renderer.isP3D) {
if (!this._didOutputGridWebGLMessage) {
this._didOutputGridWebGLMessage = true;
console.error('gridOutput() does not yet work in WebGL mode.');
}
return;
}
//if html structure is not there yet
if (!this.dummyDOM.querySelector(`#${idT}_summary`)) {
return;
}
let current = this._accessibleOutputs[idT];
//create shape details list
let innerShapeDetails = _gridShapeDetails(idT, this.ingredients.shapes);
//create summary
let innerSummary = _gridSummary(
innerShapeDetails.numShapes,
this.ingredients.colors.background,
this.width,
this.height
);
//create grid map
let innerMap = _gridMap(idT, this.ingredients.shapes);
//if it is different from current summary
if (innerSummary !== current.summary.innerHTML) {
//update
current.summary.innerHTML = innerSummary;
}
//if it is different from current map
if (innerMap !== current.map.innerHTML) {
//update
current.map.innerHTML = innerMap;
}
//if it is different from current shape details
if (innerShapeDetails.details !== current.shapeDetails.innerHTML) {
//update
current.shapeDetails.innerHTML = innerShapeDetails.details;
}
this._accessibleOutputs[idT] = current;
};
//creates spatial grid that maps the location of shapes
function _gridMap(idT, ingredients) {
let shapeNumber = 0;
let table = '';
//create an array of arrays 10*10 of empty cells
let cells = Array.from(Array(10), () => Array(10));
for (let x in ingredients) {
for (let y in ingredients[x]) {
let fill;
if (x !== 'line') {
fill = `<a href="#${idT}shape${shapeNumber}">${
ingredients[x][y].color
} ${x}</a>`;
} else {
fill = `<a href="#${idT}shape${shapeNumber}">${
ingredients[x][y].color
} ${x} midpoint</a>`;
}
// Check if shape is in canvas, skip if not
if(
ingredients[x][y].loc.locY < cells.length &&
ingredients[x][y].loc.locX < cells[ingredients[x][y].loc.locY].length
){
//if empty cell of location of shape is undefined
if (!cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX]) {
//fill it with shape info
cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX] =
fill;
//if a shape is already in that location
} else {
//add it
cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX] =
cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX] +
' ' +
fill;
}
shapeNumber++;
}
}
}
//make table based on array
for (let _r in cells) {
let row = '<tr>';
for (let c in cells[_r]) {
row = row + '<td>';
if (cells[_r][c] !== undefined) {
row = row + cells[_r][c];
}
row = row + '</td>';
}
table = table + row + '</tr>';
}
return table;
}
//creates grid summary
function _gridSummary(numShapes, background, width, height) {
let text = `${background} canvas, ${width} by ${height} pixels, contains ${
numShapes[0]
}`;
if (numShapes[0] === 1) {
text = `${text} shape: ${numShapes[1]}`;
} else {
text = `${text} shapes: ${numShapes[1]}`;
}
return text;
}
//creates list of shapes
function _gridShapeDetails(idT, ingredients) {
let shapeDetails = '';
let shapes = '';
let totalShapes = 0;
//goes trhough every shape type in ingredients
for (let x in ingredients) {
let shapeNum = 0;
for (let y in ingredients[x]) {
//it creates a line in a list
let line = `<li id="${idT}shape${totalShapes}">${
ingredients[x][y].color
} ${x},`;
if (x === 'line') {
line =
line +
` location = ${ingredients[x][y].pos}, length = ${
ingredients[x][y].length
} pixels`;
} else {
line = line + ` location = ${ingredients[x][y].pos}`;
if (x !== 'point') {
line = line + `, area = ${ingredients[x][y].area} %`;
}
line = line + '</li>';
}
shapeDetails = shapeDetails + line;
shapeNum++;
totalShapes++;
}
if (shapeNum > 1) {
shapes = `${shapes} ${shapeNum} ${x}s`;
} else {
shapes = `${shapes} ${shapeNum} ${x}`;
}
}
return { numShapes: [totalShapes, shapes], details: shapeDetails };
}
}
export default gridOutput;
if(typeof p5 !== 'undefined'){
gridOutput(p5, p5.prototype);
}