xxxxxxxxxx
60
let my_table
let count = 0;
function preload() { // can use later if file exists ( is uploaded to data/test.csv )
// my_table = loadTable("data/test.csv", "csv", "header");
}
function setup() {
createCanvas(200, 200);
make_my_table(); // step 1 make table and add columns
add_row(); // step 2 add first row
check_table(); // step 3 print the table
print("!! click on canvas to add rows");
}
function make_my_table() {
my_table = new p5.Table();
my_table.addColumn('name');
my_table.addColumn('posx');
my_table.addColumn('posy');
}
function add_row () {
var mx = mouseX;
var my = mouseY;
print("mx " + mx + " my "+my+ " count "+count);
var newRow = my_table.addRow();
newRow.setString('name', "line"+count);
newRow.setNum('posx', mx);
newRow.setNum('posy', my);
count++;
}
function check_table() {
print(my_table.getRowCount() + ' total rows in table');
print(my_table.getColumnCount() + ' total columns in table');
//cycle through the table
for (var r = 0; r < my_table.getRowCount(); r++)
for (var c = 0; c < my_table.getColumnCount(); c++) {
print("row " + r + " col " + c + " : " + my_table.getString(r, c));
}
}
function save_my_table() {
// called by key [s] and offers download file
saveTable(my_table, 'test.csv');
}
function draw() {
background(220);
}
function keyPressed() {
if ( key == 's' ) save_my_table();
}
function mousePressed() {
add_row();
check_table();
}