Skip to content

Video as code: canvas scenes rendered with WebCodecs in headless Chrome, a review viewer, and agent skills that build the video from your references.

6.5 KB
Raw Blame

2D assets: SVG, characters, transitions

Every 2D asset is an SVG you write by hand (or a canvas module, for transitions), rendered to PNG by Chrome through templates/asset.html. Measured on the test laptop: a 400x600 SVG character to a 1600x2400 PNG in 2.6 s, most of it Chrome starting.

Writing the SVG

  • A viewBox in the asset's size at 1080p (0 0 400 600 for a figure 600 px tall in a 1080p frame), so the tokens' px@1080 values are the SVG's own units.
  • Colours, stroke width, joins and radii from tokens.json. Fills flat unless style.md says otherwise.
  • Text outlined to paths, or the font loaded by the page that draws it. An SVG drawn as an image cannot load fonts by itself, so a live <text> falls back to a system font in the PNG.
  • No <image> that points at another file and no external CSS: an SVG drawn as an image cannot fetch them. Embed a raster as a data: URL only when it truly is a photo.
  • Group what moves or might be recoloured: <g id="...">, named as in kit.md.
  • Redraw from the reference in its own pixel space: put the reference image under the SVG at the same size while drawing (grid.py from the motlab skill prints coordinates), then remove it. Check the result over the reference on sheet.py, not from memory.

Rendering: templates/asset.html

Copy it next to the SVG, set SRC, W, H (the box at 1x) and PAD, then:

motlab stills asset.html out 0 --scale 4        # out/0.00.png at 4x
motlab stills asset.html out 0 1 2 --scale 4    # one PNG per file in SRC

Rename the outputs to <asset>@4x.png. Chrome rasterises the SVG at the drawn size, so 4x is as sharp as 1x. For an icon set, put all icons in SRC and render them in one call.

Icons, illustrations, backgrounds, patterns

  • Icon sets: one grid for the whole set (for example 24 units with 2 units of padding), one stroke weight, the same corner radius and cap style. Build them in one session from the same rules, then put them side by side on one sheet: an odd one out shows at once.
  • Logos: rebuild the user's logo as clean paths only from a good reference (a vector file, or a large PNG). Put the rebuild over the original at the same size on a sheet and check every curve. The user confirms it before it goes in the kit.
  • Backgrounds and patterns: a pattern is an SVG <pattern> or a tile whose edges meet; check it by tiling the PNG 3x3 on a sheet. Grain or noise comes from a seeded function, so it is the same every render.

2D characters: parts.svg + rig.json

A character the scenes can pose is one SVG with every moving part as a top-level <g id> in the root <svg>, back to front, and a rig.json:

{ "parts": [
  { "id": "arm_l", "pivot": [102, 252], "parent": "torso" },
  { "id": "leg_l", "pivot": [165, 450] },
  { "id": "torso", "pivot": [200, 460] },
  { "id": "head",  "pivot": [200, 230], "parent": "torso" },
  { "id": "arm_r", "pivot": [298, 252], "parent": "torso" } ] }
  • pivot is in SVG units: a shoulder for an arm, the neck for the head, the hips for the torso. A part with a parent moves with it; a hand is a child of its forearm.
  • Draw each part a little past its joint and cover the joint with the part in front (the sleeve over the top of the arm), so rotating it never opens a gap. Check the extreme poses, not just the rest pose.
  • Things that change shape rather than turn (mouths, eyes, hands) are alternative groups: mouth_smile, mouth_open. The scene draws one of them; give each the same pivot.
  • templates/svgparts.js draws it in a scene: loadRig(svg, rig, res) then draw(ctx, x, y, pose, size) with pose = { head: { rot: -0.1 }, arm_r: { rot: 0.6, dx: 0, dy: 0, s: 1 } }. Copy it into the video's project next to the kit. A pose is a plain object, so poses can live in the kit as JSON and be tweened in the scene.
  • For a character drawn in one piece with detail too fine for parts (a painted illustration), use the motlab skill's puppet pins (puppet.js, references/engine-2d-puppet.md) on its PNG instead.

Outputs for a character:

  • <name>@4x.png: the rest pose, through asset.html.
  • turnaround.svg: front, three-quarter and side, drawn on one row at the same height with guide lines for eyes, shoulders, hips and feet. It is the reference for later poses and for a 3D version.
  • poses.png: a few poses through a page that uses svgparts.js (copy asset.html and draw with the rig), which also proves the rig bends well.
  • Expressions: one sheet with each mouth and eye group.

Transitions: transition.js

A transition here is a layer over motlab's hard cut between scenes: shapes cover the frame, the cut happens under them, and they uncover the next scene. templates/transition.js is a working example (three bands in brand colours sweeping across). Build one when the user asks for it; the video skill's default is still a transition designed frame by frame in the scenes. To use it, name it in project.json on the scene it leads into, and motlab draws it over the cut in the viewer and the render:

{ "file": "scenes/02.html", "transition": { "layer": "assets/kits/acme/transitions/swish/transition.js", "duration": 0.6 } }
  • DURATION in seconds; half plays at the end of the scene before, half at the start of the scene after.
  • draw(ctx, p, W, H) draws the layer at progress p from 0 to 1, in the page's own pixels. At p = 0.5 it must cover the frame completely, or the cut shows.
  • atEnd(t, TOTAL) and atStart(t) give p for the two scenes (or null outside the transition), for a scene that has to draw it itself, such as one page that holds two shots. With "layer" in project.json neither scene draws it.
  • Colours from tokens.json; motion from the motlab skill's easing rules (craft.md).

Check it with templates/transition.html: open it in a browser with BEHIND = true to see it over two cards, and render motlab stills transition.html frames 0 0.1 0.2 0.3 ... with BEHIND = false for the PNG sequence with alpha. Look at the still at exactly DURATION / 2: it must be fully covered. The PNG sequence is for editors outside motlab; motlab uses transition.js itself. In a two-scene render at 30 fps with a rigged svgparts.js character in both, the frame was fully covered on the last frame before the cut and the first two after it, and the character gave the same stills forward and in reverse.

motlab's shader transitions ("type" in a "transition") mix two scenes over an overlap and are a different thing; they are listed in the motlab skill's references/motion-and-libraries.md.