Use Levni
Node API
Everything the CLI does is a function. Use it in a build step, a CMS hook, a script or your own MCP server. Node.js 20 or newer, ES modules.
Import
npm install @levni/levniimport { compose, critique, render } from '@levni/levni';The package is ESM only. From a CommonJS file, use await import('@levni/levni'). Boards need nothing extra; posters and the type engine arrive with 0.3.0 (see install).
Render a scene
render(scene, out, opts?) draws a scene and writes the file. The extension picks the format. It returns the critic’s notes and the SVG.
import { render } from '@levni/levni';
const scene = {
seed: 4,
elements: [{ type: 'prop', name: 'rocket', x: 600, y: 336, size: 360, shadow: true }],
};
const { notes } = await render(scene, 'rocket.png'); // or .webp, .svg
if (notes.length) console.warn(notes.join('\n')); // the critic
await render(scene, 'rocket.png', { preview: true }); // also rocket.prev.jpg (560 px)Options: preview (PNG only) writes a small JPEG next to the file; webp is passed to the WebP encoder (budgetKB, startQ, minQ, step).
Compose and critique
compose(scene) returns the picture as an SVG string without writing anything, plus used, the boxes of everything it drew, and the canvas. critique(used, canvas) turns those boxes into layout notes.
import { compose, critique } from '@levni/levni';
import sharp from 'sharp';
const { svg, used, canvas } = await compose(scene);
const notes = critique(used, canvas); // [] when the layout is fine
const png = await sharp(Buffer.from(svg)).png().toBuffer();Scenes are plain objects, so build them in code. The same seed always gives the same picture, which makes renders safe to cache by scene hash.
const sizes = { og: 'og', square: 'square', story: 'story' };
for (const [name, format] of Object.entries(sizes)) {
await render({ ...scene, format, reflow: true }, `cover-${name}.webp`);
}format with reflow: true recomposes the scene for another canvas instead of cropping it. See Formats and reflow.
Work with drawings
import { loadProps, findProp, validateProp, measureVariant, addPropDir } from '@levni/levni';
const all = loadProps(); // { rocket: { name, category, tags, variants }, ... }
const rocket = findProp('roket'); // names and tags match, in any language
const chair = findProp('armchair-2'); // an old name finds the renamed drawing (armchair-padded)
console.log(rocket.name, rocket.variants.length);
addPropDir('./my-drawings'); // include your own folder
const problems = validateProp(myProp); // [] when it is valid
const { size, warnings } = measureVariant(myProp.variants[0]);Boards and posters
import { renderBoard, expandBrand, renderPosters, expandPosters } from '@levni/levni';
// one call, nine tiles
const brand = { name: 'Feneri', tagline: 'Slow ferries across the Bosphorus', mark: 'lighthouse',
inks: ['#14213D', '#FCA311', '#3A86A8', '#EFEAE0'] };
const { width, height, bytes } = await renderBoard({ brand }, 'board.png');
const spec = expandBrand(brand); // the full board spec, to print and edit
// eight posters and series.png (0.3.0)
const { files } = await renderPosters({ brand: { ...brand, words: ['Sail', 'Drift', 'Land', 'Return'] } }, 'posters/');
const list = expandPosters({ brand }); // [{ template, scene }, ...]: edit a scene, then render(scene, ...)Formats: Brand boards and Branding posters. renderBoard takes { baseDir } to resolve scene paths in a spec.
Export and print passes
If you hold an SVG already, toPng and toWebp rasterise it, both through rasterize: the picture is drawn in three plain layers and the crayon finish is applied to the pixels in JavaScript, which is about seven times faster than the SVG renderer's own filter with the same result. Use rasterize(svg, { density, raw }) directly for buffers, thumbnails or your own pipeline. A scene’s screen field runs an ordered-dither print pass after the raster; screenRaster exposes it.
import { compose, toWebp, toPng } from '@levni/levni';
const { svg } = await compose({ ...scene, screen: 'gameboy' });
await toWebp(svg, 'out.webp', { budgetKB: 80, screen: 'gameboy' });
await toPng(svg, 'out.png', { screen: 'gameboy' });All exports
| Export | What it is |
|---|---|
compose(scene) | async. Returns { svg, used, canvas, xf }. |
render(scene, out, opts?) | async. Writes .png, .webp or .svg and returns { notes, svg }. |
critique(used, canvas?) | The layout critic: an array of at most six strings. |
COLORS, PALETTE | House colour names and hex values. |
W, H, rng, frame | Default canvas size, the seeded random generator, and the helper that wraps drawn markup in a canvas with paper and grain. |
loadProps() | All drawings as an object keyed by name. |
findProp(nameOrTag) | One drawing by name, old name (aliases) or tag, or null. |
expandProp(element) | Turns a prop element into a draw element (what compose does). |
addPropDir(dir) | Add a folder of drawings. |
LIBRARY_DIR | Path of the bundled library. |
validateProp(prop) | Array of problems; empty when valid. Also refuses brand names and uppercase or repeated tags. |
lintProp(prop) | Array of style hints from the Folio guide (tags, names, notes); empty when the drawing follows it. Never blocks. |
parseProps(text) | Parses one drawing, an array, or a chat answer with json blocks. |
measureVariant(variant) | { size, cx, cy, colors, warnings }. |
CATEGORIES | The shelf keys and their labels. |
FORMATS, resolveCanvas(spec) | Named canvases (cover, og, square, story, youtube, poster, poster-tall, a4) and the resolver. |
STYLES, applyStyle(svg, name) | Style packs (riso, miniature). |
rasterize(svg, opts?) | async. A Levni SVG to a PNG buffer, or raw RGBA with { raw: true }; density sets the scale (72 = 1 px per unit). Applies the crayon finish and film grain itself: about 0.15 s for a cover. |
toPng(input, out, opts?) | async. PNG with optional preview and screen pass. |
toWebp(input, out, opts?) | async. WebP under a size budget. |
bayer, screenRaster, SCREEN_PRESETS | Ordered dither and the screen print presets (1bit, riso, blueprint, gameboy, newsprint). |
expandIso(element) | Expands an iso element into a list of drawable elements. |
ASSETS | The generators behind the built-in element types. |
FONTS, layoutType | The bundled font stacks, and the layout the type element uses to turn text into paths. |
iconsAvailable(), resolveIcon(name) | The optional Tabler icon bridge. |
renderBoard(spec, out, opts?) | async. Renders a brand board PNG. |
expandBrand(brand) | Turns a brand shortcut into a full board spec. |
renderPosters(spec, outDir) | async. Renders the poster series. 0.3.0. |
expandPosters(spec) | The posters as [{ template, scene }]. 0.3.0. |
POSTER_TEMPLATES | The eight template names. 0.3.0. |
Subpath imports
The package also exposes narrower entry points: @levni/levni/props, /critic, /board, /poster, /judge and /mcp.
import { critique } from '@levni/levni/critic';
import { serve } from '@levni/levni/mcp'; // run the MCP server from your own process