//! Probe: a single-scene fframes video. //! //! `render_frame` returns an SVG tree for every frame; `svgr!` is SVG with `{rust //! expressions}` in attributes and text. Check your work with the CLI in `main.rs` //! (`cargo run --release -- --help`). use fframes::{ AudioMap, Color, Duration, FFramesContext, FontQuery, Frame, Svgr, Transform, Video, animation::Easing, include_media_dir, }; use std::sync::OnceLock; // Every file of `media/` is embedded into the binary. Fonts are used by family name. include_media_dir!(pub struct ProbeMedia, "media"); pub const WIDTH: usize = 1920; pub const HEIGHT: usize = 1080; /// 1.0 at 1080p, landscape or portrait: multiply sizes and distances by it. const UNIT: f32 = (if WIDTH < HEIGHT { WIDTH } else { HEIGHT }) as f32 / 1080.0; const FONT: &str = "DM Sans"; /// The only weight in `media/`; add font files for more. const WEIGHT: u16 = 500; const INK: &str = "#0f172a"; const MUTED: &str = "#475569"; const ACCENT: &str = "#6366f1"; pub struct ProbeVideo<'a> { pub media: &'a ProbeMedia, greeting: String, /// Font size of the greeting, measured once so it fits the width. greeting_size: OnceLock, } impl<'a> ProbeVideo<'a> { pub fn new(media: &'a ProbeMedia, title: &str) -> Self { Self { media, greeting: format!("Hello, {title}"), greeting_size: OnceLock::new(), } } /// Largest size up to 150 px (at 1080p) at which the greeting fits 80% of the width. fn greeting_size<'b>(&'b self, frame: &mut Frame, ctx: &FFramesContext<'b, '_>) -> usize { if let Some(size) = self.greeting_size.get() { return *size; } let max_width = WIDTH as f32 * 0.8; let mut size = (150. * UNIT) as usize; loop { let font = FontQuery { family: FONT, size, weight: WEIGHT, ..Default::default() }; match frame.text_width(ctx, font, &self.greeting) { Some(width) if width as f32 > max_width && size > 24 => size -= 4, // Only remember real measurements (fonts can be missing in the web editor). Some(_) => return *self.greeting_size.get_or_init(|| size), None => return size, } } } } impl std::fmt::Debug for ProbeVideo<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ProbeVideo").field("greeting", &self.greeting).finish() } } impl Video for ProbeVideo<'_> { const FPS: usize = 30; const WIDTH: usize = WIDTH; const HEIGHT: usize = HEIGHT; const BACKGROUND_COLOR: Color = Color::WHITE; fn duration(&self) -> Duration<'_> { Duration::Seconds(60.0) } fn audio(&self) -> AudioMap<'_> { // Put audio files into `media/` and place them on the timeline, e.g.: // // use fframes::{AudioTrack, AudioTimestamp::*}; // AudioMap::from([AudioTrack::new("music.mp3", Second(0.)..Eof).gain_db(-12.).fade_out(1.5)]) AudioMap::none() } fn render_frame<'a>(&'a self, mut frame: Frame, ctx: &FFramesContext<'a, '_>) -> Svgr<'a> { let spring = Easing::Spring { mass: 1.0, stiffness: 180.0, damping: 20.0 }; let size = self.greeting_size(&mut frame, ctx); // The greeting springs up and fades in, the subtitle follows, everything fades out. let rise = frame.animate(&fframes::timeline!(at 0.2, animate 80.0_f32 => 0.0, spring)); let fade_in = frame.animate(&fframes::timeline!(at 0.2 => 0.6, animate 0.0_f32 => 1.0, Easing::EaseOut)); let subtitle = frame.animate(&fframes::timeline!(at 0.9 => 1.4, animate 0.0_f32 => 1.0, Easing::EaseOut)); let fade_out = frame.animate(&fframes::timeline!(at 59.4 => 59.9, animate 1.0_f32 => 0.0, Easing::EaseIn)); // SVG skips zero-sized rects, so the underline grows from a hairline. let underline = frame.animate(&fframes::timeline!(at 0.6 => 1.3, animate 0.5_f32 => 360.0, Easing::EaseInOut)) * UNIT; // A slow drift keeps the background alive while the text holds. let drift = frame.animate(&fframes::timeline!(at 0.0 => 60.0, animate 0.0_f32 => 160.0, Easing::EaseInOut)) * UNIT; let (cx, cy) = (WIDTH as f32 / 2., HEIGHT as f32 / 2.); fframes::svgr!( {self.greeting.as_str()} "made with fframes" {format!("frame {} / {:.2}s", frame.index, frame.seconds())} ) } }