Basic Example

This example demonstrates the basic usage of ranim.

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

basic.mp4

1use glam::DVec3;
2use log::LevelFilter;
3use ranim::animation::creation::WritingAnimSchedule;
4use ranim::animation::fading::FadingAnimSchedule;
5use ranim::components::ScaleHint;
6use ranim::items::group::Group;
7use ranim::items::vitem::VItem;
8use ranim::timeline::TimeMark;
9use ranim::utils::rate_functions::linear;
10use ranim::{prelude::*, typst_svg};
11
12const SVG: &str = include_str!("../../assets/Ghostscript_Tiger.svg");
13
14#[scene]
15struct BasicScene;
16
17impl TimelineConstructor for BasicScene {
18 fn construct(self, timeline: &RanimTimeline, _camera: &mut Rabject<CameraFrame>) {
19 timeline.forward(0.2);
20
21 let mut svg = Group::<VItem>::from_svg(SVG);
22 svg.scale_to_with_stroke(ScaleHint::PorportionalHeight(3.0))
23 .put_center_on(DVec3::Y * 2.0);
24 let mut svg = timeline.insert(svg);
25
26 let mut text = Group::<VItem>::from_svg(&typst_svg!(
27 r#"
28 #align(center)[
29 #text(18pt)[Ranim]
30
31 #text(6pt)[Hello 你好]
32 ]
33 "#
34 ));
35 text.scale_to_with_stroke(ScaleHint::PorportionalHeight(2.0))
36 .put_center_on(DVec3::NEG_Y * 2.0);
37
38 text.iter_mut().for_each(|item| {
39 item.set_fill_opacity(0.8);
40 });
41 let mut text = timeline.insert(text);
42
43 timeline.play(
44 text.lagged_anim(0.2, |item| item.write())
45 .with_total_duration(3.0)
46 .with_rate_func(linear),
47 );
48 timeline.play(svg.lagged_anim(0.0, |item| item.fade_in().with_duration(3.0))); // At the same time, the svg fade in
49 timeline.sync();
50 timeline.insert_time_mark(
51 timeline.duration_secs(),
52 TimeMark::Capture("preview.png".to_string()),
53 );
54
55 timeline.forward(0.5);
56 timeline.play(
57 text.lagged_anim(0.2, |item| item.unwrite())
58 .with_total_duration(3.0)
59 .with_rate_func(linear),
60 );
61 timeline.play(svg.lagged_anim(0.0, |item| item.fade_out().with_duration(3.0))); // At the same time, the svg fade in
62 timeline.sync();
63 }
64}
65
66fn main() {
67 #[cfg(debug_assertions)]
68 pretty_env_logger::formatted_timed_builder()
69 .filter(Some("ranim"), LevelFilter::Trace)
70 .init();
71 #[cfg(not(debug_assertions))]
72 pretty_env_logger::formatted_timed_builder()
73 .filter(Some("ranim"), LevelFilter::Info)
74 .init();
75 render_scene(BasicScene, &AppOptions::default());
76}
77