Manual
Almnck.
Price: 50ⓐ

Video Figure Design

Status: Superseded by Video Player Overhaul

Custom video player component for Almanack articles. Wraps HTML5 <video> with quality switching, timed annotations, and bidirectional linking to other page elements.

Goals

  1. Let readers choose between video sources (resolution, enhanced vs original) without leaving the page.
  2. Drive margin-box annotations and SVG overlay shapes from timecoded metadata, using standard WebVTT.
  3. Allow other figures and page elements to command the video (seek, highlight) and vice versa.
  4. Degrade gracefully: without JS, readers get a working native video player with subtitles.

Markup Contract

The article markdown contains a self-describing figure:

<figure class="figure" data-interactive data-video-figure>
<video controls preload="none" poster="assets/poster.jpg">
  <source src="assets/kropotkin-funeral-1921-360p.mp4"
          type="video/mp4" data-label="Original (360p)" default>
  <source src="assets/kropotkin-funeral-1921-enhanced-360p.mp4"
          type="video/mp4" data-label="Enhanced (360p)">
  <source src="assets/kropotkin-funeral-1921-enhanced.mp4"
          type="video/mp4" data-label="Enhanced (Full)" data-size="409MB">
  <track kind="subtitles" src="assets/funeral-ru.vtt"
         srclang="ru" label="Русский">
  <track kind="subtitles" src="assets/funeral-en.vtt"
         srclang="en" label="English">
  <track kind="metadata" src="assets/funeral-annotations.vtt"
         data-output="#kropotkin-annotations">
</video>
<figcaption>Похороны Кропоткина, 1921. Soviet archival footage.
  Public domain.</figcaption>
</figure>

Attributes

  • data-video-figure on the <figure>: marks this for JS enhancement.
  • data-label on each <source>: display name in the quality picker.
  • data-size on <source> (optional): file size hint shown to the reader before they stream a large file.
  • default on <source>: the initially selected source. Without JS, the browser picks the first compatible source as usual.
  • data-output on <track kind="metadata">: CSS selector for the margin box (or other element) that receives timed annotations.

Sources

Multiple <source> elements enumerate available quality tiers. The native <source> fallback chain still works without JS: the browser picks the first it can play. The quality picker reads all sources and lets the reader switch.

Subtitle Tracks

Standard <track kind="subtitles"> elements with WebVTT files. Rendered natively by the browser. WebVTT is the only subtitle format HTML5 <track> supports natively, and it is the right choice.

Metadata Tracks

<track kind="metadata"> elements carry timed annotations. The data-output attribute declares where annotations render. The browser does not render metadata tracks visually; JS sets track.mode = "hidden" to enable cuechange events.

Metadata VTT Format

Each cue in a metadata track is a JSON object:

WEBVTT

NOTE Annotations for Kropotkin's Funeral, 1921

00:00:12.000 --> 00:00:28.000
{"text": "The procession assembles outside the Hall of Columns",
 "shape": {"type": "rect", "x": 0.1, "y": 0.3, "w": 0.8, "h": 0.5}}

00:01:45.000 --> 00:02:10.000
{"text": "Sofia Grigoryevna, Kropotkin's widow",
 "shape": {"type": "rect", "x": 0.35, "y": 0.2, "w": 0.15, "h": 0.3}}

00:03:22.000 --> 00:03:22.001
{"text": "Black banners: 'Where there is authority, there is no freedom'"}

00:05:10.000 --> 00:06:00.000
{"text": "The cortège crosses the frozen Moskva River",
 "short": "Crossing the Moskva"}

Cue fields

Field Required Description
text yes Annotation text. Rendered in margin box or overlay.
short no Condensed text for overlay mode (mobile) when text is too long.
shape no SVG overlay shape drawn on the video canvas.
shape.type if shape Shape type: rect, circle, ellipse.
shape.x, shape.y if shape Position as fraction of video dimensions (0–1).
shape.w, shape.h if rect Size as fraction of video dimensions.
shape.r if circle Radius as fraction of video width.

Coordinates are normalized (0–1) so they are resolution-independent.

Multiple cues can be active simultaneously. The renderer stacks them.

Point-in-time annotations (no duration) use a near-zero duration (e.g., 00:03:22.000 --> 00:03:22.001).

JavaScript Behavior

video-figure.js is a dual-mode module. It works two ways:

Standalone mode: When loaded as a <script> tag or on page load, it queries [data-video-figure] elements and enhances each. Used by articles that just want a video with quality switching and annotations, without the full interactive figures framework.

Figure module mode: Exports meta and setup(container, { publish, subscribe, assets }) conforming to the interactive figures module interface. When loaded by runtime.js via a manifest, it receives pub/sub capabilities and can publish timecode and subscribe to seek commands from other figures.

Both modes provide the same three features:

1. Quality Picker

Reads all <source> elements and builds a selector inserted below the video inside the figure. When the reader picks a different source:

  1. Capture currentTime and paused state.
  2. Set video.src to the new source URL and call video.load().
  3. On loadedmetadata, seek to saved time. Resume playback if the video was playing.

The picker shows data-label text. If data-size is present, it appends a hint: “Enhanced (Full) — 409 MB”.

2. Annotation Toggle

A button in the figure toolbar that toggles metadata track rendering:

  • On: track.mode = "hidden" (cue events fire, annotations render).
  • Off: track.mode = "disabled" (no events, annotations cleared).

State persisted in localStorage, keyed by figure id.

3. Metadata Track Dispatch

