tiny-engine includes a minimal math library centered onDocumentation 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.
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
The x-coordinate (horizontal axis).
The y-coordinate (vertical axis).
Properties
The x-component of the vector. Mutable.
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).
Vector2.ONE
Returns a new Vector2(1, 1).
Vector2.fromJSON(position)
Creates a Vector2 from a Position object ({ x, y }).
Vector2.vectorize(vectorLike)
Converts any VectorLike value to a Vector2. See VectorLike below.
Instance methods
All mutating methods returnthis 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.
.toAdded(arg)
Returns a new Vector2 that is this vector plus arg. The original is unchanged.
.subtract(arg)
Subtracts another Vector2 or a scalar in place.
.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.
.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.
.clone()
Returns a new Vector2 with the same x and y.
.equals(vector2)
Returns true if both components are equal.
.apply(fn) / .toApplied(fn)
Applies a function to each component in place (or to a clone).
.toJSON()
Returns a plain { x, y } Position object, suitable for serialisation.
VectorLike
VectorLike is accepted by every engine API that receives a position or size. Pass whichever form is most convenient:
| Form | Example |
|---|---|
Vector2 instance | new Vector2(10, 20) |
Position object | { x: 10, y: 20 } |
| Two-element tuple | [10, 20] |
| Scalar (sets both axes) | 5 → { x: 5, y: 5 } |
Vector2.vectorize(value) to normalise any VectorLike into a Vector2 instance.
Position
Vector2.toJSON() and as a serialisation-friendly alternative to Vector2 when you do not need vector methods.
Color
number in the range 0 (none) to 1 (full). Used by fill, stroke, and tint properties across all visual nodes.
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.
The absolute or relative URL of the image to load.
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.