Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Nightre/Rapid.js/llms.txt
Use this file to discover all available pages before exploring further.
MatrixStack is a high-performance hierarchical transform manager modelled after the Canvas 2D context’s save() / restore() API. It maintains both a local matrix (transforms applied at the current level) and a world matrix (the accumulated transform from all parent levels). MatrixStore is the underlying flat buffer that stores every 2D 3×3 matrix as a compact 6-element array [a, b, c, d, tx, ty] packed into a single Float32Array, maximising memory locality and minimising GC pressure.
Access MatrixStack via rapid.matrixStack and MatrixStore via rapid.matrix.
MatrixStack
MatrixStack is accessed through rapid.matrixStack. It uses MatrixStore internally and exposes a save/restore interface familiar from Canvas 2D.
rapid.clear() calls matrixStack.reset() at the start of every frame. You should not need to call reset() directly in normal usage.save()
CanvasRenderingContext2D.save().
Index of the newly allocated world matrix in
MatrixStore.Index of the newly allocated local matrix in
MatrixStore.The current step counter, usable with
updateMatrix().restore()
curWorldM and curLocalM to their pre-save() values. Equivalent to CanvasRenderingContext2D.restore(). Silently no-ops if the stack is empty.
translate(x, y)
Translation along the X axis in logical pixels.
Translation along the Y axis in logical pixels.
scale(scaleX, scaleY?)
Scale factor along the X axis.
Scale factor along the Y axis. Defaults to
scaleX for uniform scaling.rotate(radians)
Rotation angle in radians.
rotateWithOffset(radians, offsetX, offsetY)
(offsetX, offsetY), rotate by radians, translate by (-offsetX, -offsetY).
Rotation angle in radians.
X component of the pivot in local space.
Y component of the pivot in local space.
identity()
reset()
rapid.clear() every frame.
applyTransform(transform, width?, height?)
ITransformOptions object to the stack. If transform.saveTransform is true (the default), save() is called first. Handles x/y, position, rotation, scale, offsetX/offsetY, offset, and origin in the correct order.
The transform descriptor. See ITransformOptions for all fields.
Logical width used to resolve
origin anchor percentages. Default 0.Logical height used to resolve
origin anchor percentages. Default 0.updateMatrix(step)
The step index returned by
save(), or an object with a step property.localToWorld(x, y)
World-space coordinates.
worldToLocal(x, y)
Local-space coordinates.
getGlobalPosition()
(tx, ty) extracted directly from the current world matrix — the position the origin would appear at on screen.
World-space position.
getGlobalScale()
World-space scale factors.
getGlobalRotation()
Math.atan2.
World-space rotation in radians.
getTransform()
Float32Array [a, b, c, d, tx, ty].
6-element 2D transform array.
setTransform(f)
A 6-element
[a, b, c, d, tx, ty] transform array.toCSSMatrix()
matrix() string representing the current world matrix, suitable for use in element.style.transform.
CSS
matrix(a, b, c, d, tx, ty) string.MatrixStore
MatrixStore is accessed via rapid.matrix. It is a low-level flat-array container — each matrix is stored as 6 consecutive Float32Array elements [a, b, c, d, tx, ty] representing the upper 2×3 portion of a homogeneous 3×3 matrix. Matrices are referenced by an integer index returned at allocation time.
Most users interact with
MatrixStack rather than MatrixStore directly. Use MatrixStore when you need to maintain transform trees outside the main frame traversal, or when building custom scene-graph tooling.alloc()
Index of the new identity matrix.
allocDirty()
alloc() when you intend to overwrite all six values immediately (e.g. via copy).
Index of the new uninitialized matrix.
identity(index)
index to the 2D identity transform [1, 0, 0, 1, 0, 0].
reset()
MatrixStack.reset() every frame.
translate(index, x, y)
(x, y) into the matrix at index.
scale(index, scaleX, scaleY)
(sx, sy) into the matrix at index.
rotate(index, radians)
radians into the matrix at index.
rotateWithOffset(index, radians, offsetX, offsetY)
index around a local pivot (offsetX, offsetY) — equivalent to translate → rotate → translate-back.
copy(dst, src)
src into the matrix at dst.
multiply(dst, src)
dst by the matrix at src, storing the result back in dst. Equivalent to multiplyOut(dst, dst, src).
multiplyOut(out, aIdx, bIdx)
aIdx by matrix bIdx and stores the result in out. out may safely alias aIdx or bIdx.
invert(index)
index in-place. If the matrix is singular (non-invertible), falls back to identity.
transformPoint(index, x, y)
(x, y) by the matrix at index and returns the result.
Transformed point.
worldToLocal(index, x, y)
index (into a temporary slot) and transforms (x, y) through it, converting a world-space point to the local space of index. The temporary matrix is immediately freed.
Point in local space.
localToWorld(index, x, y)
(x, y) from the local space of index to world space — equivalent to transformPoint.
Point in world space.
getPosition(index)
(tx, ty) from the matrix at index.
Translation component.
getScale(index)
index.
Scale factors
{ x, y }.getRotation(index)
index using Math.atan2(b, a).
Rotation in radians.
toCSSMatrix(index)
matrix() string for the matrix at index.
CSS
matrix(a, b, c, d, tx, ty) string.ITransformOptions Interface
ITransformOptions is accepted by every draw method and by MatrixStack.applyTransform(). Properties are applied in this order: x/y → position → rotation → scale → offsetX/offsetY/offset → origin.
When
true (default), save() is called before applying any transforms and the caller is responsible for calling restore() (or using withTransform). Set to false to mutate the current matrix in-place without pushing a new stack frame.Optional callback executed immediately after the
save() call (if any), before transforms are applied. Useful for inserting additional state setup.Translation along the X axis in logical pixels.
Translation along the Y axis in logical pixels.
Translation as a
Vec2. Applied after x/y. Both may be used simultaneously — they are additive.Rotation in radians, applied after position.
Uniform scale when a
number; per-axis scale when a Vec2. Applied after rotation.Additional X translation applied after scale, in the already-scaled local space. Useful for moving the draw origin without affecting rotation pivot.
Additional Y translation applied after scale.
Additional translation as a
Vec2, summed with offsetX/offsetY.Normalised pivot point in
0–1 range relative to the sprite’s width / height. 0.5 centres the pivot. A Vec2 sets X and Y independently. The calculated pixel offset is subtracted from offsetX/offsetY.For example, origin: 0.5 on a 64×64 sprite shifts the draw origin by (-32, -32), so the sprite rotates around its centre.