p5.TableRow setNum() 方法
p5.js 中 p5.TableRow 的setNum() 方法用于将给定的 Float 值存储到表的给定列中。列可以由其列 ID 或列名指定。
句法:
setNum( column, value )
参数:此方法接受上面提到的两个参数,如下所述:
- column:它是一个字符串或数字,表示列的名称或列的 ID。
- value:它是一个数字,指定必须存储的值。
下面的示例说明了 p5.js 中的setNum() 方法:
例子:
javascript
function setup() {
createCanvas(500, 300);
textSize(18);
text("Enter the index of the table " +
"to be modified", 20, 20);
rowInput = createInput();
rowInput.size(30);
rowInput.position(30, 40);
colInput = createInput();
colInput.size(30);
colInput.position(80, 40);
setBtn =
createButton("Modify Given Row and Column");
setBtn.position(30, 80);
setBtn.mouseClicked(modifyTableData);
// Create the table
table = new p5.Table();
// Add 5 columns and rows to the table
for (let i = 0; i < 5; i++) {
table.addColumn("Column " + i);
table.addRow();
}
for (let r = 0; r < 5; r++) {
// Get the table row from the table
let tableRow = table.rows[r];
for (let c = 0; c < 5; c++) {
// Set the value at the given
// column of the table row
tableRow.set(c, r * 100 + c);
}
}
showTable();
}
function modifyTableData() {
givenRow = int(rowInput.value());
givenCol = int(colInput.value());
if (givenRow < table.getRowCount() &&
givenCol < table.getColumnCount()) {
// Get the specified table row
let tableRow = table.rows[givenRow];
// Set a number value to the given
// column of the table row
tableRow.setNum(givenCol, -99999);
}
showTable();
}
function showTable() {
clear();
text("The value is modified using " +
"the setNum() method", 20, 20);
let colCount = table.getColumnCount();
let rowCount = table.getRowCount();
// Show all the columns present
for (let c = 0; c < colCount; c++) {
text(table.columns, 20 + 100 * c, 120);
}
// Show all the rows currently
// present in the table
for (let r = 0; r < rowCount; r++) {
for (let c = 0; c < colCount; c++) {
text(table.getString(r, c),
20 + 100 * c, 150 + 20 * r);
}
}
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.TableRow/setNum