broadsheet 0.3.0

Newspaper-styled 2D animation engine for algorithm & data-structure explainer videos — stateless timeline, deterministic frame recording, built on macroquad
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! The runtime: live preview window with transport controls, or offline
//! recording, both driving the same pure `Timeline::apply(base, t)`.
//!
//! Every frame renders into an offscreen render target at the output
//! resolution; live mode blits it to the window (fit, centred, optional
//! grain), record mode reads the pixels back. Window size never affects
//! recorded output.
//!
//! Live controls: `Space` pause, `←/→` frame step, `,`/`.` ±1 s, `1`–`9`
//! section jump, `F` / `Ctrl+Cmd+F` fullscreen, `R` restart, drag bottom bar to scrub.
//! The HUD is live-only.
//!
//! CLI flags (after `--`):
//! - `--record [dir]`  render offline (default sink: ffmpeg pipe → out.mp4)
//! - `--fps N`         output frame rate (default 60)
//! - `--scale F`       supersampling (default 1.5 recorded → 1080p, 1 live)
//! - `--from S --to S` record a time range (clips for social posts)
//! - `--frames N`      hard frame cap (smoke tests)
//! - `--still S`       export the single frame at time S as PNG and exit
//! - `--alpha`         transparent background, no chrome, PNG sequence
//! - `--png`           force PNG sequence instead of the ffmpeg pipe
//! - `--gif`           pipe frames into out.gif instead of out.mp4
//! - `--grain`         newsprint grain + vignette post-process
//! - `--slideshow`     present live: pause at every `m.slide(..)` boundary,
//!   `Space`/`→` animates to the next slide, `←` snaps back one

use macroquad::prelude::*;

use crate::movie::Movie;
#[cfg(not(target_arch = "wasm32"))]
use crate::record::Recorder;
use crate::render::{self, View};
use crate::style::{self, Fonts};

/// JS interop for web builds. miniquad's `mq_js_bundle.js` exposes these as
/// `wasm_exports.bs_*`, letting the host page drive the stateless timeline
/// (scroll-scrub, play/pause) without any bindgen layer.
#[cfg(target_arch = "wasm32")]
mod web {
    use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
    use std::sync::Mutex;

    // f32s smuggled through AtomicU32 bit patterns; NaN = "no seek pending".
    pub static SEEK_BITS: AtomicU32 = AtomicU32::new(u32::MAX);
    pub static TIME_BITS: AtomicU32 = AtomicU32::new(0);
    pub static DUR_BITS: AtomicU32 = AtomicU32::new(0);
    pub static PAUSED: AtomicBool = AtomicBool::new(true);

    /// The movie's named beat markers, published so the host page can read
    /// them without a bundled markers.json. Strings cross the wasm boundary
    /// a byte at a time (`bs_mark_name_len` + `bs_mark_name_byte`) — clunky,
    /// but it runs once at load and needs no bindgen or allocator handshake.
    pub static MARKS: Mutex<Vec<(f32, String)>> = Mutex::new(Vec::new());

    #[no_mangle]
    pub extern "C" fn bs_mark_count() -> u32 {
        MARKS.lock().unwrap().len() as u32
    }

    #[no_mangle]
    pub extern "C" fn bs_mark_time(i: u32) -> f32 {
        MARKS
            .lock()
            .unwrap()
            .get(i as usize)
            .map_or(f32::NAN, |(t, _)| *t)
    }

    #[no_mangle]
    pub extern "C" fn bs_mark_name_len(i: u32) -> u32 {
        MARKS
            .lock()
            .unwrap()
            .get(i as usize)
            .map_or(0, |(_, n)| n.len() as u32)
    }

    #[no_mangle]
    pub extern "C" fn bs_mark_name_byte(i: u32, j: u32) -> u32 {
        MARKS
            .lock()
            .unwrap()
            .get(i as usize)
            .and_then(|(_, n)| n.as_bytes().get(j as usize).copied())
            .map_or(0, u32::from)
    }

    pub fn take_seek() -> Option<f32> {
        let t = f32::from_bits(SEEK_BITS.swap(u32::MAX, Ordering::Relaxed));
        (!t.is_nan()).then_some(t)
    }

