舞台
3. cheatsheet
3.1. 预先加载资源
preload()
示例:
let img;
let c;
function preload() {
// preload() runs once
img = loadImage('assets/laDefense.jpg');
}
function setup() {
// setup() waits until preload() is done
img.loadPixels();
// get color of middle pixel
c = img.get(img.width / 2, img.height / 2);
}
function draw() {
background(c);
image(img, 25, 25, 50, 50);
}
舞台区显示的画布内容如下:
Description
Called directly before setup(), the preload() function is used to handle asynchronous loading of external files in a blocking way. If a preload function is defined, setup() will wait until any load calls within have finished. Nothing besides load calls (loadImage, loadJSON, loadFont, loadStrings, etc.) should be inside the preload function. If asynchronous loading is preferred, the load methods can instead be called in setup() or anywhere else with the use of a callback parameter.
By default the text "loading..." will be displayed. To make your own loading page, include an HTML element with id "p5_loading" in your page. More information here.
Syntax
preload()