tween
is an std-optional tweening library, designed for use in games and animations.
This trait exports a trait Tween
, a variety of structs which implement common tweens (such as Elastic
, Quart
, etc), and two Tweeners
, which wrap around tweens, allowing users to drive them trivially.
First, tweens are simple to create:
```rust use tween::SineIn;
let range = 0.0..=200.0; let duration = 60;
let mut sinein = SineIn::new(range, duration); let _value0 = sinein.run(0); let value1 = sinein.run(1); let value2 = sinein.run(2); let value3 = sinein.run(3); ```
Notice how we work over a RangeInclusive
to provide values.
However, as the above example shows, it's more typical that you'll want to simply drive a tween over time, rather than giving arbitrary times within a duration.
For that, we use tweeners, which come in two kinds: Tweener
and FixedTweener
. For both, users provide deltas, rather than arbitrary times. A FixedTweener
uses just one delta (appropriate in a game engine with a Fixed Update pipeline), whereas a Tweener
can take a fixed delta.
```rust use tween::{SineIn, FixedTweener};
let range = 0.0..=200.0; let duration = 60;
let sinein = SineIn::new(range, duration); let delta = 1; let mut sineintweener = FixedTweener::new(sinein, delta);
let value0: Option
// we can finish off the tweener by using the iterator interface.
// we only use &mut
here to demonstrate the None
after this loop, you can just
// take it by value like normal.
for value in &mut sineintweener {
// FixedTweener provides an iterator interface
}
assert!(sineintweener.next().is_none()); ```
This library uses std
with the default feature std
. Disable default features, and enable libm
, for a no-std experience. (We need to use libm
for the floating point math).
This library uses generics heavily. There are two core generics used: TweenValue
and TweenTime
. All built-in numeric types implement both traits. For your own code, you can implement either trait. For example, you could easily implement TweenValue
for your favorite math library.