UNPKG

p5

Version:

[![npm version](https://badge.fury.io/js/p5.svg)](https://www.npmjs.com/package/p5)

87 lines (86 loc) • 3.48 kB
//#region src/accessibility/gridOutput.js /** * @module Environment * @submodule Environment * @for p5 */ function gridOutput(p5, fn) { 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 (!this.dummyDOM.querySelector(`#${idT}_summary`)) return; let current = this._accessibleOutputs[idT]; let innerShapeDetails = _gridShapeDetails(idT, this.ingredients.shapes); let innerSummary = _gridSummary(innerShapeDetails.numShapes, this.ingredients.colors.background, this.width, this.height); let innerMap = _gridMap(idT, this.ingredients.shapes); if (innerSummary !== current.summary.innerHTML) current.summary.innerHTML = innerSummary; if (innerMap !== current.map.innerHTML) current.map.innerHTML = innerMap; if (innerShapeDetails.details !== current.shapeDetails.innerHTML) current.shapeDetails.innerHTML = innerShapeDetails.details; this._accessibleOutputs[idT] = current; }; function _gridMap(idT, ingredients) { let shapeNumber = 0; let table = ""; 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>`; if (ingredients[x][y].loc.locY < cells.length && ingredients[x][y].loc.locX < cells[ingredients[x][y].loc.locY].length) { if (!cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX]) cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX] = fill; else cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX] = cells[ingredients[x][y].loc.locY][ingredients[x][y].loc.locX] + " " + fill; shapeNumber++; } } for (let _r in cells) { let row = "<tr>"; for (let c in cells[_r]) { row = row + "<td>"; if (cells[_r][c] !== void 0) row = row + cells[_r][c]; row = row + "</td>"; } table = table + row + "</tr>"; } return table; } 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; } function _gridShapeDetails(idT, ingredients) { let shapeDetails = ""; let shapes = ""; let totalShapes = 0; for (let x in ingredients) { let shapeNum = 0; for (let y in ingredients[x]) { 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 }; } } if (typeof p5 !== "undefined") gridOutput(p5, p5.prototype); //#endregion export { gridOutput as default };