Basic Example⁋
This example demonstrates the basic usage of ranim
.
https://github.com/user-attachments/assets/dadab1ab-65e4-4cd3-bda4-b2ef97c61b0b
basic.mp4
1 | use env_logger::Env;
|
2 | use glam::DVec3;
|
3 | use ranim::animation::creation::WritingAnimSchedule;
|
4 | use ranim::animation::fading::FadingAnimSchedule;
|
5 | use ranim::components::ScaleHint;
|
6 | use ranim::items::group::Group;
|
7 | use ranim::items::svg_item::SvgItem;
|
8 | use ranim::items::vitem::VItem;
|
9 | use ranim::timeline::TimeMark;
|
10 | use ranim::utils::rate_functions::linear;
|
11 | use ranim::{prelude::*, typst_svg};
|
12 |
|
13 | const SVG: &str = include_str!("../../assets/Ghostscript_Tiger.svg");
|
14 |
|
15 | #[scene]
|
16 | struct BasicScene;
|
17 |
|
18 | impl TimelineConstructor for BasicScene {
|
19 | fn construct<'t: 'r, 'r>(
|
20 | self,
|
21 | timeline: &'t RanimTimeline,
|
22 | _camera: &'r mut Rabject<'t, CameraFrame>,
|
23 | ) {
|
24 | timeline.forward(0.2);
|
25 |
|
26 | let mut svg = SvgItem::from_svg(SVG);
|
27 | svg.scale_to(ScaleHint::PorportionalHeight(3.0))
|
28 | .put_center_on(DVec3::Y * 2.0);
|
29 | let mut svg = timeline.insert(svg);
|
30 |
|
31 | let mut text = Group::<VItem>::from_svg(&typst_svg!(
|
32 | r#"
|
33 | #align(center)[
|
34 | #text(18pt)[Ranim]
|
35 |
|
36 | #text(6pt)[Hello 你好]
|
37 | ]
|
38 | "#
|
39 | ));
|
40 | text.scale_to(ScaleHint::PorportionalHeight(2.0))
|
41 | .put_center_on(DVec3::NEG_Y * 2.0);
|
42 |
|
43 | text.iter_mut().for_each(|item| {
|
44 | item.set_fill_opacity(0.8);
|
45 | });
|
46 | let mut text = timeline.insert(text);
|
47 |
|
48 | timeline.play(
|
49 | text.lagged_anim(0.2, |item| item.write())
|
50 | .with_total_duration(3.0)
|
51 | .with_rate_func(linear),
|
52 | );
|
53 | timeline.play(svg.fade_in().with_duration(3.0)); |
54 | timeline.sync();
|
55 | timeline.insert_time_mark(
|
56 | timeline.duration_secs(),
|
57 | TimeMark::Capture("preview.png".to_string()),
|
58 | );
|
59 |
|
60 | timeline.forward(0.5);
|
61 | timeline.play(
|
62 | text.lagged_anim(0.2, |item| item.unwrite())
|
63 | .with_total_duration(3.0)
|
64 | .with_rate_func(linear),
|
65 | );
|
66 | timeline.play(svg.fade_out().with_duration(3.0));
|
67 | timeline.sync();
|
68 | }
|
69 | }
|
70 |
|
71 | fn main() {
|
72 | #[cfg(debug_assertions)]
|
73 | env_logger::Builder::from_env(Env::default().default_filter_or("basic=trace")).init();
|
74 | #[cfg(not(debug_assertions))]
|
75 | env_logger::Builder::from_env(Env::default().default_filter_or("basic=info,ranim=info")).init();
|
76 |
|
77 | render_scene(BasicScene, &AppOptions::default());
|
78 | }
|
79 |
|