api.graphics
GraphicsApi
The pictures the viewport draws, for a plugin's own lists and previews: the same cached canvases, so asking for one costs nothing after the first time. Everything is null when the graphics it needs were never extracted — a plugin shows a name instead.
- ready
- load
- unitImage
- spriteImage
- tileImage
- doodadImage
- renderRect
- renderClip
- playerColor
- requestUnit
- requestSprite
- icon
- requestIcons
- onImageLoaded
- grpImage
Members
ready
ready(): { tileset: boolean; units: boolean; };
Whether the tileset graphics and the unit tables are in memory.
load
load(): Promise<{ tileset: boolean; units: boolean; }>;
Fetch both; resolves with what is available afterwards.
unitImage
unitImage(unitId: number, options?: { owner?: number; }): PluginImage | null;
A unit type in its editor pose, in a player's colours (owner 0 by default).
spriteImage
spriteImage(kind: SpriteKind, id: number, options?: { owner?: number; flipped?: boolean; }): PluginImage | null;
A THG2 sprite: a sprites.dat image for "pure", a unit for "unit".
tileImage
tileImage(tileId: number): PluginImage | null;
One 32 × 32 megatile of the open map's tileset.
renderRect
renderRect(rect: Rect, options?: Partial<MapImageOptions>): Promise<Blob | null>;
Part of the map as File ▸ Export ▸ Image draws it — the same render, cropped to a tile rect. Everything it needs is loaded first; null without a map or a canvas.
renderClip
renderClip(clip: Clip, options?: RenderClipOptions): PluginImage | null;
A Clip drawn as the clipboard layer's paste ghost draws it: the picture (its own
tiles, or the catalogue's for a doodad-only clip), then units, sprites and doodad
overlays with the viewport's own graphics, then location boxes — a thumbnail for a
list of clips, or the ghost under the pointer while a plugin stamps one. It is drawn
with the graphics of the clip's own tileset (clip.era), which are in memory for
the open map once loaded; a clip from another tileset answers null until
tileset.load(id) has fetched that one. So does a missing tileset or a page with no
canvas. Synchronous and uncached:
keep the result while the clip and the tileset stay the same.
requestUnit
requestUnit(unitId: number): boolean;
Start fetching the GRPs a unit type needs, and say whether they are already in
memory. Graphics load lazily, so the first unitImage for a type is often null —
ask for it here, redraw on onImageLoaded, and the list fills in.
requestSprite
requestSprite(kind: SpriteKind, id: number): boolean;
icon
icon(kind: IconKind, id: number, options?: { look?: IconLook; }): PluginImage | null;
The game's own button picture for a unit type, an upgrade or a technology — the icon
its command card shows — 36 × 34 at most and transparent round the edges. look
colours it as the game does: "normal" yellow (the default), "disabled" grey,
"selected" white. It does not depend on the map or its tileset. A unit type past 227
has a picture only where the data set carries a sheet for every type
(unit\cmdbtns\uniticons.grp). Null for an id with no picture (a turret, a type past
227 without that sheet) and until requestIcons has the pictures;
null for good from a game data copy made before the editor kept them (installing the
game data again brings them). The canvas is the editor's own cached one: draw from it.
// A row of what a player researched, as the game's icons, with the names where there are no pictures.
const show = () => list.replaceChildren(...done.map((r) => {
const icon = api.graphics.icon(r.kind, r.id);
if (!icon) return api.ui.el("span", {}, r.kind === "tech" ? api.names.tech(r.id) : api.names.upgrade(r.id));
const canvas = api.ui.el("canvas", { width: icon.width, height: icon.height });
canvas.getContext("2d")?.drawImage(icon.image, 0, 0);
return canvas;
}));
api.graphics.requestIcons();
api.graphics.onImageLoaded(show);
show();
requestIcons
requestIcons(): boolean;
Start fetching the icon pictures (one sheet holds them all), and say whether they are
in memory. onImageLoaded fires when they arrive.
onImageLoaded
onImageLoaded(listener: () => void): Disposable;
A GRP finished loading: anything drawn from one may look different now.
grpImage
grpImage(grp: Uint8Array, frame: number, options?: GrpImageOptions): Promise<PluginImage | null>;
Draw one frame of a GRP you hold — a mod's picture, one you made — the way the editor
draws a unit's: through a tileset's palette, with the team colour slots in a player's
colour, index 0 transparent. Either kind of GRP is read (the run-length one units use,
and the uncompressed one of the interface pictures). The canvas is the GRP's whole box,
as unitImage's is. Resolves null when the bytes are no GRP, the frame is past its
last, or there is no game data to take the palette from. A GRP drawn through
remapping (fire, sparks) or Remastered's pictures are not drawn here.
const grp = await api.gameData.read("unit/terran/marine.grp");
const pic = grp && (await api.graphics.grpImage(grp, 0, { palette: "jungle", teamColor: 1 }));
if (pic) panelBody.append(pic.image);
Types
Declarations only this group names.
PluginImageinterface
interface PluginImage
A picture the editor already has: the canvas it draws from, and its size in pixels.
image: HTMLCanvasElement | |
|---|---|
width: number | |
height: number |
RenderClipOptionsinterface
interface RenderClipOptions
pixelsPerTile?: number | Pixels per tile; 32 (the game's own) by default. A thumbnail wants 4–8. |
|---|---|
parts?: Partial<ClipParts> | Which parts to draw; every part the clip carries by default. |
GrpImageOptionsinterface
interface GrpImageOptions
How graphics.grpImage draws a GRP.
palette?: string | The tileset whose palette the picture is drawn through, by its file name: |
|---|---|
teamColor?: number | null | The player whose colour the team slots (indices 8–15) take, as the open map colours its players; 0 by default. Null leaves them the palette's own. |
flipped?: boolean | Mirrored left to right, as a unit facing the other way is drawn. |
shadow?: boolean | As a shadow: a half-transparent black silhouette. |