📜  p5.js | createStringDict()函数

📅  最后修改于: 2022-05-13 01:56:33.166000             🧑  作者: Mango

p5.js | createStringDict()函数

createStringDict()函数用于使用给定数据创建 p5.StringDict 实例。数据可以作为键值对单独传递,也可以使用对象作为值的集合给出。

句法:

createStringDict(key, value)

或者

createStringDict(object)

参数:

  • key:它指定用作字典中的键的字符串。
  • value:它指定用作字典中的值的字符串。
  • object:它指定用作字典的对象。

返回值:它返回一个带有给定数据的p5.StringDict对象。
下面的程序说明了 p5.js 中的createStringDict()函数
示例 1:

javascript
function setup() {
  createCanvas(500, 200);
  textSize(20);
 
  // Creating a string dictionary
  // with the given key and value pair
  let mydict = createStringDict("title", "GeeksForGeeks");
 
  // Accessing the data using the data property
  text('The dictionary has the following data under "title":', 20, 20);
  text(mydict.data.title, 20, 40);
 
  // Checking if a key exists in the dictionary
  text('The dictionary has the "title" key present:', 20, 80);
  text(mydict.hasKey("title"), 20, 100);
 
  text('The dictionary has the "author" key present:', 20, 140);
  text(mydict.hasKey("author"), 20, 160);
}


javascript
function setup() {
  createCanvas(600, 200);
  textSize(20);
 
  let obj = {
    book: "Let Us C",
    author: "Yashavant Kanetkar",
    language: "English"
  }
 
  // Creating a string dictionary
  // with the above object
  let mydict = createStringDict(obj);
 
  // Accessing the data using the data property
  text('The dictionary has the following data under "title":', 20, 20);
  text(mydict.data.book, 20, 40);
   
  text('The dictionary has the following data under "author":', 20, 80);
  text(mydict.data.author, 20, 100);
   
  text('The dictionary has the following data under "language":', 20, 140);
  text(mydict.data.language, 20, 160);
}


输出:

前一

示例 2:

javascript

function setup() {
  createCanvas(600, 200);
  textSize(20);
 
  let obj = {
    book: "Let Us C",
    author: "Yashavant Kanetkar",
    language: "English"
  }
 
  // Creating a string dictionary
  // with the above object
  let mydict = createStringDict(obj);
 
  // Accessing the data using the data property
  text('The dictionary has the following data under "title":', 20, 20);
  text(mydict.data.book, 20, 40);
   
  text('The dictionary has the following data under "author":', 20, 80);
  text(mydict.data.author, 20, 100);
   
  text('The dictionary has the following data under "language":', 20, 140);
  text(mydict.data.language, 20, 160);
}

输出:

ex2

在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5/createStringDict