p5.js | clearStorage()函数
p5.js 中的clearStorage()函数用于使用storeItem ()函数清除本地存储集中存在的所有项目。当前域的项目被删除。尝试使用getItem()函数检索任何项目现在将返回 null。它不接受任何参数。
句法:
clearStorage()
参数:此函数不接受任何参数。
下面的程序说明了 p5.js 中的clearStorage()函数:
例子:
function setup() {
createCanvas(400, 300);
textSize(16);
text("Use the button to set and"+
" retrieve random values", 20, 20);
setBtn = createButton("Set items to storage");
setBtn.position(20, 150);
setBtn.mouseClicked(setItems);
getBtn = createButton("Get items from storage");
getBtn.position(20, 180);
getBtn.mouseClicked(getItems);
clearBtn = createButton("Clear items from storage");
clearBtn.position(20, 210);
clearBtn.mouseClicked(clearItems);
}
function clearItems() {
clear();
text("Use the button to set and retrieve"+
" random values", 20, 20);
text("Storage Cleared!", 20, 40);
// Clear all items in storage
clearStorage();
}
function getItems() {
clear();
text("Use the button to set and retrieve"+
" random values", 20, 20);
// Retrieve values from local storage
id = getItem("savedNumber");
author = getItem("savedString");
isBestseller = getItem("savedBoolean");
// Display the values
text("The retrieved items are:", 20, 40);
text("Book ID: " + id, 20, 60);
text("Author: " + author, 20, 80);
text("Bestseller: " + isBestseller, 20, 100);
}
function setItems() {
clear();
text("Use the button to set and retrieve"+
" random values", 20, 20);
text("Random items set!", 20, 40);
// Generate random values
randomID = floor(random(100));
randomAuthor = "Author " + randomID;
randomBool = randomID > 50 ? true : false;
// Store values to local storage
storeItem("savedNumber", randomID);
storeItem("savedString", randomAuthor);
storeItem("savedBoolean", randomBool);
}
输出:
参考: https://p5js.org/reference/#/p5/clearStorage
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5/rectMode