【问题标题】:nodejs reverse counting (increment/decrement)nodejs反向计数(递增/递减)
【发布时间】:2017-07-16 12:24:07
【问题描述】:

我用 jquery 缓动函数编写了一个小动画库。 现在我遇到问题,当我的起始值大于或等于结束值时,我的函数 dosnt 工作。

var easing = require("../easing.js").ease;


function fade(start_value, end_value, duration) {

  var interval = 46;
  var time = 0;


  setInterval(function () {

    // calc current time
    time = time + interval;

    // call easing lib
    value = easing["linear"](time, start_value, end_value, duration);
    value = Math.round(value);
    
    if(value >= end_value){
      clearInterval(this);
      console.log("Done");
    }
    
    console.log(value);

  }, interval);
}


fade(255, 0, 1000); // 255 immediately
fade(0, 255, 1000); // 0 to 255 in 1s

我希望当起始值大于结束值时减少计数。 我怎样才能用 easing lib 做到这一点?

非常感谢

【问题讨论】:

  • 你能提供一个工作代码sn-p吗?我收到一条错误消息。

标签: jquery node.js animation easing fading


【解决方案1】:

你的问题是这一行

  if(value >= end_value){

在这里,您在编写它时假设您正朝着end_value 前进。相反,该行应该不知道start_value 是否大于或小于end_value。您的函数应如下所示:

function fade(start_value, end_value, duration) {

  ...

  var min_value = Math.min(start_value, end_value);
  var max_value = Math.max(start_value, end_value);

  ...

  // Stop the animation when out of range
  if (value <= min_value || value >= max_value) {
    clearInterval(this);
    console.log("Done");
  }

  ...

}

【讨论】:

  • 对不起,但这不起作用。这里来自“easing”方法“linear”的来源:function linear(t, b, c, d) { return c * t/d + b; },我认为问题在于这个函数,如果你像“linear(0, 255, 0, 1000)”这样调用这个函数,它会立即返回“255”
猜你喜欢
  • 2013-03-27
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
相关资源
最近更新 更多