Basic Example

This example demonstrates the basic usage of ranim.

https://github.com/user-attachments/assets/dadab1ab-65e4-4cd3-bda4-b2ef97c61b0b

basic.mp4

1use env_logger::Env;
2use glam::DVec3;
3use ranim::animation::creation::WritingAnimSchedule;
4use ranim::animation::fading::FadingAnimSchedule;
5use ranim::components::ScaleHint;
6use ranim::items::group::Group;
7use ranim::items::svg_item::SvgItem;
8use ranim::items::vitem::VItem;
9use ranim::timeline::TimeMark;
10use ranim::utils::rate_functions::linear;
11use ranim::{prelude::*, typst_svg};
12
13const SVG: &str = include_str!("../../assets/Ghostscript_Tiger.svg");
14
15#[scene]
16struct BasicScene;
17
18impl 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)); // At the same time, the svg fade in
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
71fn 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