舞台

3. cheatsheet

3.7. 循环执行


loop()

示例:

▶️运行示例代码



let x = 0;
function setup() {
  createCanvas(100, 100);
}
function draw() {
  background(204);
  x = x + 0.1;
  if (x > width) {
    x = 0;
  }
  line(x, 0, x, height);
}
function mousePressed() {
  noLoop();
}
function mouseReleased() {
  loop();
}

舞台区显示的画布内容如下:


Description

By default, p5.js loops through draw() continuously, executing the code within it. However, the draw() loop may be stopped by calling noLoop(). In that case, the draw() loop can be resumed with loop().

Avoid calling loop() from inside setup().

Use isLooping() to check the current state of loop().

一般情况下,p5.js的循环是通过调用draw()函数,draw()函数中的代码是持续循环执行的。然而,通过调用noLoop()函数可以停止draw()函数的循环执行。当循环中止的情况下,可以使用loop()来恢复draw()函数的循环执行。

避免从setup()函数内部调用loop()函数。

使用isLooping()函数检查loop()函数的当前状态。

Syntax

loop()


标签: