Skip to main content

camera_demo/
camera_demo.rs

1use motion_canvas_rs::prelude::*;
2use std::time::Duration;
3
4fn main() {
5    let mut project = Project::default()
6        .with_title("Camera Demo")
7        .with_background(Color::rgb8(0x1a, 0x1a, 0x1a))
8        .close_on_finish();
9
10    // Create a grid of circles to move the camera around
11    let mut nodes: Vec<Box<dyn Node>> = Vec::new();
12
13    for x in -2..=2 {
14        for y in -2..=2 {
15            let color = if (x + y) % 2 == 0 {
16                Color::rgb8(0x34, 0x98, 0xdb) // Blue
17            } else {
18                Color::rgb8(0xe7, 0x4c, 0x3c) // Red
19            };
20
21            nodes.push(Box::new(
22                Circle::default()
23                    .with_position(Vec2::new(x as f32 * 200.0, y as f32 * 200.0))
24                    .with_radius(40.0)
25                    .with_fill(color),
26            ));
27        }
28    }
29
30    // Create the camera and add our nodes to it
31    let camera = CameraNode::new(nodes)
32        .with_position(Vec2::new(0.0, 0.0))
33        .with_zoom(1.0);
34
35    project.scene.add(Box::new(camera.clone()));
36
37    // Add HUD text (NOT in camera, so it stays fixed)
38    project.scene.add(Box::new(
39        TextNode::default()
40            .with_position(Vec2::new(400.0, 50.0))
41            .with_text("Camera Control")
42            .with_font_size(60.0)
43            .with_fill(Color::WHITE)
44            .with_anchor(Vec2::ZERO),
45    ));
46
47    // Animation: Pan, Zoom, and Rotate the camera
48    project.scene.video_timeline.add(loop_anim!(
49        chain![
50            wait(Duration::from_secs(1)),
51            // 1. Pan to the right
52            camera
53                .position
54                .to(Vec2::new(200.0, 0.0), Duration::from_secs(1)),
55            // 2. Zoom in
56            camera.zoom.to(2.0, Duration::from_secs(1)),
57            // 3. Pan down and rotate
58            all![
59                camera
60                    .position
61                    .to(Vec2::new(200.0, 200.0), Duration::from_secs(1)),
62                camera
63                    .rotation
64                    .to(std::f32::consts::PI / 4.0, Duration::from_secs(1)),
65            ],
66            // 4. Zoom out and reset
67            all![
68                camera.position.to(Vec2::ZERO, Duration::from_secs(1)),
69                camera.zoom.to(0.5, Duration::from_secs(1)),
70                camera.rotation.to(0.0, Duration::from_secs(1)),
71            ],
72            // 5. Back to normal
73            camera.zoom.to(1.0, Duration::from_secs(1)),
74        ],
75        None,
76    ));
77
78    project.show().expect("Failed to render");
79}