如何延迟p5中的元素绘制?

时间:2019-05-11 22:15:28

标签: javascript p5.js

我有一个单词数组,我只想在经过一定的秒数后才在数组中显示以下元素。当我遍历每个元素时,draw有效地显示了每个元素,但同时也这样做。如何延迟此过程?

这是当前代码的样子:

=SUM(A{start}:A{end})/60/60

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

要在单词之间添加延迟,以使所有单词不会同时显示,则可以按索引访问数组,并根据帧数增加索引。

let x = 0;
let speed = 3;
let words = ['hello', 'this', 'is', 'a', 'test'];
let wordIndex = 0;

function setup() {
  createCanvas(200, 100);
  frameRate(10);
  fill(0, 102, 153);
  textSize(32);
}

function draw() {
  background(255);
  // increment the word index every tenth frame
  if (frameCount % 10 === 0){
    wordIndex++;
  }
  // when we reach the end of the array reset x and wordIndex
  if (wordIndex === words.length){
    wordIndex = 0;
    x = 0;
  }
  var word = words[wordIndex];
    text(word, x, height/2);
    x += speed;
} 
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>