Search code examples
javascripteasingeasing-functions

Easing Function in JavaScript - set change in value to 0


// t: current time, b: beginning value, c: change in value, d: duration
function easeInQuad(t, b, c, d) {
    return c*(t/=d)*t + b;
}
function easeOutElastic(t, b, c, d) {
    var s = 1.70158;
    var p = 0;
    var a = c;
    if (t == 0) return b;
    if ((t /= d) == 1) return b + c;
    if (!p) p = d * .3;
    if (a < Math.abs(c)) {
        a = c;
        var s = p / 4;
    } else var s = p / (2 * Math.PI) * Math.asin(c / a);
    return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
}

easeInQuad(100, 20, 0, 1000)  // 20
easeInQuad(500, 20, 0, 1000)  // 20

easeOutElastic(0, 20, 0, 1000)  // 20
easeOutElastic(500, 20, 0, 1000)  // NaN
easeOutElastic(1000, 20, 0, 1000)  // 20

if change in value is 0, easeInQuad is always return 20, easeOutElastic is also return wrong value.

the change in value can't be 0 ? How jQuery handle animate({top: 0}), set 0 to 0.0001?


i misunderstand what change in value meaning. if i want end value be 0, parameter c should be -20


Solution

  • For the following execution of easeInQuad

    easeInQuad(100, 20, 0, 1000)
    

    I get 20 because of the multiplication of 0 across most of your formula.

    0*(100/=1000)*100 + 20 = 20
    

    After looking at other easing formulas I think this is expected behavior. See jQuery source here

    Further tests show that changing 0 to 1 should produce out of 20.01 and this trend continues. Again I think this is expected behavior.