p5js怎么导入html,p5.js loadTable()用法及代码示例

loadTable()函数用于读取文件或URL的内容并从中创建p5.Table对象。 options参数可用于定义期望读取数据的格式类型。所有加载和保存的文件均采用UTF-8编码。

该函数是异步的,因此建议在preload()函数中调用该函数,以确保该函数先于其他函数执行。

用法:

loadTable(filename, options, [callback], [errorCallback])

或者

loadTable(filename, [callback], [errorCallback])

参数:该函数接受上面提到和下面描述的四个参数。

filename:这是一个字符串,表示必须从中加载数据的文件路径或URL。

options:它是一个字符串,表示要加载的文件的格式。可以是“csv”(使用逗号分隔值加载表),也可以是“tsv”(使用制表符分隔值加载表)。还可以指定值“header”来表示表是否具有标题值。通过将它们作为单独的参数传递,可以使用多个命令。它是一个可选参数。

callback:当该函数成功执行时,将调用此函数。此函数的第一个参数是从文件加载的XML数据。它是一个可选参数。

errorCallback:如果执行该函数时有任何错误,则调用该函数。此函数的第一个参数是错误响应。它是一个可选参数。

以下示例说明了p5.js中的loadTable()函数:

范例1:

// Contents of books.csv

// Book One, Author One, Price One

// Book Two, Author Two, Price Two

// Book Three, Author Three, Price Three

let loadedTable = null;

function setup() {

createCanvas(500, 300);

textSize(18);

text("Click on the button below to"+

" load Table from file", 20, 20);

// Create a button for loading the table

loadBtn = createButton("Load Table from file");

loadBtn.position(30, 50)

loadBtn.mousePressed(loadFile);

}

function loadFile() {

// Load the table from file

loadedTable = loadTable('books.csv', 'csv', onFileload);

}

function onFileload() {

text("Table loaded successfully...", 20, 100);

// Display through the table

for (let r = 0; r < loadedTable.getRowCount(); r++) {

for (let c = 0; c < loadedTable.getColumnCount(); c++) {

text(loadedTable.getString(r, c),

20 + c * 200, 150 + r * 20);

}

}

}

输出:

bfc36c754ffdeedf5a5ea9e93014bd3f.gif

范例2:

// Contents of books_header.csv

// title, author, price

// Book One, Author One, Price One

// Book Two, Author Two, Price Two

// Book Three, Author Three, Price Three

let loadedTable = null;

function setup() {

createCanvas(500, 300);

textSize(22);

text("Click on the button below to "

+ "load Table from file", 20, 20);

// Create a button for loading the table

loadBtn = createButton("Load Table from file");

loadBtn.position(30, 50)

loadBtn.mousePressed(loadFile);

}

function loadFile() {

// Load the table from file with headers

loadedTable = loadTable('books_header.csv',

'csv', 'header', onFileload);

}

function onFileload() {

text("Table loaded successfully...", 20, 100);

// Display the headers

for (let h = 0; h < loadedTable.getColumnCount(); h++) {

text(loadedTable.columns[h], 20 + h * 200, 150);

}

textSize(16);

// Display the data in the table

for (let r = 0; r < loadedTable.getRowCount(); r++) {

for (let c = 0; c < loadedTable.getColumnCount(); c++) {

text(loadedTable.getString(r, c),

20 + c * 200, 170 + r * 20);

}

}

}

输出:

5f457171d66e4b4dc6b1d8d3136e9b66.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值