loadBytes()函數用於讀取文件或URL的內容,並將其作為包含字節序列的對象返回。然後可以使用對象的“bytes”屬性訪問字節。該文件必須存在於草圖目錄中才能訪問。此方法可以支持最大64MB的文件。
該函數是異步的,因此建議在preload()函數中調用該函數,以確保該函數先於其他函數執行。
用法:
loadBytes(file, [callback], [errorCallback])
參數:此函數接受上述和以下所述的三個參數:
- file:它是一個字符串,表示必須從中加載XML數據的文件路徑或URL。
- callback:當該函數成功執行時,將調用該函數。此函數的第一個參數是從文件加載的XML數據。它是一個可選參數。
- errorCallback:如果執行該函數有任何錯誤,則會調用該函數。此函數的第一個參數是錯誤響應。它是一個可選參數。
返回值:它返回一個對象,該對象的“bytes”屬性設置為從文件加載的字節。
以下示例說明了p5.js中的loadBytes()函數:
例:
let loadedBytes = null;
function setup() {
createCanvas(500, 300);
textSize(22);
text("Click on the button below to "
+ "load bytes from file", 20, 20);
// Create a button for loading the XML
loadBtn = createButton("Load bytes from file");
loadBtn.position(30, 50)
loadBtn.mousePressed(loadFileBytes);
}
function loadFileBytes() {
// Load bytes from file
loadedBytes = loadBytes('characters.txt', onFileload);
}
function onFileload() {
text("Bytes loaded successfully...", 30, 100);
// Print the bytes
for (let i = 0; i < loadedBytes.bytes.length; i++)
text(loadedBytes.bytes[i], 30 + i * 50, 150);
}
輸出:
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5/loadBytes
相關用法
- p5.js pow()用法及代碼示例
- PHP Ds\Set contains()用法及代碼示例
- p5.js log()用法及代碼示例
- p5.js second()用法及代碼示例
- p5.js tan()用法及代碼示例
- p5.js nf()用法及代碼示例
- PHP min( )用法及代碼示例
- p5.js sin()用法及代碼示例
- p5.js cos()用法及代碼示例
- p5.js hue()用法及代碼示例
- PHP ord()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js | loadBytes() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。