Preview App text_item 示例输出

scenes.rs(共享场景与项目装配)

use std::f64::consts::TAU;

use ranim::utils::rate_functions::smooth;
use ranim::{color::palettes::manim, glam::DVec3, prelude::*};
use ranim_anims::{
    creation::{CreationAnim, WritingAnim},
    rotating::RotatingAnim,
};
use ranim_items::vitem::{VItem, text::TextItem};

#[scene]
#[wasm_demo_doc]
#[output(dir = "./output/text_item")]
fn text_item(r: &mut RanimScene) {
    let text = "The quick brown fox jumps over the lazy dog.";

    let text = TextItem::new(text, 0.5);
    let text_box = text
        .text_box()
        .with(|item| item.set_stroke_color(manim::RED_C).discard());

    let mut text_sequence = seq![
        Vec::<VItem>::from(text.clone())
            .iter_mut()
            .map(|item| item.write().with_rate_func(smooth))
            .into_lagged(0.1)
    ];
    text_sequence
        .hold(3.0)
        .push(
            Vec::<VItem>::from(text.clone())
                .rotating(TAU * 4.0, DVec3::Z)
                .with_duration(4.0)
                .with_rate_func(smooth),
        )
        .hold(1.0)
        .push(
            Vec::<VItem>::from(text.clone())
                .iter_mut()
                .map(|item| item.unwrite().with_rate_func(smooth))
                .into_lagged(0.1),
        )
        .hold(1.0);

    let mut box_sequence = AnimSequence::new();
    box_sequence
        .forward(1.0)
        .push(
            VItem::from(text_box.clone())
                .create()
                .with_rate_func(smooth),
        )
        .push(VItem::from(text_box).uncreate().with_rate_func(smooth));

    let total_secs = text_sequence.cursor_sec().max(box_sequence.cursor_sec());
    r.play(CameraFrame::default().show().with_duration(total_secs));
    r.play(stack![text_sequence, box_sequence]);

    r.insert_time_mark(
        total_secs / 2.0,
        TimeMark::Capture("preview.png".to_string()),
    );
}

/// Register the scenes provided by this example.
pub struct ScenesPlugin;

impl ranim::Plugin for ScenesPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        app.add_scenes([text_item::scene()]);
    }
}

main.rs(平台入口)

mod scenes;

#[cfg(not(target_arch = "wasm32"))]
fn main() {
    #[cfg(feature = "cmd-render")]
    {
        use ranim::prelude::*;
        let mut app = App::new();
        app.add_plugins(scenes::ScenesPlugin);
        run_cli(app.build());
    }
    #[cfg(not(feature = "cmd-render"))]
    compile_error!("native examples require the `cmd-render` feature (enabled by default)");
}

// The WASM module is driven by JavaScript through app().
#[cfg(target_arch = "wasm32")]
fn main() {}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn app() -> ranim::wasm::WasmApp {
    use ranim::prelude::*;

    let mut app = App::new();
    app.add_plugins(scenes::ScenesPlugin);
    ranim::wasm::WasmApp::new(app.build())
}