跪拜 Guibai
← All articles
SVG

Who's Actually Playing Your SVG Animation? Turning Mermaid Step Sequences Into GIFs

By Swot ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Anyone building slide decks, documents, or printed handouts from Mermaid step-sequence diagrams will hit the gap between a browser-playing SVG and a static PNG; this workflow defines a deterministic path from SVG to GIF, including the `begin`/`dur` math, reduced-motion handling, and version checks that otherwise cause silent failures.

Summary

Mermaid's step-number animation isn't a rendering-time feature: the exported SVG carries its own SMIL and CSS declarations, so Chrome plays it directly and even an `<img>` tag keeps it moving. Static converters such as `rsvg-convert`, `cairosvg`, and ImageMagick don't run that timeline; they draw one DOM snapshot, and they also drop the HTML text inside `<foreignObject>`, leaving Chinese node labels empty. The only reliable route to a GIF is a headless browser that seeks the SVG timeline with `setCurrentTime(t)` and captures frames.

That process requires reading the loop period from the `begin` and `dur` attributes, explicitly disabling reduced-motion, and verifying the output with frame hashes. A short script wraps that loop and hands the frames to ffmpeg. The same script also relies on mermaid 12.3.0, the first version with the `stepNumbers` syntax; 12.2.0 silently ignores it.

Takeaways
— SMIL and CSS animations declared inside an SVG run when the file is opened directly in Chrome or via `<img>`, because the image context disables scripts but not these engines.
— Static converters such as `rsvg-convert`, `cairosvg`, and ImageMagick do not run the SVG animation timeline and won't render HTML text inside `<foreignObject>`.
— The GIF loop period is derived from the `begin` and `dur` attributes on `<animateMotion>`; for a `dur` of 1s and a repeat base of `id.end + 2s`, the per-edge period is 3 seconds.
— Identical `step: N` labels mean parallel playback, not sequential; two edges with the same step number animate at the same time.
— Frame capture must explicitly set `prefers-reduced-motion` to `no-preference`, otherwise the CSS `@media` rule hides the dot and highlight with `display:none`.
— Use `setCurrentTime(t)` for deterministic seeking instead of `sleep`, because screenshot latency causes drift and a visible loop seam.
— Verification uses `ffprobe` for frame count and duration, then frame-hash comparisons; if only one or two distinct frames exist, the animation didn't run.
— Mermaid's `stepNumbers` syntax requires mermaid 12.3.0; 12.2.0 silently ignores `@{ step: N }` and produces an SVG without `animateMotion`.
— The number badge is static and remains readable in PNG, PPT, PDF, and print, because it isn't part of the animation.
Conclusions

An exported SVG is still a self-contained animation document; only a raster format like PNG freezes a frame.

rsvg-convert and its peers treat foreignObject as opaque, so any SVG with embedded HTML loses text even before animation is considered.

A CLI update can silently ship a new renderer; the real mermaid version lives in the lockfile, and that determines whether step-number syntax works.

Concepts & terms
SMIL
Synchronized Multimedia Integration Language: a browser-native SVG animation engine that runs `<animateMotion>`, `<animateTransform>`, and similar elements; it is not disabled in an `<img>` context.
foreignObject
An SVG element that embeds HTML content inside an SVG; static converters and some renderers don't execute it, leaving the text empty.
prefers-reduced-motion
A CSS media query that lets users signal reduced motion; when active, the SVG's CSS hides the step dot and highlight.
stepNumbers
A Mermaid flowchart configuration that draws a static numbered badge on each edge; it is independent of the SMIL animation and survives static export.
From the discussion
Featured comments
Swot

Actually doesn't touch .mmd; that term was removed after the modification.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