Renders .excalidraw files to SVG, byte-identical to the official exportToSvg, without a browser.
$ excalidraw2svg diagram.excalidraw -o diagram.svgEnglish: a MoonBit renderer for Excalidraw files. It reads .excalidraw JSON and writes the same SVG as Excalidraw 0.18.1's exportToSvg, byte for byte, without a browser. All expected outputs come from the pinned official package.
| 项目 | 做什么 | 和本项目的关系 |
|---|---|---|
| moon_rough | rough.js 4.6.6 的逐位一致移植 | 本项目依赖它画所有手绘图形。Excalidraw 用的是 rough.js 4.6.4,所以在 moon_rough 里加了 roughjs_464 选项(默认关闭,不影响原有行为) |
| mizchi/moonlight | 一个“像 Excalidraw”的编辑器 | 用自己的数据模型,不读写 .excalidraw 格式;本项目只渲染官方格式,不做编辑 |
| f4ah6o/papyr | 把 Excalidraw 数据当 JSON 原样放进 Markdown | 不渲染;本项目可以给它提供渲染 |
| perfect-freehand | Excalidraw 手绘笔迹的算法 | MoonBit 里原先没有实现,本项目移植了 getStroke |
moon add sayoi7799/moon_excalidrawimport {
"sayoi7799/moon_excalidraw",
}moon run --target js src/cmd/excalidraw2svg -- diagram.excalidraw -o diagram.svgexcalidraw2svg <input.excalidraw> [options]
-o, --output <file> 把 SVG 写到文件,而不是打印
--no-fonts SVG 里不带字体规则
--strict 遇到画不了的元素就报错,而不是跳过
-h, --help///|
fn main {
let text =
#|{"type":"excalidraw","version":2,"elements":[
#| {"type":"rectangle","id":"a","x":0,"y":0,"width":120,"height":60,"seed":1}
#|]}
let scene = @moon_excalidraw.parse_scene(text) catch {
// 非法 JSON、坏字段会 raise ParseError
err => {
println("bad file: \{err}")
return
}
}
let result = @moon_excalidraw.export_to_svg(
scene,
fonts=None, // Url(默认)或 None
on_unsupported=SkipWithWarning, // 或 Error
) catch {
err => {
println("cannot draw: \{err}")
return
}
}
println(result.svg)
for w in result.warnings {
println(w.message)
}
}moon run --target js src/cmd/excalidraw2site -- examples/site/diagrams -o examples/site/dist --title "我的图"| 内容 | 状态 |
|---|---|
| 元素:rectangle、diamond、ellipse(含圆角) | 支持 |
| 元素:line、arrow(直线、曲线、12 种箭头样式) | 支持 |
| 元素:freedraw(含闭合后的填充) | 支持 |
| 元素:text(含绑定在矩形、菱形、椭圆、箭头里的文字)、image(含裁剪、翻转、圆角)、frame、magicframe | 支持 |
| 样式:strokeColor、backgroundColor、fillStyle(hachure、cross-hatch、solid、zigzag)、strokeWidth、strokeStyle、roughness、opacity、angle、seed、link | 支持 |
| 导出:画布大小、10px padding、背景色、元素顺序、frame 裁剪和标题、<a> 链接 | 支持 |
| 字体:FontMode::Url(官方取不到字体时的 @font-face URL 形式)、FontMode::None(对应 skipInliningFonts) | 支持 |
| 容错:isDeleted、缺字段的旧文件(strokeSharpness、font、boundElementIds、draw 等)、未知字段 | 支持 |
| embeddable、iframe、肘形箭头(elbowed)、未知元素类型 | 报错或跳过并警告,由调用方选择,不会静默画错 |
| 编辑和交互、PNG、协作、.excalidrawlib、Obsidian 压缩格式、深色模式、exportEmbedScene、导出单个 frame、字体子集化 | 不做 |
| 情况 | 官方 | 本库 |
|---|---|---|
| 字体 | 内联子集化后的 base64 字体 | 只能是 URL 引用或不带字体。逐字节对照的是官方“取不到字体”时的输出 |
| embeddable、iframe、肘形箭头 | 画占位框或肘形线 | 报错或跳过并警告;跳过的元素也不计入画布大小 |
| 未知元素类型 | 静默丢弃 | 同样不画,但给出警告 |
| 缺 points 的 line/arrow、缺 pressures 又没开 simulatePressure 的 freedraw | 抛 TypeError,或画到一半静默丢掉这个元素 | 整个文件返回 ParseError 并指出元素 id |
| seed: 0 | 用 Math.random,每次结果不同 | 用 moon_rough 的固定回退序列,并给出警告 |
| 重复的元素 id | 随机生成新 id | 保留,并给出警告 |
| frame 标题的宽度 | 用浏览器 canvas 测量 | 默认按每个字符 0.5 × 字号 估算,调用方可以传入 measure_text。只影响过长标题在哪里被截断 |
| 标题截断时把一个代理对切成两半 | 字符串里留下孤立代理项,保存成文件后是 U+FFFD | 直接输出 U+FFFD |
| Math.pow(x, 3)(箭头标签位置用到) | V8 调用平台的 C 库,最后一位随平台变化 | 用正确舍入的立方;每次调用约有 0.04% 的概率和某个平台差最后一位 |
| 画布里有一个 1e12 宽的 hachure 填充矩形 | 内存耗尽(要画几十亿条线) | 同样的问题,没有额外保护 |
| Suite | What it covers | Scenes | Byte-identical | Different |
|---|---|---|---|---|
| single basic | Rectangles, diamonds, ellipses × fill, stroke, roughness, roundness | 252 | 252 | 0 |
| single linear | Lines, arrows, closed fills, all arrowhead styles | 362 | 362 | 0 |
| single text | Text: every font id, alignment, direction, bound to shapes and arrows | 200 | 200 | 0 |
| single misc | Freedraw, images, frames, links | 78 | 78 | 0 |
| fonts | Font rules (FontMode::Url) for each family and character set | 185 | 185 | 0 |
| combined | Diagrams (editor API, hand-made, random, and 3 drawn in the real editor) | 55 | 55 | 0 |
| edge cases | Empty and degenerate scenes, extreme numbers, old formats, official errors | 65 | 65 | 0 |
| Total | 1197 | 1197 | 0 |
| Lower-level check | Cases | Different |
|---|---|---|
| perfect-freehand outlines, every number compared by bit pattern | 322 | 0 |
| restoreElements normalisation of old and partial elements | 50 | 0 |
| JSON numbers read like V8 does | 10000 | 0 |
| Math.hypot | 5256 | 0 |
| Math.atan2 | 5256 | 0 |
| x³ correctly rounded (V8's Math.pow(x, 3) varies by platform) | 8000 | 0 |
| 包 | 核心逻辑 |
|---|---|
| export | 编排整个流程:过滤元素、给 frame 加标题文字、算画布大小(frame 里的元素不计入)、写根节点、裁剪路径、背景,再按顺序画每个元素,绑定文字紧跟它的容器 |
| shape | 把元素样式变成 rough 选项(线宽、虚线、小图形降低粗糙度),每类元素对应一次 rough 调用;箭头头部的方向取自 rough 画出的第一段或最后一段贝塞尔曲线,所以和 seed 有关;包围盒来自 rough 的路径 |
| freehand | perfect-freehand 的 getStroke:先平滑输入点,再把笔迹向两侧偏移、加端帽,连成一个多边形 |
| text | 字体 id 到字体名和度量的表、基线位置的公式、RTL 判断、官方 @font-face 规则的选择 |
| svg | 一棵很小的 SVG 树,按 outerHTML 的规则序列化(属性顺序、转义) |
| model | 读 JSON,丢掉已删除的元素,套用官方 restore 的规则补齐旧文件缺的字段 |
| internal/jsnum | Math.hypot、atan2、parseFloat 等 V8 行为,逐位对照 |
moon check --deny-warn
moon fmt --check
moon test --target all # js、wasm-gc、nativecd scripts
npm ci
node gen_fixtures.mjs # 全部;也可以只跑某一组,如 `node gen_fixtures.mjs edge`
cd .. && moon fmt # 生成的 .mbt 要格式化,之后和提交的应该完全一致
node scripts/update_readme_stats.mjsfn export_to_svg(scene : Scene, fonts? : FontMode, on_unsupported? : UnsupportedPolicy, measure_text? : (String, String) -> Double) -> ExportResult raise ExportErrorInstall
Download zipRenders .excalidraw files to SVG, byte-identical to the official exportToSvg, without a browser.