Skip to main content
Version: Next

Types Reference

All types are exported from both packages and can be imported directly:

// From core package (vanilla JS)
import { Rect, Circle, Text, Path, ImageItem, Coords } from "@canvas-tile-engine/core";

// From React package
import { Rect, Circle, Text, Path, ImageItem, Coords } from "@canvas-tile-engine/react";

Drawing Types

Rect

Rectangle/square shape definition. Supports rotation and border radius.

type Rect = {
x: number;
y: number;
size?: number;
origin?: {
mode?: "cell" | "self";
x?: number; // 0 to 1
y?: number; // 0 to 1
};
style?: { fillStyle?: string; strokeStyle?: string; lineWidth?: number };
rotate?: number;
radius?: number | number[];
};
PropertyTypeDefaultDescription
x, ynumberRequiredWorld coordinates
sizenumber1Size in grid units
originobject{ mode: "cell", x: 0.5, y: 0.5 }Anchor point
styleobject{}Fill and stroke styling
rotatenumber0Rotation in degrees (clockwise)
radiusnumber | number[]-Border radius in pixels

Circle

Circle shape definition. Does not support rotation or border radius.

type Circle = {
x: number;
y: number;
size?: number;
origin?: {
mode?: "cell" | "self";
x?: number;
y?: number;
};
style?: { fillStyle?: string; strokeStyle?: string; lineWidth?: number };
};
PropertyTypeDefaultDescription
x, ynumberRequiredWorld coordinates
sizenumber1Diameter in grid units
originobject{ mode: "cell", x: 0.5, y: 0.5 }Anchor point
styleobject{}Fill and stroke styling

Text

Text element with position, content, and font styling. Extends the DrawObject pattern.

type Text = {
x: number;
y: number;
text: string;
size?: number;
origin?: {
mode?: "cell" | "self";
x?: number;
y?: number;
};
style?: {
fillStyle?: string;
fontFamily?: string;
textAlign?: CanvasTextAlign;
textBaseline?: CanvasTextBaseline;
};
rotate?: number;
};
PropertyTypeDefaultDescription
x, ynumberRequiredWorld coordinates
textstringRequiredText content to render
sizenumber1Font size in world units (scales with zoom)
originobject{ mode: "cell", x: 0.5, y: 0.5 }Anchor point
styleobject{}Font styling options
rotatenumber0Rotation in degrees (clockwise)

Style Options:

PropertyTypeDefaultDescription
fillStylestring-Text color
fontFamilystring"sans-serif"Font family
textAlignCanvasTextAlign"left"Horizontal alignment
textBaselineCanvasTextBaseline"top"Vertical baseline

Line

Line between two points. Style is passed separately to the draw method.

type Line = {
from: Coords;
to: Coords;
};
PropertyTypeDescription
fromCoordsStarting point
toCoordsEnding point

Path

Array of coordinates forming a continuous path.

type Path = Coords[];

Example:

const path: Path = [
{ x: 0, y: 0 },
{ x: 5, y: 0 },
{ x: 5, y: 5 },
{ x: 0, y: 5 },
];

ImageItem

Image element with position, size, and rotation.

type ImageItem = {
x: number;
y: number;
size?: number;
origin?: {
mode?: "cell" | "self";
x?: number;
y?: number;
};
rotate?: number;
img: HTMLImageElement;
};
PropertyTypeDefaultDescription
x, ynumberRequiredWorld coordinates
sizenumber1Size in grid units
originobject{ mode: "cell", x: 0.5, y: 0.5 }Anchor point
rotatenumber0Rotation in degrees (clockwise)
imgHTMLImageElementRequiredLoaded image object

Utility Types

Coords

Basic 2D coordinate object used throughout the engine.

type Coords = {
x: number;
y: number;
};

DrawObject

Base type that Rect extends. You can use this for generic drawing operations.

type DrawObject = {
x: number;
y: number;
size?: number;
origin?: {
mode?: "cell" | "self";
x?: number;
y?: number;
};
style?: { fillStyle?: string; strokeStyle?: string; lineWidth?: number };
rotate?: number;
radius?: number | number[];
};

Callback Types

MouseEventCallback

Callback for mouse events.

type MouseEventCallback = (
coords: { raw: Coords; snapped: Coords },
mouse: { raw: Coords; snapped: Coords },
client: { raw: Coords; snapped: Coords }
) => void;

type onClickCallback = MouseEventCallback;
type onRightClickCallback = MouseEventCallback;
type onHoverCallback = MouseEventCallback;
type onMouseDownCallback = MouseEventCallback;
type onMouseUpCallback = MouseEventCallback;
type onMouseLeaveCallback = MouseEventCallback;
ParameterDescription
coordsWorld coordinates (raw = exact, snapped = grid-aligned)
mouseMouse position relative to canvas
clientMouse position relative to viewport

onDrawCallback

Callback for custom drawing after all layers are rendered.

type onDrawCallback = (ctx: unknown, info: { scale: number; width: number; height: number; coords: Coords }) => void;
ParameterDescription
ctxRendering context (type depends on renderer being used)
info.scaleCurrent zoom scale
info.widthCanvas width in pixels
info.heightCanvas height in pixels
info.coordsTop-left world coordinate of the viewport

Example usage with Canvas2D renderer:

import { RendererCanvas } from "@canvas-tile-engine/renderer-canvas";

const engine = new CanvasTileEngine(wrapper, config, new RendererCanvas());

engine.onDraw = (ctx) => {
// Cast to the appropriate context type for your renderer
const context = ctx as CanvasRenderingContext2D;

context.fillStyle = "red";
context.fillRect(info.width / 2 - 5, info.height / 2 - 5, 10, 10);
};
tip

The ctx parameter is typed as unknown to support different renderer backends. When using RendererCanvas, cast it to CanvasRenderingContext2D.