Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sanchedev/tiny-engine/llms.txt

Use this file to discover all available pages before exploring further.

tiny-engine includes a minimal math library centered on Vector2. Alongside the vector class, the library exports the VectorLike union type (accepted everywhere a position or size is needed), the Color RGBA tuple, and the Position plain-object interface. All math exports are available directly from 'tiny-engine'.

Vector2

A mutable 2D vector used throughout the engine for positions, sizes, velocities, and directions.

Constructor

import { Vector2 } from 'tiny-engine'

const pos = new Vector2(10, 20)
x
number
required
The x-coordinate (horizontal axis).
y
number
required
The y-coordinate (vertical axis).

Properties

.x
number
The x-component of the vector. Mutable.
.y
number
The y-component of the vector. Mutable.

Static factories

Vector2.ZERO

Returns a new Vector2(0, 0) each time it is accessed (a fresh instance, not a singleton).
const origin = Vector2.ZERO  // Vector2 { x: 0, y: 0 }

Vector2.ONE

Returns a new Vector2(1, 1).
const unit = Vector2.ONE  // Vector2 { x: 1, y: 1 }

Vector2.fromJSON(position)

Creates a Vector2 from a Position object ({ x, y }).
const v = Vector2.fromJSON({ x: 5, y: 3 })

Vector2.vectorize(vectorLike)

Converts any VectorLike value to a Vector2. See VectorLike below.

Instance methods

All mutating methods return this for chaining. Use the to* variants for non-mutating (cloned) equivalents.

.add(arg)

Adds another Vector2 or a scalar number to x and y in place.
const v = new Vector2(1, 2)
v.add(new Vector2(3, 4))  // { x: 4, y: 6 }
v.add(10)                 // { x: 14, y: 16 }

.toAdded(arg)

Returns a new Vector2 that is this vector plus arg. The original is unchanged.
const a = new Vector2(1, 2)
const b = a.toAdded(new Vector2(10, 10))  // { x: 11, y: 12 }
// a is still { x: 1, y: 2 }

.subtract(arg)

Subtracts another Vector2 or a scalar in place.
new Vector2(5, 8).subtract(new Vector2(1, 2))  // { x: 4, y: 6 }
new Vector2(10, 10).subtract(3)                 // { x: 7, y: 7 }

.toSubtracted(arg)

Returns a new Vector2 equal to this minus arg.

.multiply(arg)

Multiplies component-wise by another Vector2, or multiplies both components by a scalar in place.
new Vector2(2, 3).multiply(new Vector2(4, 5))  // { x: 8, y: 15 }
new Vector2(2, 3).multiply(2)                  // { x: 4, y: 6 }

.toMultiplied(arg)

Returns a new Vector2 equal to this times arg.

.normalize()

Scales this vector to unit length (magnitude = 1) in place. If the vector is (0, 0), it is left unchanged.
const dir = new Vector2(3, 4)
dir.normalize()
// dir.x ≈ 0.6, dir.y ≈ 0.8  (magnitude is 1)

.clone()

Returns a new Vector2 with the same x and y.
const original = new Vector2(5, 10)
const copy = original.clone()
copy.x = 99
// original.x is still 5

.equals(vector2)

Returns true if both components are equal.
new Vector2(1, 2).equals(new Vector2(1, 2))  // true

.apply(fn) / .toApplied(fn)

Applies a function to each component in place (or to a clone).
// Clamp both axes to [0, 100]
v.apply((coord) => Math.min(Math.max(coord, 0), 100))

.toJSON()

Returns a plain { x, y } Position object, suitable for serialisation.

VectorLike

type VectorLike = Vector2 | Position | [x: number, y: number] | number
VectorLike is accepted by every engine API that receives a position or size. Pass whichever form is most convenient:
FormExample
Vector2 instancenew Vector2(10, 20)
Position object{ x: 10, y: 20 }
Two-element tuple[10, 20]
Scalar (sets both axes)5{ x: 5, y: 5 }
Use Vector2.vectorize(value) to normalise any VectorLike into a Vector2 instance.
Prefer the [x, y] tuple shorthand for static positions and sizes — it is more concise than new Vector2(x, y) and zero-cost at runtime since the engine converts it automatically.
// These are equivalent
position={new Vector2(64, 32)}
position={[64, 32]}

Position

interface Position {
  x: number
  y: number
}
A plain object representing a 2D point. Used as the return type of Vector2.toJSON() and as a serialisation-friendly alternative to Vector2 when you do not need vector methods.

Color

type Color = [number, number, number, number]
An RGBA color tuple where each channel is a number in the range 0 (none) to 1 (full). Used by fill, stroke, and tint properties across all visual nodes.
import type { Color } from 'tiny-engine'

const red: Color            = [1, 0, 0, 1]
const green: Color          = [0, 1, 0, 1]
const semiTransparentBlue: Color = [0, 0, 1, 0.5]
const white: Color          = [1, 1, 1, 1]
const invisible: Color      = [0, 0, 0, 0]

Asset utilities

loadTexture(url)

Loads an image from a URL, caches it, and returns a symbol that acts as a stable texture ID. If the same URL is requested again, the cached entry is returned without re-fetching.
import { loadTexture } from 'tiny-engine'

// Load during scene setup — await before the scene goes live
const heroTextureId = await loadTexture('/assets/hero.png')

// Use the symbol wherever a texture ID is expected
url
string
required
The absolute or relative URL of the image to load.
Returns: Promise<symbol> — a unique symbol that identifies the loaded texture.

getTexture(id)

Retrieves a loaded Texture instance by its symbol ID. Throws TextureNotFoundError if the ID has not been loaded.
import { getTexture } from 'tiny-engine'

const texture = getTexture(heroTextureId)
console.log(texture.width, texture.height)

Code examples

import { Vector2 } from 'tiny-engine'
import type { Color, VectorLike } from 'tiny-engine'

// Basic arithmetic
const velocity = new Vector2(100, 0)
const gravity  = new Vector2(0, 9.8)

velocity.add(gravity)
// { x: 100, y: 9.8 }

// Non-mutating
const next = velocity.toAdded(new Vector2(0, 9.8))

// Normalize a direction vector
const dir = new Vector2(3, 4)
dir.normalize()
// { x: 0.6, y: 0.8 }

// Colors
const orange: Color = [1, 0.5, 0, 1]
const translucent: Color = [1, 1, 1, 0.4]

// VectorLike shorthand
function moveNode(offset: VectorLike) {
  node.position = Vector2.vectorize(offset)
}

moveNode([5, 10])     // tuple form
moveNode(3)           // scalar — both axes set to 3
moveNode({ x: 5, y: 10 }) // Position form

Build docs developers (and LLMs) love