为临时消息创建图形

问题描述 投票:0回答:1

所以,我想创建一条消息,在屏幕上显示一段时间,然后“删除”它。 该消息可能会更改并且通常是隐藏的。

我目前正在考虑使用 createGraphics 创建消息容器,例如:

messagePoPup = createGraphics(200,120);

需要时显示它,例如:

image(messagePoPup, 100, 100);

并在超时后隐藏它(使用 setTimeOut 或 millis),例如:

setTimeOut(messagePopUp.clear,1000);

那么,image(messagePoPup, 100, 100) 的逆是什么? 即: hidePopUp 函数应该做什么? messagePopUp.clear() 可以解决问题吗?

p5.js
1个回答
0
投票

我会有一个变量指示您是否应该绘制弹出窗口,然后在

draw()
内,您可以根据变量值绘制或不绘制弹出窗口

let drawPopup = true;
let messagePoPup;

setup() {
  messagePoPup = createGraphics(200, 120);

  // Populate message popup with some text
  messagePopup.text("hello", 50, 50);

  // sets the drawPopup variable to false after 5 seconds
  setTimeout(() => {
    drawPopup = false;    
  }, 5000);
}

draw() {
  // clear the screen
  fill(255);

  // only draw the popup if the variable drawPopup is true
  if (drawPopup) {
     // draw the popup window
     image(messagePoPup, 100, 100);
  }
  // draw the rest of the page
}
© www.soinside.com 2019 - 2024. All rights reserved.