3

我有以下一段代码

int steps = 10;
for (int i = 0; i <= steps; i++) {
    float t = i / float(steps);
    console.log( "t  " + t );
}

这将数字置于这样的线性方式 { 0, 0.1, 0.2, ..., 0.9, 1.0 } 我想应用三次(输入或输出)缓动方程,以便输出数字逐渐增加或减少


更新

不确定我的实现是否正确,但我得到了预期的曲线

float b = 0;
float c = 1;
float d = 1;
for (int i = 0; i <= steps; i++) {
    float t = i / float(steps);

    t /= d;

    float e = c * t * t * t + b;

    console.log( "e  " + e );
    //console.log( "t  " + t );
}
4

2 回答 2

4

EasIn 三次函数

/**
 * @param {Number} t The current time
 * @param {Number} b The start value
 * @param {Number} c The change in value
 * @param {Number} d The duration time
 */ 
function easeInCubic(t, b, c, d) {
   t /= d;
   return c*t*t*t + b;
}

EaseOut 三次函数

/**
 * @see {easeInCubic}
 */
function easeOutCubic(t, b, c, d) {
   t /= d;
   t--;
   return c*(t*t*t + 1) + b;
}

在这里您可以找到其他有用的方程式:http ://www.gizma.com/easing/#cub1

像以前一样,将这段代码放在一段时间后,您将获得输出三次递减的数字。

于 2011-09-08T11:07:16.140 回答
1

您可以使用 jQuery Easing 插件中的代码:http: //gsgd.co.uk/sandbox/jquery/easing/

/*
*  t: current time
*  b: begInnIng value
*  c: change In value
*  d: duration
*/

easeInCubic: function (x, t, b, c, d) {
    return c*(t/=d)*t*t + b;
},
easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
},
easeInOutCubic: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t + b;
    return c/2*((t-=2)*t*t + 2) + b;
}
于 2011-09-08T11:08:18.573 回答