Pregunta

Tengo el siguiente código

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

Eso sale de una manera lineal como esta {0, 0.1, 0.2, ..., 0.9, 1.0} Me gustaría aplicar la ecuación cúbica (dentro o fuera) para que los números de salida aumenten o disminuyan gradualmente


ACTUALIZAR

No estoy seguro si mi implementación si es correcta, pero estoy obteniendo curva como se esperaba

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 );
}
¿Fue útil?

Solución

Función cúbica de fácil

/**
 * @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;
}

Forma de la función cúbica

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

Aquí puedes encontrar otras ecuaciones útiles: http://www.gizma.com/easing/#cub1

Coloque este código en un tiempo, ya que antes y tendrá su salida cúbica disminuyendo los números.

Otros consejos

Puede usar el código del complemento JQuery Easing: http://gsgd.co.uk/sandbox/jquery/esing/

/*
*  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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top