On load, finds <track kind="metadata"> elements with data-output. For each track:

  1. Set track.mode = "hidden".
  2. Listen for cuechange events on the track.
  3. When cues activate: parse cue text as JSON, render into the output target.
  4. When cues deactivate: remove or fade the annotation.

Rendering modes

The dispatcher checks whether the data-output target is currently visible in a margin layout:

  • Desktop (margin visible): Annotations render as content inside the target margin box. The existing sidenotes.js handles placement and collision avoidance.
  • Mobile/narrow (margin collapsed): Annotations render as positioned overlays on the video canvas, similar to subtitle rendering.

This is a layout adaptation, not a content decision. The markup does not change. The JS detects layout state and chooses the rendering path.

SVG overlay shapes

When a cue includes a shape field, the JS renders it as an SVG element overlaid on the video:

  • A transparent <svg> element is created, sized to match the video, positioned absolutely over it.
  • Shapes are drawn using normalized coordinates scaled to the current video display size.
  • The SVG resizes with the video on window resize.
  • Shapes are clickable: clicking a shape scrolls to the corresponding margin annotation (or shows the overlay text on mobile).

Reverse linking

Annotation elements (in margin boxes or overlays) receive a data-timecode attribute. Clicking an annotation seeks the video to that timecode. This completes the bidirectional link: video drives annotations, annotations drive video.

Dual-Mode Integration

Standalone mode

For articles with just a video figure and no other interactive figures. The article markdown includes a script tag:

<script type="module">
  import { init } from './assets/video-figure.js';
  init();
</script>

Or video-figure.js can self-initialize: if not imported as a module by the runtime, it queries [data-video-figure] on DOMContentLoaded.

The markup uses data-video-figure:

<figure class="figure" data-video-figure>
  <video controls ...>...</video>
</figure>

Figure module mode

For articles using the interactive figures framework. The manifest declares the video figure:

{
  "figures": {
    "video": {
      "module": "./video-figure.js",
      "publishes": ["timecode"],
      "subscribes": ["seek"]
    }
  }
}

The markup uses data-interactive="video":

<figure class="figure" data-interactive="video">
  <video controls ...>...</video>
</figure>

The runtime calls setup(container, { publish, subscribe }). The module publishes timecode on every timeupdate event and subscribes to seek messages from other figures.

Module structure

// video-figure.js

export const meta = {
  name: 'video',
  description: 'Enhanced video player with quality switching and annotations',
};

// Called by runtime in figure-module mode
export function setup(container, { publish, subscribe }) {
  return enhance(container, { publish, subscribe });
}

// Called directly in standalone mode
export function init() {
  document.querySelectorAll('[data-video-figure]').forEach(fig => {
    enhance(fig, {});
  });
}

function enhance(container, { publish, subscribe } = {}) {
  const video = container.querySelector('video');
  // ... quality picker, metadata dispatch, SVG overlays
  // if publish.timecode exists, wire up timeupdate
  // if subscribe.seek exists, wire up seek handler
  return () => { /* teardown */ };
}

// Self-initialize if not loaded by runtime
if (!window.__almanackRuntime) {
  document.addEventListener('DOMContentLoaded', init);
}

The core enhance() function is shared. Pub/sub capabilities are optional — present in figure-module mode, absent in standalone mode. The module checks for their existence before wiring them up.

Progressive Degradation

Feature Without JS
Video playback Native <video controls>, first <source>
Subtitles Native <track kind="subtitles"> rendering
Quality switching No — gets default source
Annotations No — metadata track ignored
SVG shapes No — overlay not created
Annotation toggle No — not applicable

Component Architecture

New files

  • web/static/js/video-figure.js — enhancement script.
  • Per-article .vtt files in assets/ as needed.

No new files needed for

  • CSS: extend existing .figure and [data-interactive] styles in almanack.css.
  • Margin boxes: use existing sidenotes.js injection system.

HTMX integration

video-figure.js must re-initialize after htmx:afterSwap, the same pattern used by the lightbox handler.

Dimension injection

The existing inject_image_dimensions in views.py already processes <video> tags using poster image dimensions. No changes needed.

Accessibility

  • Native <video controls> handles play/pause/seek/volume keyboard access.
  • Quality picker is a standard <select> or uses role="listbox" with arrow key navigation.
  • Annotation toggle is a <button> with aria-pressed state.
  • SVG overlay shapes have role="link" and aria-label set to the annotation text.
  • prefers-reduced-motion: disable annotation fade transitions.

Performance

  • No eager loading: preload="none" on the video.
  • Metadata VTT files are small (a few KB).
  • SVG overlay is a single element with a handful of shapes, not a per-frame render.
  • Quality switching causes a reload of the video stream, which is expected and unavoidable.

Implementation Sequence

  1. video-figure.js with quality picker only. Test with Kropotkin article.
  2. Add metadata track dispatch and margin-box annotation rendering.
  3. Add SVG overlay shapes.
  4. Add mobile overlay fallback for annotations.
  5. Add annotation toggle with localStorage persistence.
  6. Add pub/sub integration for articles using the figures runtime.

Open Questions

  • Authoring tooling: Editing VTT-with-JSON by hand is workable but not friendly. A small helper script that takes a simpler format (YAML, CSV) and produces the VTT could come later.
  • Streaming large files: The 2.6 GB final_restored.mp4 probably needs HLS or DASH segmentation if we ever want to serve it directly. For now, the 360p versions are the web-served files.
  • Poster per source: Should each quality tier have its own poster image, or does one poster suffice? One poster is simpler.