createNumberDict()函數用於創建具有給定數據的p5.NumberDict實例。數據可以單獨傳遞鍵值對,也可以使用對象作為值的集合給出。
用法:
createNumberDict(key, value)
或者
createNumberDict(object)
參數:
- key:它指定用作字典中鍵的數字。
- value:它指定用作字典中值的數字。
- object:它指定用作字典的對象。
返回值:它返回帶有給定數據的p5.NumberDict對象。
以下程序說明了p5.js中的createNumberDict()函數:
範例1:
function setup() {
createCanvas(500, 200);
textSize(20);
// Creating a number dictionary
// with the given key and value pair
let mydict = createNumberDict("19", "1999");
// Accessing the data using the data property
text('The dictionary has the following data under "19":', 20, 20);
text(mydict.data["19"], 20, 40);
// Checking if a key exists in the dictionary
text('The dictionary has the "25" key present:', 20, 80);
text(mydict.hasKey("19"), 20, 100);
text('The dictionary has the "100" key present:', 20, 140);
text(mydict.hasKey("100"), 20, 160);
}
輸出:
範例2:
function setup() {
createCanvas(600, 200);
textSize(20);
let squaresObject = {
2:4,
3:9,
4:16,
5:25
}
// Creating a number dictionary
// with the above object
let mydict = createNumberDict(squaresObject);
// Accessing the data using the data property
text('The dictionary has the following data under key "2":', 20, 20);
text(mydict.data["2"], 20, 40);
text('The dictionary has the following data under key "4":', 20, 80);
text(mydict.data["4"], 20, 100);
text('The dictionary has the following data under key "5":', 20, 140);
text(mydict.data["5"], 20, 160);
}
輸出:
參考: https://p5js.org/reference/#/p5/createNumberDict
相關用法
- PHP pow( )用法及代碼示例
- p5.js min()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP next()用法及代碼示例
- p5.js hue()用法及代碼示例
- p5.js value()用法及代碼示例
- p5.js pan()用法及代碼示例
- PHP each()用法及代碼示例
- d3.js d3.min()用法及代碼示例
- p5.js sin()用法及代碼示例
- d3.js d3.max()用法及代碼示例
- p5.js tan()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js | createNumberDict() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。