【发布时间】:2022-02-16 00:59:58
【问题描述】:
我以为我在所有情况下都掌握了承诺,但我被困在这里:我希望在计时器上发生动画。我可以从图形包中获得的唯一信息是帧号,因此我根据帧速率估计来计算结束帧。
图形包是p5.js,它公开了frameCount,它在每个绘制周期仅以大约60 fps 的速度运行。 (我在这个问题中将其命名为 currentFrame,希望能澄清)。据我所知,这是我在 p5.js 中进入动画帧状态的唯一可见性
doAnimation(duration) {
// animation stuff
this.endFrame = currentFrame + (60 * duration); // 60fps seems to be a good guess
}
draw() {
if (currentFrame < this.endFrame) {
// compute and draw the state of the animation
} else if (currentFrame >= this.endFrame) {
// animation done
}
}
我很困惑的是如何给 doAnimation 调用者一个在其他方法中解析的承诺。我试过这个:
doAnimation(duration) {
// animation stuff
this.endFrame = currentFrame + (60 * duration);
this.animationPromise = new Promise(resolve => {
// I realize this is wrong, but it illustrates the problem. How do I run this test later?
if (currentFrame >= this.endFrame) resolve();
});
return this.animationPromise;
}
draw() {
if (currentFrame < this.endFrame) {
// compute and draw the state of the animation
} else if (currentFrame >= this.endFrame) {
this.animationPromise.resolve(); // also wrong, I think
// since resolve isn't an instance method of a promise
}
}
有人能帮我搞清楚这个吗?
【问题讨论】:
-
currentFrame是某个东西的参数吗?draw的?还是真的是全球性的? -
@trincot - 图形包将其公开为全局
-
承诺的目的是什么?在做某事之前等待承诺的是什么?
-
@T.J.Crowder - 调用者想要在这个动画完成后开始另一个动画。所以来电者想说
object.doAnimation(2).then( // do another -
我会为这个问题添加更多说明。
标签: javascript promise