gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:57:27 25 4
gpt4 key购买 nike

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

我有一个非常简单的关键帧 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;
}

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

加拉瓦尼

最佳答案

这是结合这两个功能的方式

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,否则它将无限运行。

关于javascript - 如何添加缓动函数以在动画期间增加变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28213526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com