p5.Table getColumnCount() 方法
p5.js 中 p5.Table 的getColumnCount() 方法用于返回一个表对象的总列数。
句法:
getColumnCount()
参数:此函数不接受任何参数。
返回值:它返回一个整数值,它指定表中的列数。
下面的示例说明了 p5.js 中的getColumnCount() 方法:
例子:
let colCount = 3;
function setup() {
createCanvas(500, 400);
textSize(16);
addColBtn = createButton("Add Column");
addColBtn.position(30, 50);
addColBtn.mouseClicked(addOneColumn);
removeColBtn =
createButton("Clear Last Column");
removeColBtn.position(160, 50);
removeColBtn.mouseClicked(clearLastColumn);
// Create the table
table = new p5.Table();
// Add columns
table.addColumn("Column 1");
table.addColumn("Column 2");
// Display the table
showTable();
}
function clearLastColumn() {
let lastColumn =
table.getColumnCount() - 1;
if (lastColumn >= 0)
table.removeColumn(lastColumn);
showTable();
}
function addOneColumn() {
table.addColumn("Column " + colCount);
colCount++;
showTable();
}
function showTable() {
clear();
text("Click on the buttons to change" +
" the number of columns in the table",
20, 20);
// Get the number of columns
// currently in the table
let columnCount = table.getColumnCount();
// Display the total columns
// present in the table
text("There are " + columnCount +
" columns in the table",
20, 100);
// Show all the column names
// currently present in the table
for (let c = 0; c < columnCount; c++)
text(table.columns, 30, 140 + c * 20);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.Table/getColumnCount