Help formulating a cubic easing equation

帅比萌擦擦* 提交于 2019-12-10 09:45:32

问题


I have the following piece of code

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

That out puts numbers in a linear fashion like this { 0, 0.1, 0.2, ..., 0.9, 1.0 } I would like apply the cubic (in or out) easing equation so the output numbers increase or decrease gradually


UPDATE

Not sure if my implementation if correct but I am getting curve as expected

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

回答1:


EasIn Cubic Function

/**
 * @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 Function

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

Here you can find othere useful equations: http://www.gizma.com/easing/#cub1

Put this code in a while, as you've don before and you will have your output cubic decreasing numbers.




回答2:


You can use code from jQuery Easing plugin: 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;
}


来源:https://stackoverflow.com/questions/7346805/help-formulating-a-cubic-easing-equation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!