如何添加缓动函数以在动画期间增加变量值

时间:2015-01-29 11:24:46

标签: javascript jquery animation easing-functions

我正在寻找一些数学天才来帮助我解决这个问题:

我有一个非常简单的关键帧js动画:

var i = 0;
var o = 0; // opacity
var y_frames = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H"];   

function draw_y() {

    requestAnimationFrame( draw_y );
    // drawing
    if ( i < y_frames.length ) {  
        $y  .text( y_frames[i] )
            .css( { opacity: o } );
        i++;
        o += 0.03;
    } else {
        // code to be executed when animation finishes
    }
};
draw_y();

除了踩踏关键帧之外,我还添加了一些不透明度,直到达到值1.这非常有效且非常流畅。到现在为止还挺好。 我想添加的是一些easeOutSine到不透明度值的增量。我知道这可以通过使用这个函数来实现:

function easeOutSine(t, b, c, d) {
    return c * Math.sin(t/d * (Math.PI/2)) + b;
}

但我需要一些帮助如何将两者结合起来。我怎样才能做到这一点? 谁在我之前这样做过?非常感谢!

加拉瓦尼

1 个答案:

答案 0 :(得分:1)

这是两种功能组合的方式

var i = 0;
var o = 0; // opacity
var y_frames = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H",];   
function draw_y() {
    // drawing
    if ( i < y_frames.length ) { 
        o = easeOutSine( i , 0 , 1, y_frames.length);
        $y  .text( y_frames[i] )
            .css( { opacity: o } );
        i++;
        window.requestAnimationFrame( draw_y );
    } else {
        // code to be executed when animation finishes
    }
};

function easeOutSine(t, b, c, d) {
    return c * Math.sin(t/d * (Math.PI/2)) + b;
}
window.requestAnimationFrame( draw_y );

注意:始终在条件内调用requestAnimationFrame,否则它将继续无限运行。