Compare commits
9 Commits
85c788ba57
..
v1.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
8518ea1bd1
|
|||
|
5c81f35250
|
|||
|
02ce2097fb
|
|||
|
36a2e0e9e2
|
|||
|
b4f5d73cbd
|
|||
|
303028dc10
|
|||
|
feb9f76053
|
|||
|
2b1c2cd9d4
|
|||
|
504bfa9bfe
|
@@ -1,3 +1,2 @@
|
||||
/target/
|
||||
/Cargo.lock
|
||||
/flake.lock
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
## 1.0.2
|
||||
|
||||
* use rust edition 2024
|
||||
* make all functions inlinable
|
||||
* add `#[must_use]` to all functions
|
||||
* enable clippy lints and fix
|
||||
|
||||
## 1.0.1
|
||||
|
||||
* repair `elastic_out` function
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
[package]
|
||||
name = "simple-easing"
|
||||
version = "1.0.1"
|
||||
version = "1.0.2"
|
||||
description = "Set of simple easing functions"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
repository = "https://gitlab.com/chpio/simple-easing"
|
||||
license = "MIT OR Apache-2.0"
|
||||
include = [
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
{
|
||||
description = "simple-easing";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let pkgs = import nixpkgs { inherit system; };
|
||||
in {
|
||||
devShell = pkgs.mkShell {
|
||||
name = "simple-easing";
|
||||
buildInputs = with pkgs; [
|
||||
cargo
|
||||
rustc
|
||||
rustfmt
|
||||
rust-analyzer
|
||||
];
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
+13
-4
@@ -3,20 +3,29 @@ const C2: f32 = C1 * 1.525;
|
||||
const C3: f32 = C1 + 1.0;
|
||||
|
||||
/// <https://easings.net/#easeInBack>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn back_in(t: f32) -> f32 {
|
||||
C3 * t * t * t - C1 * t * t
|
||||
(C3 * t * t).mul_add(t, -(C1 * t * t))
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutBack>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn back_out(t: f32) -> f32 {
|
||||
1.0 + C3 * (t - 1.0).powi(3) + C1 * (t - 1.0).powi(2)
|
||||
C1.mul_add((t - 1.0).powi(2), C3.mul_add((t - 1.0).powi(3), 1.0))
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutBack>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn back_in_out(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
((2.0 * t).powi(2) * ((C2 + 1.0) * 2.0 * t - C2)) / 2.0
|
||||
((2.0 * t).powi(2) * ((C2 + 1.0) * 2.0).mul_add(t, -C2)) / 2.0
|
||||
} else {
|
||||
((2.0 * t - 2.0).powi(2) * ((C2 + 1.0) * (t * 2.0 - 2.0) + C2) + 2.0) / 2.0
|
||||
f32::midpoint(
|
||||
2.0f32.mul_add(t, -2.0).powi(2) * (C2 + 1.0).mul_add(t.mul_add(2.0, -2.0), C2),
|
||||
2.0,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+12
-6
@@ -1,28 +1,34 @@
|
||||
/// <https://easings.net/#easeInBounce>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn bounce_in(t: f32) -> f32 {
|
||||
1.0 - bounce_out(1.0 - t)
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutBounce>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn bounce_out(t: f32) -> f32 {
|
||||
const N1: f32 = 7.5625;
|
||||
const D1: f32 = 2.75;
|
||||
if t < 1.0 / D1 {
|
||||
return N1 * t * t;
|
||||
N1 * t * t
|
||||
} else if t < 2.0 / D1 {
|
||||
return N1 * (t - 1.5 / D1).powi(2) + 0.75;
|
||||
N1.mul_add((t - 1.5 / D1).powi(2), 0.75)
|
||||
} else if t < 2.5 / D1 {
|
||||
return N1 * (t - 2.25 / D1).powi(2) + 0.9375;
|
||||
N1.mul_add((t - 2.25 / D1).powi(2), 0.937_5)
|
||||
} else {
|
||||
return N1 * (t - 2.625 / D1).powi(2) + 0.984375;
|
||||
N1.mul_add((t - 2.625 / D1).powi(2), 0.984_375)
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutBounce>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn bounce_in_out(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
(1.0 - bounce_out(1.0 - 2.0 * t)) / 2.0
|
||||
(1.0 - bounce_out(2.0f32.mul_add(-t, 1.0))) / 2.0
|
||||
} else {
|
||||
(1.0 + bounce_out(2.0 * t - 1.0)) / 2.0
|
||||
f32::midpoint(1.0, bounce_out(2.0f32.mul_add(t, -1.0)))
|
||||
}
|
||||
}
|
||||
|
||||
+16
-4
@@ -1,18 +1,30 @@
|
||||
/// <https://easings.net/#easeInCirc>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn circ_in(t: f32) -> f32 {
|
||||
1.0 - (1.0 - t.powi(2)).sqrt()
|
||||
1.0 - t.mul_add(-t, 1.0).sqrt()
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutCirc>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn circ_out(t: f32) -> f32 {
|
||||
(1.0 - (t - 1.0).powi(2)).sqrt()
|
||||
(t - 1.0).mul_add(-(t - 1.0), 1.0).sqrt()
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutCirc>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn circ_in_out(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
(1.0 - (1.0 - (2.0 * t).powi(2)).sqrt()) / 2.0
|
||||
(1.0 - (2.0 * t).mul_add(-(2.0 * t), 1.0).sqrt()) / 2.0
|
||||
} else {
|
||||
((1.0 - (-2.0 * t + 2.0).powi(2)).sqrt() + 1.0) / 2.0
|
||||
f32::midpoint(
|
||||
(-2.0f32)
|
||||
.mul_add(t, 2.0)
|
||||
.mul_add(-(-2.0f32).mul_add(t, 2.0), 1.0)
|
||||
.sqrt(),
|
||||
1.0,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -1,18 +1,24 @@
|
||||
/// <https://easings.net/#easeInCubic>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn cubic_in(t: f32) -> f32 {
|
||||
t * t * t
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutCubic>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn cubic_out(t: f32) -> f32 {
|
||||
1.0 - (1.0 - t).powi(3)
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutCubic>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn cubic_in_out(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
4.0 * t * t * t
|
||||
} else {
|
||||
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
|
||||
1.0 - (-2.0f32).mul_add(t, 2.0).powi(3) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
+12
-4
@@ -4,36 +4,44 @@ const C4: f32 = (2.0 * PI) / 3.0;
|
||||
const C5: f32 = (2.0 * PI) / 4.5;
|
||||
|
||||
/// <https://easings.net/#easeInElastic>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn elastic_in(t: f32) -> f32 {
|
||||
if t <= 0.0 {
|
||||
0.0
|
||||
} else if 1.0 <= t {
|
||||
1.0
|
||||
} else {
|
||||
-2f32.powf(10.0 * t - 10.0) * ((t * 10.0 - 10.75) * C4).sin()
|
||||
-10.0f32.mul_add(t, -10.0).exp2() * (t.mul_add(10.0, -10.75) * C4).sin()
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutElastic>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn elastic_out(t: f32) -> f32 {
|
||||
if t <= 0.0 {
|
||||
0.0
|
||||
} else if 1.0 <= t {
|
||||
1.0
|
||||
} else {
|
||||
2f32.powf(-10.0 * t) * ((t * 10.0 - 0.75) * C4).sin() + 1.0
|
||||
(-10.0 * t)
|
||||
.exp2()
|
||||
.mul_add((t.mul_add(10.0, -0.75) * C4).sin(), 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutElastic>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn elastic_in_out(t: f32) -> f32 {
|
||||
if t <= 0.0 {
|
||||
0.0
|
||||
} else if 1.0 <= t {
|
||||
1.0
|
||||
} else if t < 0.5 {
|
||||
-(2f32.powf(20.0 * t - 10.0) * ((20.0 * t - 11.125) * C5).sin()) / 2.0
|
||||
-(20.0f32.mul_add(t, -10.0).exp2() * (20.0f32.mul_add(t, -11.125) * C5).sin()) / 2.0
|
||||
} else {
|
||||
(2f32.powf(-20.0 * t + 10.0) * ((20.0 * t - 11.125) * C5).sin()) / 2.0 + 1.0
|
||||
((-20.0f32).mul_add(t, 10.0).exp2() * (20.0f32.mul_add(t, -11.125) * C5).sin()) / 2.0 + 1.0
|
||||
}
|
||||
}
|
||||
|
||||
+10
-4
@@ -1,30 +1,36 @@
|
||||
/// <https://easings.net/#easeInExpo>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn expo_in(t: f32) -> f32 {
|
||||
if t <= 0.0 {
|
||||
0.0
|
||||
} else {
|
||||
2f32.powf(10.0 * t - 10.0)
|
||||
10.0f32.mul_add(t, -10.0).exp2()
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutExpo>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn expo_out(t: f32) -> f32 {
|
||||
if 1.0 <= t {
|
||||
1.0
|
||||
} else {
|
||||
1.0 - 2f32.powf(-10.0 * t)
|
||||
1.0 - (-10.0 * t).exp2()
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutExpo>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn expo_in_out(t: f32) -> f32 {
|
||||
if t <= 0.0 {
|
||||
0.0
|
||||
} else if 1.0 <= t {
|
||||
1.0
|
||||
} else if t < 0.5 {
|
||||
2f32.powf(20.0 * t - 10.0) / 2.0
|
||||
20.0f32.mul_add(t, -10.0).exp2() / 2.0
|
||||
} else {
|
||||
(2.0 - 2f32.powf(-20.0 * t + 10.0)) / 2.0
|
||||
(2.0 - (-20.0f32).mul_add(t, 10.0).exp2()) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
+10
-5
@@ -20,6 +20,9 @@
|
||||
//! assert_eq!(easing(1.0), 1.0);
|
||||
//! ```
|
||||
|
||||
#![warn(clippy::pedantic, clippy::nursery)]
|
||||
#![allow(clippy::missing_const_for_fn)]
|
||||
|
||||
mod back;
|
||||
mod bounce;
|
||||
mod circ;
|
||||
@@ -42,11 +45,15 @@ pub use quart::*;
|
||||
pub use quint::*;
|
||||
pub use sine::*;
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn linear(t: f32) -> f32 {
|
||||
t
|
||||
}
|
||||
|
||||
/// A linear easing that goes from `1.0` to `0.0`.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn reverse(t: f32) -> f32 {
|
||||
1.0 - t
|
||||
}
|
||||
@@ -61,10 +68,8 @@ pub fn reverse(t: f32) -> f32 {
|
||||
/// let descending = cubic_in(roundtrip(0.75));
|
||||
/// assert!((ascending - descending).abs() < 0.001);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn roundtrip(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
t * 2.0
|
||||
} else {
|
||||
(1.0 - t) * 2.0
|
||||
}
|
||||
if t < 0.5 { t * 2.0 } else { (1.0 - t) * 2.0 }
|
||||
}
|
||||
|
||||
+8
-2
@@ -1,18 +1,24 @@
|
||||
/// <https://easings.net/#easeInQuad>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quad_in(t: f32) -> f32 {
|
||||
t * t
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutQuad>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quad_out(t: f32) -> f32 {
|
||||
1.0 - (1.0 - t).powi(2)
|
||||
(1.0 - t).mul_add(-(1.0 - t), 1.0)
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutQuad>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quad_in_out(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
2.0 * t * t
|
||||
} else {
|
||||
1.0 - (-2.0 * t + 2.0).powi(2) / 2.0
|
||||
1.0 - (-2.0f32).mul_add(t, 2.0).powi(2) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -1,18 +1,24 @@
|
||||
/// <https://easings.net/#easeInQuart>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quart_in(t: f32) -> f32 {
|
||||
t * t * t * t
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutQuart>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quart_out(t: f32) -> f32 {
|
||||
1.0 - (1.0 - t).powi(4)
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutQuart>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quart_in_out(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
8.0 * t * t * t * t
|
||||
} else {
|
||||
1.0 - (-2.0 * t + 2.0).powi(4) / 2.0
|
||||
1.0 - (-2.0f32).mul_add(t, 2.0).powi(4) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -1,18 +1,24 @@
|
||||
/// <https://easings.net/#easeInQuint>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quint_in(t: f32) -> f32 {
|
||||
t * t * t * t
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutQuint>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quint_out(t: f32) -> f32 {
|
||||
1.0 - (1.0 - t).powi(5)
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutQuint>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn quint_in_out(t: f32) -> f32 {
|
||||
if t < 0.5 {
|
||||
16.0 * t * t * t * t * t
|
||||
} else {
|
||||
1.0 - (-2.0 * t + 2.0).powi(5) / 2.0
|
||||
1.0 - (-2.0f32).mul_add(t, 2.0).powi(5) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
use ::std::f32::consts::PI;
|
||||
|
||||
/// <https://easings.net/#easeInSine>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn sine_in(t: f32) -> f32 {
|
||||
1.0 - (t * PI / 2.0).cos()
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeOutSine>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn sine_out(t: f32) -> f32 {
|
||||
(t * PI / 2.0).sin()
|
||||
}
|
||||
|
||||
/// <https://easings.net/#easeInOutSine>
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn sine_in_out(t: f32) -> f32 {
|
||||
-((PI * t).cos() - 1.0) / 2.0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user