p5
Version:
[](https://www.npmjs.com/package/p5)
71 lines (70 loc) • 2.83 kB
JavaScript
//#region src/accessibility/textOutput.js
/**
* @module Environment
* @submodule Environment
* @for p5
*/
function textOutput(p5, fn) {
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 (!this.dummyDOM.querySelector(`#${idT}_summary`)) return;
let current = this._accessibleOutputs[idT];
let innerList = _shapeList(idT, this.ingredients.shapes);
let innerSummary = _textSummary(innerList.numShapes, this.ingredients.colors.background, this.width, this.height);
let innerShapeDetails = _shapeDetails(idT, this.ingredients.shapes);
if (innerSummary !== current.summary.innerHTML) current.summary.innerHTML = innerSummary;
if (innerList.listShapes !== current.list.innerHTML) current.list.innerHTML = innerList.listShapes;
if (innerShapeDetails !== current.shapeDetails.innerHTML) current.shapeDetails.innerHTML = innerShapeDetails;
this._accessibleOutputs[idT] = current;
};
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;
}
function _shapeDetails(idT, ingredients) {
let shapeDetails = "";
let shapeNumber = 0;
for (let x in ingredients) for (let y in ingredients[x]) {
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;
}
function _shapeList(idT, ingredients) {
let shapeList = "";
let shapeNumber = 0;
for (let x in ingredients) for (let y in ingredients[x]) {
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
};
}
}
if (typeof p5 !== "undefined") textOutput(p5, p5.prototype);
//#endregion
export { textOutput as default };