function
setup() {
createCanvas(500, 300);
textSize(18);
text(
"Click on the button to set the"
+
" values of the table"
, 20, 20);
setBtn = createButton(
"Add Table Values"
);
setBtn.position(30, 40);
setBtn.mouseClicked(setTableData);
table =
new
p5.Table();
for
(let i = 0; i < 5; i++) {
table.addColumn(
"Column "
+ i);
table.addRow();
}
}
function
setTableData() {
for
(let r = 0; r < 5; r++) {
for
(let c = 0; c < 5; c++) {
table.set(r, c, r +
" "
+ c);
}
}
showTable();
}
function
showTable() {
clear();
text(
"Click on the button to set the"
+
" values of the table"
, 20, 20);
for
(let c = 0; c < 5; c++) {
text(table.columns, 20 + 80 * c, 80);
}
for
(let r = 0; r < 5; r++) {
for
(let c = 0; c < 5; c++) {
text(table.getString(r, c),
20 + 80 * c,
100 + 20 * r);
}
}
}