    #[no_mangle]
    pub extern "C" fn bs_seek(t: f32) {
        SEEK_BITS.store(t.to_bits(), Ordering::Relaxed);
    }

    #[no_mangle]
    pub extern "C" fn bs_set_paused(p: i32) {
        PAUSED.store(p != 0, Ordering::Relaxed);
    }

    #[no_mangle]
    pub extern "C" fn bs_duration() -> f32 {
        f32::from_bits(DUR_BITS.load(Ordering::Relaxed))
    }

    #[no_mangle]
    pub extern "C" fn bs_time() -> f32 {
        f32::from_bits(TIME_BITS.load(Ordering::Relaxed))
    }
}

pub(crate) struct Opts {
    pub record: Option<String>,
    pub fps: u32,
    pub max_frames: Option<u32>,
    pub scale: f32,
    pub still: Option<f32>,
    pub from: f32,
    pub to: Option<f32>,
    pub alpha: bool,
    pub png: bool,
    pub gif: bool,
    pub grain: bool,
    /// Opaque paper background with no page chrome (web embedding).
    pub bare: bool,
    /// Present as a slideshow: pause at every [`Movie::slide`] boundary,
    /// `Space`/`→` plays forward to the next one.
    pub slideshow: bool,
}

pub(crate) fn parse_opts() -> Opts {
    let args: Vec<String> = std::env::args().collect();
    let mut opts = Opts {
        record: None,
        fps: 60,
        max_frames: None,
        scale: 0.0,
        still: None,
        from: 0.0,
        to: None,
        alpha: false,
        png: false,
        gif: false,
        grain: false,
        bare: false,
        slideshow: false,
    };
    let mut i = 1;
    let value = |args: &[String], i: usize, flag: &str| -> String {
        args.get(i + 1)
            .unwrap_or_else(|| panic!("{flag} expects a value"))
            .clone()
    };
    while i < args.len() {
        match args[i].as_str() {
            "--record" => {
                if i + 1 < args.len() && !args[i + 1].starts_with("--") {
                    opts.record = Some(args[i + 1].clone());
                    i += 1;
                } else {
                    opts.record = Some("frames".into());
                }
            }
            "--fps" => {
                opts.fps = value(&args, i, "--fps").parse().expect("--fps: number");
                i += 1;
            }
            "--scale" => {
                opts.scale = value(&args, i, "--scale").parse().expect("--scale: number");
                i += 1;
            }
            "--frames" => {
                opts.max_frames = Some(
                    value(&args, i, "--frames")
                        .parse()
                        .expect("--frames: number"),
                );
                i += 1;
            }
            "--still" => {
                opts.still = Some(
                    value(&args, i, "--still")
                        .parse()
                        .expect("--still: seconds"),
                );
                i += 1;
            }
            "--from" => {
                opts.from = value(&args, i, "--from").parse().expect("--from: seconds");
                i += 1;
            }
            "--to" => {
                opts.to = Some(value(&args, i, "--to").parse().expect("--to: seconds"));
                i += 1;
            }
            "--alpha" => opts.alpha = true,
            "--png" => opts.png = true,
            "--gif" => opts.gif = true,
            "--grain" => opts.grain = true,
            "--slideshow" => opts.slideshow = true,
            _ => {}
        }
        i += 1;
    }
    if opts.scale <= 0.0 {
        // web renders into a high-DPI canvas: supersample so it stays crisp
        // on retina displays instead of upscaling a 1x target.
        opts.scale = if cfg!(target_arch = "wasm32") {
            2.0
        } else if opts.record.is_some() || opts.still.is_some() {
            1.5
        } else {
            1.0
        };
    }
    opts
}

const GRAIN_VERT: &str = r#"#version 100
attribute vec3 position;
attribute vec2 texcoord;
varying lowp vec2 uv;
uniform mat4 Model;
uniform mat4 Projection;
void main() {
    gl_Position = Projection * Model * vec4(position, 1);
    uv = texcoord;
}"#;

