-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathtextOutput.js
More file actions
132 lines (125 loc) · 3.93 KB
/
textOutput.js
File metadata and controls
132 lines (125 loc) · 3.93 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
/**
* @module Environment
* @submodule Environment
* @for p5
*/
function textOutput(p5, fn){
//the functions in this file support updating the text output
//updates textOutput
fn._updateTextOutput = function(idT) {
if (this._renderer && this._renderer.isP3D) {
if (!this._didOutputTextWebGLMessage) {
this._didOutputTextWebGLMessage = true;
console.error('textOutput() 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 list
let innerList = _shapeList(idT, this.ingredients.shapes);
//create output summary
let innerSummary = _textSummary(
innerList.numShapes,
this.ingredients.colors.background,
this.width,
this.height
);
//create shape details
let innerShapeDetails = _shapeDetails(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 shape list
if (innerList.listShapes !== current.list.innerHTML) {
//update
current.list.innerHTML = innerList.listShapes;
}
//if it is different from current shape details
if (innerShapeDetails !== current.shapeDetails.innerHTML) {
//update
current.shapeDetails.innerHTML = innerShapeDetails;
}
this._accessibleOutputs[idT] = current;
};
//Builds textOutput summary
function _textSummary(numShapes, background, width, height) {
let text = `Your output is a, ${width} by ${height} pixels, ${background} canvas containing the following`;
if (numShapes === 1) {
text = `${text} shape:`;
} else {
text = `${text} ${numShapes} shapes:`;
}
return text;
}
//Builds textOutput table with shape details
function _shapeDetails(idT, ingredients) {
let shapeDetails = '';
let shapeNumber = 0;
//goes trhough every shape type in ingredients
for (let x in ingredients) {
//and for every shape
for (let y in ingredients[x]) {
//it creates a table row
let row = `<tr id="${idT}shape${shapeNumber}"><th>${
ingredients[x][y].color
} ${x}</th>`;
if (x === 'line') {
row =
row +
`<td>location = ${ingredients[x][y].pos}</td><td>length = ${
ingredients[x][y].length
} pixels</td></tr>`;
} else {
row = row + `<td>location = ${ingredients[x][y].pos}</td>`;
if (x !== 'point') {
row = row + `<td> area = ${ingredients[x][y].area}%</td>`;
}
row = row + '</tr>';
}
shapeDetails = shapeDetails + row;
shapeNumber++;
}
}
return shapeDetails;
}
//Builds textOutput shape list
function _shapeList(idT, ingredients) {
let shapeList = '';
let shapeNumber = 0;
//goes trhough every shape type in ingredients
for (let x in ingredients) {
for (let y in ingredients[x]) {
//it creates a line in a list
let _line = `<li><a href="#${idT}shape${shapeNumber}">${
ingredients[x][y].color
} ${x}</a>`;
if (x === 'line') {
_line =
_line +
`, ${ingredients[x][y].pos}, ${
ingredients[x][y].length
} pixels long.</li>`;
} else {
_line = _line + `, at ${ingredients[x][y].pos}`;
if (x !== 'point') {
_line = _line + `, covering ${ingredients[x][y].area}% of the canvas`;
}
_line = _line + '.</li>';
}
shapeList = shapeList + _line;
shapeNumber++;
}
}
return { numShapes: shapeNumber, listShapes: shapeList };
}
}
export default textOutput;
if(typeof p5 !== 'undefined'){
textOutput(p5, p5.prototype);
}