简体   繁体   English

PixiJS - 设置固定帧率

[英]PixiJS - Setting a Fixed Frame Rate

As the title says, how do you set a fixed frame rate of 25 fps for PixiJS?正如标题所说,如何为 PixiJS 设置 25 fps 的固定帧率?

Here is my setup:这是我的设置:

g_App = new PIXI.Application(800, 600, { backgroundColor: 0x1099bb });
document.getElementById("canvas-div").appendChild(g_App.view);

I do not want to do any more frames than that.我不想做更多的框架。

After @wavemode's comments about PixiJS using requestAnimationFrame I think I may have to do the following.在 @wavemode 关于使用 requestAnimationFrame 的 PixiJS 的评论之后,我想我可能必须执行以下操作。 (Note: if there is a better solution, please post it, otherwise I will mark this as the answer.) (注意:如果有更好的解决方案,请贴出来,否则我会标记为答案。)

Basically, stop any animation if we are exceeding the frame rate.基本上,如果我们超过帧速率,则停止任何 animation。

var g_TICK = 40; // 1000/40 = 25 frames per second
var g_Time = 0;

Then later on when we set up the animation:稍后当我们设置 animation 时:

// Listen for animate update
g_App.ticker.add(function (delta) {
    // Limit to the frame rate
    var timeNow = (new Date()).getTime();
    var timeDiff = timeNow - g_Time;
    if (timeDiff < g_TICK)
        return;

    // We are now meeting the frame rate, so reset the last time the animation is done
    g_Time = timeNow;

    // Now do the animation

    // rotate the container!
    // use delta to create frame-independent tranform
    container.rotation -= 0.01 * delta;
    g_Bunny0.x += 1;
});

You can change the maximum FPS of the application like this:您可以像这样更改应用程序的最大 FPS:

g_App.ticker.maxFPS = 25;

(The maxFPS needs to be higher than the minFPS value) (maxFPS 需要高于 minFPS 值)

25 FPS is 40 milliseconds per frame. 25 FPS 是每帧 40 毫秒。 So, every frame, you should be calling所以,每一帧,你都应该调用

setTimeout( myRenderFunction, 40 )设置超时(我的渲染函数,40)

if you want the screen to update 25 times per second.如果您希望屏幕每秒更新 25 次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM