Skip to main content

math_code/
math_code.rs

1use motion_canvas_rs::prelude::*;
2use std::time::Duration;
3
4fn main() {
5    let mut project = Project::default()
6        .with_fps(120)
7        .with_title("Math Code")
8        .close_on_finish();
9
10    let triangle = Polygon::default()
11        .with_position(Vec2::ZERO)
12        .with_anchor(Vec2::new(-1.0, -1.0))
13        .with_points(vec![
14            Vec2::new(0.0, 0.0),
15            Vec2::new(0.0, 200.0),
16            Vec2::new(200.0, 200.0),
17        ])
18        .with_fill(Color::rgb8(0x68, 0xab, 0xdf));
19
20    let lines = vec![
21        Line::default()
22            .with_start(Vec2::new(0.0, 0.0))
23            .with_end(Vec2::new(0.0, 200.0))
24            .with_stroke(Color::WHITE, 10.0),
25        Line::default()
26            .with_start(Vec2::new(0.0, 0.0))
27            .with_end(Vec2::new(200.0, 200.0))
28            .with_stroke(Color::WHITE, 10.0),
29        Line::default()
30            .with_start(Vec2::new(0.0, 200.0))
31            .with_end(Vec2::new(200.0, 200.0))
32            .with_stroke(Color::WHITE, 10.0),
33    ];
34
35    let triangle_line_group = GroupNode::default()
36        .with_nodes(vec![
37            triangle.clone_node(),
38            lines[0].clone_node(),
39            lines[1].clone_node(),
40            lines[2].clone_node(),
41        ])
42        .with_position(Vec2::new(400.0, 200.0))
43        .with_scale(0.0);
44
45    let pytagorean_theorem = MathNode::default()
46        .with_position(Vec2::new(150.0, 150.0))
47        .with_equation("a^2 + b^2 = c^2")
48        .with_font_size(10.0)
49        .with_fill(Color::GRAY)
50        .with_opacity(0.0);
51
52    let text_a = TextNode::default()
53        .with_text("a")
54        .with_fill(Color::GRAY)
55        .with_opacity(0.0)
56        .with_scale(0.7)
57        .with_position(Vec2::new(370.0, 300.0));
58
59    let text_b = TextNode::default()
60        .with_text("b")
61        .with_fill(Color::GRAY)
62        .with_opacity(0.0)
63        .with_scale(0.7)
64        .with_position(Vec2::new(500.0, 430.0));
65
66    let text_c = TextNode::default()
67        .with_text("c")
68        .with_fill(Color::GRAY)
69        .with_opacity(0.0)
70        .with_scale(0.7)
71        .with_position(Vec2::new(510.0, 260.0));
72
73    project.scene.add(Box::new(triangle_line_group.clone()));
74    project.scene.add(Box::new(pytagorean_theorem.clone()));
75    project.scene.add(Box::new(text_a.clone()));
76    project.scene.add(Box::new(text_b.clone()));
77    project.scene.add(Box::new(text_c.clone()));
78
79    project.scene.video_timeline.add(all![
80        all![triangle_line_group
81            .scale
82            .to(Vec2::ONE, Duration::from_secs(1)),],
83        all![
84            pytagorean_theorem.opacity.to(1.0, Duration::from_secs(1)),
85            pytagorean_theorem
86                .font_size
87                .to(32.0, Duration::from_secs(1)),
88        ],
89        sequence![
90            Duration::from_millis(300),
91            text_a.scale.to(Vec2::new(1.0, 1.0), Duration::from_secs(1)),
92            text_b.scale.to(Vec2::new(1.0, 1.0), Duration::from_secs(1)),
93            text_c.scale.to(Vec2::new(1.0, 1.0), Duration::from_secs(1)),
94            text_a.opacity.to(1.0, Duration::from_secs(1)),
95            text_b.opacity.to(1.0, Duration::from_secs(1)),
96            text_c.opacity.to(1.0, Duration::from_secs(1))
97        ]
98    ]);
99
100    project.show().expect("Failed to render");
101}