Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/tools/build-easefunction-graphs/src/main.rs
6595 views
1
//! Generates graphs for the `EaseFunction` docs.
2
3
#![expect(clippy::print_stdout, reason = "Allowed in tools.")]
4
5
use std::path::PathBuf;
6
7
use bevy_math::curve::{CurveExt, EaseFunction, EasingCurve, JumpAt};
8
use svg::{
9
node::element::{self, path::Data},
10
Document,
11
};
12
13
fn main() {
14
let root_dir = PathBuf::from(
15
std::env::var("CARGO_MANIFEST_DIR")
16
.expect("Please run via cargo or set CARGO_MANIFEST_DIR"),
17
);
18
let directory = root_dir
19
.join("../../crates/bevy_math/images/easefunction")
20
.canonicalize()
21
.unwrap();
22
23
for function in [
24
EaseFunction::SineIn,
25
EaseFunction::SineOut,
26
EaseFunction::SineInOut,
27
EaseFunction::QuadraticIn,
28
EaseFunction::QuadraticOut,
29
EaseFunction::QuadraticInOut,
30
EaseFunction::CubicIn,
31
EaseFunction::CubicOut,
32
EaseFunction::CubicInOut,
33
EaseFunction::QuarticIn,
34
EaseFunction::QuarticOut,
35
EaseFunction::QuarticInOut,
36
EaseFunction::QuinticIn,
37
EaseFunction::QuinticOut,
38
EaseFunction::QuinticInOut,
39
EaseFunction::SmoothStepIn,
40
EaseFunction::SmoothStepOut,
41
EaseFunction::SmoothStep,
42
EaseFunction::SmootherStepIn,
43
EaseFunction::SmootherStepOut,
44
EaseFunction::SmootherStep,
45
EaseFunction::CircularIn,
46
EaseFunction::CircularOut,
47
EaseFunction::CircularInOut,
48
EaseFunction::ExponentialIn,
49
EaseFunction::ExponentialOut,
50
EaseFunction::ExponentialInOut,
51
EaseFunction::ElasticIn,
52
EaseFunction::ElasticOut,
53
EaseFunction::ElasticInOut,
54
EaseFunction::BackIn,
55
EaseFunction::BackOut,
56
EaseFunction::BackInOut,
57
EaseFunction::BounceIn,
58
EaseFunction::BounceOut,
59
EaseFunction::BounceInOut,
60
EaseFunction::Linear,
61
EaseFunction::Steps(4, JumpAt::Start),
62
EaseFunction::Steps(4, JumpAt::End),
63
EaseFunction::Steps(4, JumpAt::None),
64
EaseFunction::Steps(4, JumpAt::Both),
65
EaseFunction::Elastic(50.0),
66
] {
67
let curve = EasingCurve::new(0.0, 1.0, function);
68
let samples = curve
69
.map(|y| {
70
// Fit into svg coordinate system
71
1. - y
72
})
73
.graph()
74
.samples(100)
75
.unwrap()
76
.collect::<Vec<_>>();
77
78
// Curve can go out past endpoints
79
let mut min = 0.0f32;
80
let mut max = 1.0f32;
81
for &(_, y) in &samples {
82
min = min.min(y);
83
max = max.max(y);
84
}
85
86
let graph = element::Polyline::new()
87
.set("points", samples)
88
.set("fill", "none")
89
.set("stroke", "red")
90
.set("stroke-width", 0.04);
91
92
let guides = element::Path::new()
93
.set("fill", "none")
94
.set("stroke", "var(--main-color)")
95
.set("stroke-width", 0.02)
96
.set("d", {
97
// Interval
98
let mut data = Data::new()
99
.move_to((0, 0))
100
.line_to((0, 1))
101
.move_to((1, 0))
102
.line_to((1, 1));
103
// Dotted lines y=0 | y=1
104
for y in 0..=1 {
105
data = data.move_to((0, y));
106
for _ in 0..5 {
107
data = data.move_by((0.1, 0.)).line_by((0.1, 0.));
108
}
109
}
110
data
111
});
112
113
let opt_tag = match function {
114
EaseFunction::Steps(_n, jump_at) => format!("{jump_at:?}"),
115
_ => String::new(),
116
};
117
118
let name = format!("{opt_tag}{function:?}");
119
let tooltip = element::Title::new(&name);
120
121
const MARGIN: f32 = 0.04;
122
let document = Document::new()
123
.set("width", "6em")
124
.set(
125
"viewBox",
126
(
127
-MARGIN,
128
min - MARGIN,
129
1. + 2. * MARGIN,
130
max - min + 2. * MARGIN,
131
),
132
)
133
.add(tooltip)
134
.add(guides)
135
.add(graph);
136
137
let file_path = directory
138
.join(name.split('(').next().unwrap())
139
.with_extension("svg");
140
println!("saving {file_path:?}");
141
svg::save(file_path, &document).unwrap();
142
}
143
}
144
145