#animation #acceleration #constant #minimum #user #target #ease

interruptable_easer

干净且可中断/重定向的缓动动画状态。在给定时间内平滑且可靠地到达目标值,具有最小恒定加速度,永远不会出现任何突然的跳跃或震动,即使在中途被中断时也是如此。

4 个稳定版本

1.1.2 2022 年 6 月 8 日
1.1.0 2022 年 6 月 7 日
1.0.0 2022 年 5 月 20 日

#8 in #minimum

MIT 许可证

12KB
216

沿线性数量运行缓动动画的状态。转折:您可以中断动画并指向不同的目标。使用简单的动画代码,这可能会导致突然跳跃,或者速度会突然变为零。InterruptableEaser 会在不产生任何突然震动或跳跃的情况下智能地重新定位。

它通过仅记住初始速度和初始位置来实现,而不是模拟一个小东西沿着移动。这可能不是实用的(这绝对不是最简单的方法,实现可能需要改进),但对于许多类型的动画来说,这是一个更好的 API。

使用 InterruptableEaser 制作动画生命条的相当真实示例

extern crate blibium; //note, blibium is my simple C++ game engine. There isn't a rust version. The following API might not be possible, but if it is, maybe it would be worth creating blibium rust, because this is better than the C++ version (because C++ doesn't have macros, so it ends up having to use a special langauge for resource stuff).
use blibium::{def, grab, rgb_hex, draw_rectangle, Rect, v2};

def!(health_bar_anim_dur, 0.2);
//ppmr is a unit representing the size of the screen, pixels per milli-radian. We can use it to do things in resolution-independent ways/allow the user to scale the UI.
def!(health_thickness, |ppmr| 0.8*ppmr);
def!(whether_rtl, false);
//resources can be grabbed by other resource definitions
def!(health_anchor, |whether_rtl, ppmr| v2(2.0*ppmr).mirror_x_if(whether_rtl));
def!(health_bar, InterruptableEaser::new(100.0));
def!(health_color, rgb_hex!(F21D40))

async fn take_damage(damage: f32){
    grab!(mut health_bar, t, health_bar_anim_dur);
    let new_health = (health_bar.end_value - damage).max(0.0);
    health_bar.approach(new_health, t, health_bar_anim_dur);
}

async fn render_health(amount: f32){
    grab!(health_bar, health_anchor, health_bar_direction, health_thickness, health_bar_anim_dur, health_color);
    draw_rectangle(
        &Rect::bl_ur(
            health_anchor,
            health_anchor + health_bar_direction*health_bar.v(t, health_bar_anim_dur) + v2(0.0, health_thickness), 
        ),
        health_color,
    );
}

无运行时依赖项