帮助制定一个三次缓和方程

时间:2011-09-08 10:46:12

标签: javascript math easing-functions

我有以下代码

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}这样以线性方式输入数字。我想应用立方(in或out)缓动方程,以便输出数字逐渐增加或减少< / p>


更新

不确定我的实施是否正确但我的曲线符合预期

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

2 个答案:

答案 0 :(得分:4)

EasIn Cubic功能

/**
 * @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 Cubic功能

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

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

暂时放一下这段代码,就像你以前一样,你的输出立方数会减少。

答案 1 :(得分: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;
}