Skip to main content

polygon/
polygon.rs

1use motion_canvas_rs::prelude::*;
2use std::time::Duration;
3
4fn main() {
5    let mut project = Project::default().with_title("Polygon").close_on_finish();
6
7    // Create a regular pentagon
8    let pentagon = Polygon::regular(5, 100.0)
9        .with_fill(Color::rgb8(0xe1, 0x32, 0x38)) // Red
10        .with_position(Vec2::new(200.0, 300.0))
11        .with_scale(0.0);
12
13    // Create a custom triangle
14    let triangle = Polygon::default()
15        .with_position(Vec2::new(500.0, 300.0))
16        .with_points(vec![
17            Vec2::new(0.0, -100.0),
18            Vec2::new(100.0, 100.0),
19            Vec2::new(-100.0, 100.0),
20        ])
21        .with_fill(Color::rgb8(0x68, 0xab, 0xdf)) // Blue
22        .with_stroke(Color::WHITE, 4.0);
23
24    project.scene.add(Box::new(pentagon.clone()));
25    project.scene.add(Box::new(triangle.clone()));
26
27    // Animate rotation and opacity
28    project.scene.video_timeline.add(all![
29        chain![
30            pentagon.scale.to(Vec2::ONE, Duration::from_secs(1)),
31            pentagon
32                .rotation
33                .to(std::f32::consts::PI, Duration::from_secs(2)),
34            pentagon.scale.to(-Vec2::ONE, Duration::from_secs(1)),
35        ],
36        chain![
37            triangle.opacity.to(1.0, Duration::from_secs(1)),
38            triangle
39                .position
40                .to(Vec2::new(500.0, 300.0), Duration::from_secs(1)),
41            triangle
42                .rotation
43                .to(360.0_f32.to_radians(), Duration::from_secs(1))
44        ],
45    ]);
46
47    project.show().expect("Failed to render");
48}