Reference
API
Every export of @industrieh/starmap-render, with its signature. Everything comes from the package root.
Rendering
renderSystem
functionDraw one system as a standalone SVG document, at a given moment.
function renderSystem(
system: StarSystem,
options?: RenderSystemOptions,
): stringParameters
- systemStarSystem
- One system —
galaxy.systems[0], not the galaxy. - optionsRenderSystemOptions
- See the table below.
Returns
A complete
<svg>…</svg> string — write it to a file, inline it, or put it in a data: URI.const svg = renderSystem(galaxy.systems[0], { t: 12.5, width: 700, height: 700 })
await Bun.write('sol.svg', svg)renderSystemContent
functionThe same drawing, without the
<svg> around it. This is what an interactive application wants: it owns the root element, so it owns the viewBox — and pan and zoom are then nothing more than changing it.function renderSystemContent(
system: StarSystem,
options?: RenderSystemOptions,
): stringconst body = renderSystemContent(system, { t })
const box = systemViewBox(system)
container.innerHTML = `<svg viewBox="${viewBoxAttr(box)}">${body}</svg>`renderGalaxy
functionDraw the whole map: systems as points, hyperlanes as the network between them. Takes no
t — at galaxy scale a planet is smaller than a rounding error.function renderGalaxy(
galaxy: Galaxy,
options?: RenderGalaxyOptions,
): stringconst route = findRoute(galaxy, 'sol', 'vega')
const map = renderGalaxy(galaxy, { highlight: route?.systemIds })renderGalaxyContent
functionThe galaxy's layers alone. Pair it with
galaxyViewBox() when your application owns the root element.function renderGalaxyContent(
galaxy: Galaxy,
options?: RenderGalaxyOptions,
): stringSYSTEM_LAYERS, GALAXY_LAYERS
constantEvery layer of each drawing, bottom to top — the order they are always drawn in. Pass a subset as the
layers option and only those are computed, still in this order.const SYSTEM_LAYERS: readonly SystemLayer[]
// ['defs', 'background', 'backdrop', 'orbits', 'fields', 'routes', 'star', 'planets', 'gates']
const GALAXY_LAYERS: readonly GalaxyLayer[]
// ['defs', 'background', 'backdrop', 'lanes', 'systems']Layers stack, so a split needs care
The
star and the gates sit above the routes, and the gates above the planets. Splitting a drawing into a still part and a moving part therefore means three still groups around the two moving ones — or accepting that the routes pass over the star.// Only routes and planets depend on t: draw the rest once…
const still = renderSystemContent(system, {
layers: ['defs', 'background', 'backdrop', 'orbits', 'fields'],
})
// …and redraw just what moves, every frame.
const moving = renderSystemContent(system, { t, layers: ['routes', 'planets'] })Options
Both option types extend DocumentOptions, so width, height, title and role are available on all four functions — the two …Content ones ignore them, since they emit no root element.
| Field | Type | Description |
|---|---|---|
t | numberdefault 0 | Simulation time, in seconds. System only— compared against each object's period, the same way systemPositions() does it. |
theme | ThemeInputdefault DEFAULT_THEME | Colours. Anything left out is merged from the default — Theming. A theme returned by resolveTheme() is used as is, with no merge. |
margin | numberdefault 0.45 / 0.12 | Empty space around the drawing, as a fraction. 0.45 of outerLimit for a system; 0.12 of the largest dimension for a galaxy. |
labels | booleandefault true | Names of planets and gates, or of systems on the galaxy map. |
backdrop | numberdefault 260 / 400 | How many decorative background stars. 0 draws none — worth doing for a thumbnail or an animation. |
idPrefix | stringdefault `starmap-<system id>` / 'starmap' | Prefix of the gradient ids this drawing declares. The system default is built from the system's id, so two systems inlined on one page never collide; pass your own only to draw the same system twice with different themes — why it matters. Characters other than [A-Za-z0-9_-] become _, and a leading non-letter gets a _ in front. |
systems | SystemsById | System only. From indexSystems(galaxy). When given, a gate label names where it leads. |
highlight | readonly string[] | Galaxy only. An ordered path — exactly what findRoute() returns in systemIds. Consecutive entries light the lane between them. |
layers | readonly SystemLayer[] / GalaxyLayer[]default every layer | Draw only these, always in their usual order — the lists. The others are not even computed, which is what lets an animation draw the still layers once and redraw only routes and planets. |
width, height | number | On the root element, in pixels. Left out by default, so the drawing fills its container. |
title | string | Adds a <title> child and role="img", so a screen reader announces the drawing instead of skipping it. |
role | string | falsedefault 'img' with a title | The root element's role. Pass false for an interactive drawing whose objects you make reachable yourself — role="img" hides everything inside it from assistive technology. Any other string is written as is. |
Framing
systemViewBox
functionThe framing
renderSystem()would use. Square and centred on the origin, because a system's star is always at (0, 0).function systemViewBox(system: StarSystem, margin?: number): ViewBox
const SYSTEM_MARGIN = 0.45Returns
{ x, y, w, h } in world units. A system whose outerLimit is 0 or nonsense falls back to 100.systemViewBox(system) // { x: -435, y: -435, w: 870, h: 870 } — outerLimit 300 + 45%
systemViewBox(system, 0) // { x: -300, y: -300, w: 600, h: 600 } — cropped to the edgegalaxyViewBox
functionThe bounding box of the systems, plus a margin. Not centred — a galaxy keeps the coordinates you gave it.
function galaxyViewBox(galaxy: Galaxy, margin?: number): ViewBox
const GALAXY_MARGIN = 0.12Returns
An empty galaxy returns
-100 -100 200 200. A single system, or several in a straight line, would give a box with zero width or height — both are clamped to at least 1.galaxyViewBox(galaxy) // { x: -50.4, y: -50.4, w: 520.8, h: 260.8 }viewBoxAttr
functionFormat a ViewBox as the attribute value.
function viewBoxAttr(view: ViewBox): stringviewBoxAttr(systemViewBox(system)) // '-435 -435 870 870'svgDocument
functionWrap finished layers in a standalone document. Writes
xmlns, which is what makes the string usable outside an HTML parser — a .svg file, a data: URI, an <img src>. A title brings role="img" with it, unless role says otherwise.function svgDocument(
view: ViewBox,
body: string,
options?: DocumentOptions,
): stringsvgDocument({ x: 0, y: 0, w: 10, h: 10 }, '<circle r="1"/>', { title: 'T' })
// <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"
// preserveAspectRatio="xMidYMid meet" role="img">
// <title>T</title>
// <circle r="1"/>
// </svg>systemScale, galaxyScale
functionThe exact sizes a drawing derives from its view — glyphs, strokes, labels are fractions of it, so they hold up at any scale. Exported so an overlay drawn on top, a selection ring or a ship, matches the drawing under it.
function systemScale(view: ViewBox): SystemScale // { u, k, font }
function galaxyScale(view: ViewBox): GalaxyScale // { dot, ring, glow, k, font }Parameters
- viewViewBox
- The drawing's framing —
systemViewBox()orgalaxyViewBox(), with the same margin you render with.
Returns
For a system:
u, the spacing unit (a 200th of the view's width), k, the stroke unit, and font. For a galaxy: the radii of a system's dot, ring and glow, the stroke unit k, and font.const view = galaxyViewBox(galaxy)
const { ring, k } = galaxyScale(view)
const sol = galaxy.systems[0]
// A selection ring just outside the faction ring, at the drawing's own stroke width.
el('circle', { cx: sol.x, cy: sol.y, r: ring * 1.8, fill: 'none', stroke: '#fff', 'stroke-width': k * 3 })Shapes
arcPath
functionThe
d of a circular arc centred on the origin. Used for asteroid belts, which are arcs rather than full rings.function arcPath(r: number, startDeg: number, sweepDeg: number): stringPast 359.5° it becomes two arcs
A single SVG arc cannot describe a complete circle — its end point would be its start point, and the arc would collapse to nothing.
arcPath(220, 0, 120)
// 'M 220 0 A 220 220 0 0 1 -110 190.53'
arcPath(220, 0, 360)
// 'M 220 0 A 220 220 0 1 1 -220 0 A 220 220 0 1 1 220 0'backdropStars
functionA field of decorative stars, sown in a square centred on the origin — renderGalaxy() then shifts it onto the centre of its view. Deterministic: the same seed always gives the same sky.
function backdropStars(
count: number,
radius: number,
seed: string | number,
scale?: number,
): BackdropStar[]Parameters
- countnumber
- How many.
- radiusnumber
- Half-side of the square they are sown in.
- seedstring | number
- Anything stable. A string is hashed, a number is used as is.
- scalenumber = 1
- Multiplies each dot radius. Dots are sized in world units, so a galaxy-wide view needs bigger ones than a single system to look the same.
Returns
{ x, y, r, o }[] — position, radius, opacity.It runs on its own seed, not the galaxy's
A generator sharing one sequence with its backdrop would shift its whole universe by changing the number of background stars.
Theme
resolveTheme
functionFill a
ThemeInput in from the default. Tables are merged key by key, so { planets: { terran: '#7ee787' } } keeps every other planet colour. A theme it returned before is handed back as is: resolve once, pass the result to every frame, and no frame merges anything.function resolveTheme(input?: ThemeInput): Theme
const DEFAULT_THEME: ThemeReturns
A complete Theme — every field filled in, every table carrying its default entry.
Nothing is merged deeper than a table
A
RouteStyle is replaced whole, because a dash pattern without its width is not a style. And a field explicitly set to undefined falls back to the default rather than leaving a hole in the drawing.const theme = resolveTheme({ background: '#000', planets: { terran: '#7ee787' } })
theme.background // '#000' yours
theme.planets.terran // '#7ee787' yours
theme.planets.gas // '#d8a06a' still the defaultpickColor, pickRouteStyle
functionLook a key up, falling back to the table's
default. The core's vocabularies are open unions, so a table can never be exhaustive — which is why defaultis required by the type. Only the table's own keys count: a planet type named constructor gets the default, not something inherited from Object.prototype.function pickColor(table: ColorMap, key: string | undefined): string
function pickRouteStyle(table: RouteStyleMap, key: string | undefined): RouteStylepickColor(theme.planets, 'terran') // '#7ee787'
pickColor(theme.planets, 'shattered') // '#8b8377' — the default
pickColor(theme.planets, undefined) // '#8b8377' — the default too
pickColor(theme.planets, 'constructor') // '#8b8377' — own keys onlyString primitives
The four functions the whole package writes its output through, exported so your own overlay gets the same escaping and number formatting. See Draw your own overlay.
| Field | Type | Description |
|---|---|---|
el(name, attrs, children?) | => string | One element, self-closing when it has no children. An attribute that is undefined, null or false is dropped.el('circle', { r: 4.005 }) → <circle r="4"/> |
group(attrs, parts) | => string | A <g> around several parts. Empty parts are dropped, and a group left with nothing returns '' — which is why an empty layer is never emitted. |
text(content, attrs) | => string | A <text> element. The content is escaped; pass raw names. |
num(value) | => string | Two decimals, no exponent. A non-finite value becomes '0' rather than 'NaN', which no SVG reader accepts. |
dash(...lengths) | => string | A stroke-dasharray value. Dash lengths are written into a string rather than an attribute, so they would otherwise escape num(). |
escapeXml(value) | => string | Escapes &, <, >, " and ', and drops the control characters XML cannot hold at all. Every name in a galaxy is user data. |
background(view, fill) | => string | The oversized rectangle behind everything — why three times. |
Types
| Field | Type | Description |
|---|---|---|
RenderSystemOptions, RenderGalaxyOptions | options | Both extend DocumentOptions. The table above. |
DocumentOptions | options | { width?, height?, title?, role? } — the root element. |
SystemLayer, GalaxyLayer | options | The layer names, for the layers option — the lists. |
SystemScale, GalaxyScale | geometry | What systemScale() and galaxyScale() return. |
ViewBox | geometry | { x, y, w, h }, in SVG viewBox order. |
BackdropStar | geometry | { x, y, r, o }. |
Theme, ThemeInput | theme | What the drawing reads, and what you write. The same two-layer split as the core. |
ColorMap | theme | A colour per key, plus a required default. |
RouteStyle, RouteStyleMap | theme | { stroke, dash, width }, and a table of them. dash follows stroke-dasharray syntax — lengths separated by spaces and/or commas; empty, none or anything unreadable draws a solid line. |
Attrs, AttrValue | primitives | What el() accepts. AttrValue includes undefined, null and false, all of which drop the attribute. |