const GRAIN_FRAG: &str = r#"#version 100
precision lowp float;
varying lowp vec2 uv;
uniform sampler2D Texture;
void main() {
    vec4 c = texture2D(Texture, uv);
    float n = fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453);
    c.rgb += (n - 0.5) * 0.04;
    float d = distance(uv, vec2(0.5, 0.5));
    c.rgb *= 1.0 - 0.15 * smoothstep(0.4, 0.9, d);
    gl_FragColor = c;
}"#;

fn fullscreen_pressed() -> bool {
    let command_down = is_key_down(KeyCode::LeftSuper) || is_key_down(KeyCode::RightSuper);
    let control_down = is_key_down(KeyCode::LeftControl) || is_key_down(KeyCode::RightControl);
    is_key_pressed(KeyCode::F)
        || is_key_pressed(KeyCode::F11)
        || (command_down && control_down && is_key_pressed(KeyCode::F))
}

pub async fn run_loop(movie: Movie) {
    let fonts = Fonts::load();
    let theme = movie.theme.clone();
    let (base, timeline) = movie.finalize();
    #[allow(unused_mut)]
    let mut opts = parse_opts();
    // On the web the page supplies its own chrome (masthead, rules, framing),
    // so render an opaque paper background with no chrome. Opaque (not
    // transparent) keeps it seamless without depending on canvas alpha
    // compositing, which is inconsistent across browsers.
    #[cfg(target_arch = "wasm32")]
    {
        opts.bare = true;
    }
    let (w, h) = (movie.width as f32, movie.height as f32);
    let s = opts.scale;
    let (pw, ph) = ((w * s).round(), (h * s).round());

    let rt = render_target(pw as u32, ph as u32);
    rt.texture.set_filter(FilterMode::Linear);
    let rt_cam = Camera2D {
        zoom: vec2(2.0 / pw, 2.0 / ph),
        target: vec2(pw / 2.0, ph / 2.0),
        render_target: Some(rt.clone()),
        ..Default::default()
    };
    // second target for baking the grain pass into recorded output
    let rt_post = render_target(pw as u32, ph as u32);
    rt_post.texture.set_filter(FilterMode::Linear);
    let rt_post_cam = Camera2D {
        zoom: vec2(2.0 / pw, 2.0 / ph),
        target: vec2(pw / 2.0, ph / 2.0),
        render_target: Some(rt_post.clone()),
        ..Default::default()
    };

    let grain = if opts.grain {
        load_material(
            ShaderSource::Glsl {
                vertex: GRAIN_VERT,
                fragment: GRAIN_FRAG,
            },
            MaterialParams::default(),
        )
        .map_err(|e| eprintln!("grain shader failed to compile: {e}"))
        .ok()
    } else {
        None
    };

    let render_canvas = |t: f32| {
        set_camera(&rt_cam);
        let scene = timeline.apply(&base, t);
        let view = View::from_scene(&scene, w, h, s);
        if opts.alpha {
            clear_background(Color::new(0.0, 0.0, 0.0, 0.0));
        } else if opts.bare {
            // web: paper background only, no masthead/border — the surrounding
            // page supplies the chrome, so the canvas blends into the article
            clear_background(theme.paper);
        } else {
            render::draw_page_chrome(&movie.title, w, h, &fonts, &view, &theme);
        }
        render::draw_scene(&scene, &fonts, &view);
    };

    // grain bake: rt -> rt_post through the material; both passes flip, so
    // orientation matches the plain path
    let capture = |grain: &Option<Material>| -> Image {
        if let Some(g) = grain {
            set_camera(&rt_post_cam);
            gl_use_material(g);
            draw_texture_ex(
                &rt.texture,
                0.0,
                0.0,
                WHITE,
                DrawTextureParams {
                    dest_size: Some(vec2(pw, ph)),
                    ..Default::default()
                },
            );
            gl_use_default_material();
            set_default_camera();
            rt_post.texture.get_texture_data()
        } else {
            set_default_camera();
            rt.texture.get_texture_data()
        }
    };

    // capture feeds the still/record paths, which don't exist on web
    #[cfg(target_arch = "wasm32")]
    let _ = &capture;

    // ---- single still frame ----
    #[cfg(not(target_arch = "wasm32"))]
    if let Some(ts) = opts.still {
        render_canvas(ts);
        next_frame().await;
        let img = capture(&grain);
        let path = format!("still_{ts:.2}.png");
        img.export_png(&path);
        println!("wrote {path} ({pw}x{ph})");
        std::process::exit(0);
    }

    // ---- offline record ----
    #[cfg(not(target_arch = "wasm32"))]
    if let Some(dir) = opts.record.clone() {
        let mut rec = Recorder::new(
            &dir,
            opts.fps,
            pw as u32,
            ph as u32,
            opts.png || opts.alpha,
            opts.gif,
        )
        .expect("cannot create record dir");
        let end_t = opts.to.unwrap_or(timeline.dur).min(timeline.dur);
        let total = (((end_t - opts.from).max(0.0) * opts.fps as f32).ceil() as u32)
            .min(opts.max_frames.unwrap_or(u32::MAX));
        for f in 0..total {
            let t = opts.from + f as f32 / opts.fps as f32;
            render_canvas(t);
            let img = capture(&grain);
            rec.capture(&img);
            next_frame().await;
        }
        rec.finish(&movie.sections, &movie.marks, &movie.slides);
        std::process::exit(0);
    }

    #[cfg(target_arch = "wasm32")]
    {
        web::DUR_BITS.store(timeline.dur.to_bits(), std::sync::atomic::Ordering::Relaxed);
        *web::MARKS.lock().unwrap() = movie.marks.clone();
    }

    // ---- live preview ----
    // slideshow: pause on every slide boundary, Space/→ plays to the next
    let slide_times: Vec<f32> = movie.slides.iter().map(|(st, _)| *st).collect();
    let slideshow = opts.slideshow && !slide_times.is_empty();
    if opts.slideshow && slide_times.is_empty() {
        eprintln!("--slideshow: movie has no slides (add m.slide(\"name\") boundaries)");
    }
    let mut play_until: Option<f32> = None;
    let mut t: f32 = if slideshow { slide_times[0] } else { 0.0 };
    // web builds start paused; the host page drives playback
    let mut paused = cfg!(target_arch = "wasm32") || slideshow;
    let mut fullscreen = false;
    let frame_dt = 1.0 / opts.fps as f32;
    const SLIDE_EPS: f32 = 1e-3;

    loop {
        // page-driven controls: the atomics are authoritative at frame start,
        // local key handling below may still flip `paused` for this frame
        #[cfg(target_arch = "wasm32")]
        {
            use std::sync::atomic::Ordering;
            paused = web::PAUSED.load(Ordering::Relaxed);
            if let Some(st) = web::take_seek() {
                t = st;
            }
        }

        if fullscreen_pressed() {
            fullscreen = !fullscreen;
            set_fullscreen(fullscreen);
        }
        if slideshow {
            // Space/→ animate forward to the next boundary; ← snaps back one
            if is_key_pressed(KeyCode::Space) || is_key_pressed(KeyCode::Right) {
                let next = slide_times.iter().copied().find(|&s| s > t + SLIDE_EPS);
                play_until = Some(next.unwrap_or(timeline.dur));
                paused = false;
            }
            if is_key_pressed(KeyCode::Left) {
                t = slide_times
                    .iter()
                    .rev()
                    .copied()
                    .find(|&s| s < t - SLIDE_EPS)
                    .unwrap_or(slide_times[0]);
                paused = true;
                play_until = None;
            }
        } else {
            if is_key_pressed(KeyCode::Space) {
                paused = !paused;
            }
            if is_key_pressed(KeyCode::Right) {
                paused = true;
                t += frame_dt;
            }
            if is_key_pressed(KeyCode::Left) {
                paused = true;
                t -= frame_dt;
            }
        }
        if is_key_pressed(KeyCode::Period) {
            t += 1.0;
        }
        if is_key_pressed(KeyCode::Comma) {
            t -= 1.0;
        }
        if is_key_pressed(KeyCode::R) {
            t = if slideshow { slide_times[0] } else { 0.0 };
            if slideshow {
                paused = true;
                play_until = None;
            }
        }
        let digits = [
            KeyCode::Key1,
            KeyCode::Key2,
            KeyCode::Key3,
            KeyCode::Key4,
            KeyCode::Key5,
            KeyCode::Key6,
            KeyCode::Key7,
            KeyCode::Key8,
            KeyCode::Key9,
        ];
        for (i, k) in digits.iter().enumerate() {
            if is_key_pressed(*k) {
                if let Some((st, _)) = movie.sections.get(i) {
                    t = *st;
                }
            }
        }

        let (sw, sh) = (screen_width(), screen_height());
        let bar_y = sh - 26.0;
        let (mx, my) = mouse_position();
        if is_mouse_button_down(MouseButton::Left) && my >= bar_y {
            paused = true;
            play_until = None;
            t = (mx / sw).clamp(0.0, 1.0) * timeline.dur;
        }

        #[cfg(target_arch = "wasm32")]
        web::PAUSED.store(paused, std::sync::atomic::Ordering::Relaxed);

        if !paused {
            t += get_frame_time();
        }
        if let Some(pu) = play_until {
            if t >= pu {
                t = pu;
                paused = true;
                play_until = None;
            }
        }
        t = t.clamp(0.0, timeline.dur);

        #[cfg(target_arch = "wasm32")]
        web::TIME_BITS.store(t.to_bits(), std::sync::atomic::Ordering::Relaxed);

        render_canvas(t);

        // blit to window: fit, centred, letterboxed
        set_default_camera();
        // web: paper letterbox so bars match the page; native: ink bars
        #[cfg(target_arch = "wasm32")]
        clear_background(theme.paper);
        #[cfg(not(target_arch = "wasm32"))]
        clear_background(theme.ink);
        let fit = (sw / pw).min(sh / ph);
        let (dw, dh) = (pw * fit, ph * fit);
        let (dx, dy) = ((sw - dw) / 2.0, (sh - dh) / 2.0);
        if let Some(g) = &grain {
            gl_use_material(g);
        }
        draw_texture_ex(
            &rt.texture,
            dx,
            dy,
            WHITE,
            DrawTextureParams {
                dest_size: Some(vec2(dw, dh)),
                ..Default::default()
            },
        );
        if grain.is_some() {
            gl_use_default_material();
        }

        // ---- HUD (never recorded, never on web — the page is the chrome) ----
        #[cfg(not(target_arch = "wasm32"))]
        {
            draw_rectangle(0.0, bar_y, sw, 26.0, style::with_opacity(theme.ink, 0.85));
            draw_rectangle(0.0, bar_y, sw * (t / timeline.dur), 3.0, theme.accent);
            for (st, _) in &movie.sections {
                draw_rectangle(sw * (st / timeline.dur) - 1.0, bar_y, 2.0, 8.0, theme.paper);
            }
            let frame_no = (t * opts.fps as f32).round() as u32;
            let hud = if slideshow {
                let cur = slide_times
                    .iter()
                    .filter(|&&s| s <= t + SLIDE_EPS)
                    .count()
                    .max(1);
                let name = &movie.slides[cur - 1].1;
                format!(
                    "{}  slide {}/{} \"{}\"  t={:6.2}s  [space/->] next  [<-] back  [F] fullscreen  [R] restart",
                    if paused { "PAUSED " } else { "PLAYING" },
                    cur,
                    slide_times.len(),
                    name,
                    t
                )
            } else {
                format!(
                "{}  t={:6.2}s  frame={:5}  [space] play/pause  [</>] step  [,/.] +/-1s  [1-9] sections  [F/Ctrl+Cmd+F] fullscreen  [R] restart",
                if paused { "PAUSED " } else { "PLAYING" },
                t,
                frame_no
            )
            };
            draw_text_ex(
                &hud,
                10.0,
                bar_y + 18.0,
                TextParams {
                    font: fonts.mono.as_ref(),
                    font_size: 13,
                    font_scale: 1.0,
                    font_scale_aspect: 1.0,
                    rotation: 0.0,
                    color: theme.paper,
                },
            );
        }

        next_frame().await;
    }
}