Animaku — 程式碼驅動的 Canvas 動畫工作室
使用 Animaku HTML5 Canvas 2D 引擎、edge-tts 神經網路語音合成,以及 headless Playwright + FFmpeg 渲染,從程式碼創作出精美的短影音動畫。
工作流程
1. 規劃故事板與旁白 (script.json)
在 projects/<slug>/ (或 examples/<slug>/) 下建立專案資料夾,並包含 script.json:
- 從 presets.md 中選擇一種語音 (例如:
id-ID-ArdiNeural、id-ID-GadisNeural、en-US-GuyNeural)。 - 將強調關鍵字用
*星號*包圍起來,讓動態字幕自動以黃色高亮這些詞 (例如:"Nahan *fantasi* liar")。 - 在台詞之間添加
pauseAfter(秒) 以營造戲劇性的停頓。
{
"voice": "id-ID-ArdiNeural",
"rate": "+6%",
"cues": [
{ "id": "c1", "text": "Nahan *fantasi* liar", "pauseAfter": 0.25 },
{ "id": "c2", "text": "Padahal masih *ngecat* tembok!", "pauseAfter": 0.3 }
]
}
2. 產生旁白音檔與精確時間戳記
執行 narrator.py 以合成 narrator.mp3 與 timing.json:
python .agents/skills/animaku/scripts/narrator.py --script projects/<slug>/script.json --out-dir projects/<slug>
3. 編寫動畫 Canvas 場景 (scene.js)
建立 projects/<slug>/scene.js,並從 ../../engine/animaku.js 導入輔助函式 (請參閱 canvas-api.md):
- 匯出
meta(width、height、fps),使其符合所需畫面比例 (預設為1080x1920直式)。 - 匯出
init({ width, height, timing, duration }),用於預先建立確定性的粒子系統或背景道具。 - 匯出
render(ctx, { width, height, t, duration, timing, state }):- 背景:使用
VFX.drawBlurredLayer(...)繪製具景深效果的房間、門口或漸層。 - 相機:將世界空間元素包裹在
cam.begin(ctx, width, height, t)...cam.end(ctx)之中,以實現流暢的平移/縮放與衝擊trauma震盪特效。 - 角色與道具:使用
drawCharacter(ctx, cfg, state, t)搭配'flat-glow'或'stickman-toon'風格、邊緣發光效果,以及VFX.drawMotionLines(...)/VFX.drawSweatDrop(...)。 - 視覺特效與後處理:添加
VFX.drawParticles(...)與VFX.drawPostProcess(ctx, width, height, { vignette, lightLeak, grain, t })。 - 動態字幕:在相機變換之外呼叫
drawKineticSubtitles(ctx, width, height, timing.cues, t),讓字幕保持清晰且置中。
- 背景:使用
4. 渲染為 MP4
執行 render.mjs,透過 headless Edge/Chrome 擷取確定性幀,並使用 FFmpeg 編碼:
node .agents/skills/animaku/scripts/render.mjs --scene projects/<slug>/scene.js --timing projects/<slug>/timing.json --audio projects/<slug>/narrator.mp3 --out projects/<slug>/output.mp4 --w 1080 --h 1920 --fps 30
選擇性:傳遞 --bgm path/to/music.mp3 參數,可在旁白之下以 18% 音量混合背景音樂。
5. 驗證輸出
確認渲染後的影片具有有效的視訊與音訊串流,且時長不為零:
ffprobe -v error -show_entries format=duration -show_entries stream=codec_type,width,height -of default=noprint_wrappers=1 projects/<slug>/output.mp4