Skip to main content

group_animation/
group_animation.rs

1use motion_canvas_rs::prelude::*;
2use std::time::Duration;
3
4fn main() {
5    let mut project = Project::default()
6        .with_title("Group Animation")
7        .close_on_finish();
8
9    // Create some child nodes
10    let rect = Rect::default()
11        .with_position(Vec2::new(0.0, 0.0))
12        .with_size(Vec2::new(100.0, 100.0))
13        .with_fill(Color::rgba8(100, 100, 255, 255))
14        .with_radius(10.0);
15
16    let circle1 = Circle::default()
17        .with_position(Vec2::new(-40.0, -40.0))
18        .with_radius(20.0)
19        .with_fill(Color::rgba8(255, 100, 100, 255));
20
21    let circle2 = Circle::default()
22        .with_position(Vec2::new(40.0, 40.0))
23        .with_radius(20.0)
24        .with_fill(Color::rgba8(100, 255, 100, 255));
25
26    // Create a GroupNode holding them
27    let group = GroupNode::default()
28        .with_nodes(vec![
29            Box::new(rect.clone()),
30            Box::new(circle1.clone()),
31            Box::new(circle2.clone()),
32        ])
33        .with_position(Vec2::new(400.0, 300.0));
34
35    // We must add the group to the scene's nodes to render it
36    project.scene.add(Box::new(group.clone()));
37
38    // Define animations and add them to the timeline
39    project.scene.video_timeline.add(chain![
40        // 1. Move the whole group
41        group
42            .position
43            .to(Vec2::new(200.0, 150.0), Duration::from_secs(2)),
44        // 2. Rotate the group
45        group
46            .rotation
47            .to(std::f32::consts::PI, Duration::from_secs(2)),
48        // 3. Complex transform (move + scale)
49        all![
50            group
51                .position
52                .to(Vec2::new(400.0, 450.0), Duration::from_secs(2)),
53            group.scale.to(Vec2::splat(2.0), Duration::from_secs(2)),
54            group.opacity.to(0.3, Duration::from_secs(2)),
55        ],
56        // 4. Reset (at the center)
57        all![
58            group
59                .position
60                .to(Vec2::new(400.0, 300.0), Duration::from_secs(1)),
61            group.scale.to(Vec2::ONE, Duration::from_secs(1)),
62            group.rotation.to(0.0, Duration::from_secs(1)),
63            group.opacity.to(1.0, Duration::from_secs(1)),
64        ],
65    ]);
66
67    project.show().expect("Failed to render");
68}