如何根据帧速率更新reactivevar?

时间:2016-08-30 15:14:46

标签: meteor

我无法理解流星的ReactiveVar究竟是如何运作的。我认为我应该做什么:

Template.template_canvas.onCreated(function() {
  this.config = new ReactiveVar([{
    name: 'FPS',
    value: Engine.mFPS
  }, {
    name: 'ElapsedTime',
    value: Engine.mElapsedTime
  }, {
    name: 'LagTime',
    value: Engine.mLagTime
  }, {
    name: 'PreviousTime',
    value: Engine.mPreviousTime
  }]);

});

Template.template_canvas.helpers({
  displayConfigs: function() {
    return Template.instance().config.get();
  }
});

所有Engine变量每40ms更新一次。 当我试图展示它们时:

<template name="template_canvas">
    <div id='info_log' draggable="true" >
        <ul>
            {{#each displayConfigs}}
            <li> {{name}}  {{value}} </li>
            {{/each}}
        </ul>
    </div>
    <canvas id='canvas_app'></canvas>
</template>

仅显示Engine的默认变量。 enter image description here 这意味着变量根本没有变化。 但是他们根据console.log改变了。

编辑:

export default class Engine {
  static start(application) {
    Engine.Application = application;
    Engine.mPreviousTime = Date.now();
    Engine.mLagTime = 0.0;
    Engine.mIsRunning = true;
    requestAnimationFrame(function() {
      Engine.run.call(Engine.Application);
    });
  }

  static run() {
    if (Engine.mIsRunning) {
      requestAnimationFrame(
        function() {
          Engine.run.call(Engine.Application);
        }
      );
      Engine.mCurrentTime = Date.now();
      Engine.mElapsedTime = Engine.mCurrentTime - Engine.mPreviousTime;
      Engine.mPreviousTime = Engine.mCurrentTime;
      Engine.mLagTime += Engine.mElapsedTime;

      // LagTime is the sum of the elapsed Time based on the last Frame.
      // So if the time passed is bigger than the given time, which bigger than milliseconds per frame
      // then we have to update.
      while ((Engine.mLagTime >= Engine.mMPF) && Engine.mIsRunning) {
        this.update();
        Engine.mLagTime -= Engine.mMPF;
      }

      // Should be called every 25ms, since we have 25ms per Frame.
      //
      this.draw();

    }
  }

}
// Frames per second.
//
Engine.mFPS = 40;

// Milliseconds per Frame 25ms/Frame with 40 FPS
Engine.mMPF = 1000 / Engine.mFPS;
Engine.mPreviousTime = 0;
Engine.mLagTime = 0;
Engine.mCurrentTime = 0;
Engine.mElapsedTime = 0;

Engine.mIsRunning = false;
Engine.Application = null;

0 个答案:

没有答案