mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-22 16:06:38 +00:00
Add animation
This commit is contained in:
parent
4a80f06ce6
commit
0ae10b2927
51
src/animation/circle.rs
Normal file
51
src/animation/circle.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use crate::animation::Animation;
|
||||||
|
use crate::vec::Vector;
|
||||||
|
|
||||||
|
const THICKNESS: f32 = 0.2;
|
||||||
|
const FINAL_RADIUS: f32 = 1.0 + THICKNESS * 2.0;
|
||||||
|
|
||||||
|
pub struct CircleAnimation {
|
||||||
|
center: Vector,
|
||||||
|
thickness: f32,
|
||||||
|
final_radius: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CircleAnimation {
|
||||||
|
pub fn new(size: Vector) -> Self {
|
||||||
|
let center = size.center();
|
||||||
|
let distance = center.length();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
center,
|
||||||
|
thickness: distance * THICKNESS,
|
||||||
|
final_radius: distance * FINAL_RADIUS,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Animation for CircleAnimation {
|
||||||
|
fn sample(&self, step: f32, pos: Vector) -> f32 {
|
||||||
|
let radius = self.final_radius * step - self.thickness;
|
||||||
|
let distance = (pos - self.center).length();
|
||||||
|
|
||||||
|
(distance - radius) / self.thickness
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample() {
|
||||||
|
let anim = CircleAnimation::new(Vector::new(10.0, 20.0));
|
||||||
|
|
||||||
|
let sample_1 = anim.sample(0.5, Vector::new(17.0, 5.0));
|
||||||
|
let sample_2 = anim.sample(0.8, Vector::new(11.0, 8.0));
|
||||||
|
let sample_3 = anim.sample(0.2, Vector::new(7.0, 10.0));
|
||||||
|
|
||||||
|
assert!(3.3 < sample_1 && sample_1 < 3.4);
|
||||||
|
assert!(-1.8 < sample_2 && sample_2 < -1.7);
|
||||||
|
assert!(0.4 < sample_3 && sample_3 < 0.5);
|
||||||
|
}
|
||||||
|
}
|
7
src/animation/mod.rs
Normal file
7
src/animation/mod.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
mod circle;
|
||||||
|
|
||||||
|
use crate::vec::Vector;
|
||||||
|
|
||||||
|
pub trait Animation {
|
||||||
|
fn sample(&self, step: f32, pos: Vector) -> f32;
|
||||||
|
}
|
@ -8,6 +8,7 @@ mod fill;
|
|||||||
mod vec;
|
mod vec;
|
||||||
mod array;
|
mod array;
|
||||||
mod surface;
|
mod surface;
|
||||||
|
mod animation;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[clap(author = "Rico Riedel", version = "0.1.0", about = "Wipe your terminal with a random animation.")]
|
#[clap(author = "Rico Riedel", version = "0.1.0", about = "Wipe your terminal with a random animation.")]
|
||||||
|
18
src/vec.rs
18
src/vec.rs
@ -1,3 +1,5 @@
|
|||||||
|
use std::ops::Sub;
|
||||||
|
|
||||||
/// A vector with a x and y axis.
|
/// A vector with a x and y axis.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct Vector {
|
pub struct Vector {
|
||||||
@ -12,6 +14,14 @@ impl Vector {
|
|||||||
Self { x, y }
|
Self { x, y }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn center(self) -> Self {
|
||||||
|
Self::new(self.x / 2.0, self.y / 2.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn length(self) -> f32 {
|
||||||
|
(self.x * self.x + self.y * self.y).sqrt()
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a vector with the on screen coordinates based on the terminal coordinates.
|
/// Creates a vector with the on screen coordinates based on the terminal coordinates.
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `x`: The x axis of the terminal character.
|
/// * `x`: The x axis of the terminal character.
|
||||||
@ -21,6 +31,14 @@ impl Vector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Sub for Vector {
|
||||||
|
type Output = Vector;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Self) -> Self::Output {
|
||||||
|
Vector::new(self.x - rhs.x, self.y - rhs.y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user