Skip to main content

code_advanced/
code_advanced.rs

1use motion_canvas_rs::prelude::*;
2use std::time::Duration;
3
4fn main() {
5    let mut project = Project::default()
6        .with_title("Code Advanced")
7        .close_on_finish();
8
9    let code = CodeNode::default()
10        .with_position(Vec2::new(50.0, 50.0))
11        .with_code(
12            r#"fn main() {
13    println!("Hello");
14}"#,
15        )
16        .with_language("rust")
17        .with_font_size(32.0)
18        .with_dim_opacity(0.1);
19
20    project.scene.add(Box::new(code.clone()));
21
22    project.scene.video_timeline.add(sequence![
23        Duration::from_secs(1),
24        // 1. Select line 2 (println) - using 1-based index string
25        code.select_string("2", Duration::from_millis(300)),
26        // 2. Select range 1-2
27        code.select_string("1-2", Duration::from_millis(300)),
28        // 3. Append a comment
29        code.append("\n// Done!", Duration::from_millis(300)),
30        // 3. Reset selection
31        code.select_lines(vec![], Duration::from_millis(300)),
32        // 4. Prepend a header (Now natively lazy, no wrapper needed!)
33        code.prepend("// My Script\n", Duration::from_millis(300)),
34    ]);
35
36    project.show().expect("Failed to render");
37}