PixiJS - 设置固定帧率

时间:2017-04-14 18:01:44

标签: javascript frame-rate pixi.js

正如标题所说,你如何为PixiJS设置25 fps的固定帧速率?

这是我的设置:

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

我不想再做那些帧了。

2 个答案:

答案 0 :(得分:1)

@ wavemode使用requestAnimationFrame关于PixiJS的评论后,我想我可能需要做以下事情。 (注意:如果有更好的解决方案,请发布,否则我会将其标记为答案。)

基本上,如果我们超过帧速率,请停止任何动画。

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

然后我们设置动画时:

// 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;
});

答案 1 :(得分:0)

25 FPS每帧40毫秒。所以,每一帧,你应该打电话

  

setTimeout(myRenderFunction,40)

如果您希望屏幕每秒更新25次。