我正在尝试创建一个应用程序,通过读取 Firebase 数据库中存储的圆的 x 和 y 坐标来在画布上绘制圆。然而,执行下面的代码只会简单地产生没有任何圆形的结果,因为函数 drawCircles 是异步运行的,因此命令
以下是我的代码:
background(40)
在圆形被绘制之前清除了所有内容。以下是我的代码:
function setup() {
createCanvas(windowWidth, windowHeight);
background(40);
stroke(80);
smooth();
frameRate(60);
}
function drawCircles() {
firebase.database().ref("circles").once("value", function(snapshot) {
var snapshotVal = snapshot.val();
var circleCount = snapshotVal.numCircles;
for (var j = 0; j < circleCount; j++) {
firebase.database().ref("circles" + j).once("value", function(snapshot) {
var snapshotValue = snapshot.val();
fill(143, 2, 2);
ellipse(snapshotValue.xPos, 50, 50);
});
}
});
}
function draw() {
stroke(80);
background(40);
stroke(0);
drawCircles();
}