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.
TextTexture is Rapid.js’s built-in text renderer. It draws text onto a hidden HTML5 <canvas> element using the 2D Canvas API, then uploads the result to a WebGL texture that you display with drawSprite. This approach gives you access to the full browser font stack — system fonts, web fonts, variable weights, custom baselines — without any dependency on a bitmap font atlas or signed distance field pre-processing step.
Creating a TextTexture
Callrapid.texture.createTextTexture() and pass an ITextOptions object. The texture is created synchronously: the canvas is sized, text is drawn, and the GPU upload happens immediately in the constructor.
ITextStyle Options
All style properties are optional. Rapid merges your values with the defaults shown below.CSS font family string passed directly to the 2D Canvas context. Supports stacks such as
"Georgia, serif". Default: "Arial".Font size in logical pixels. The canvas is rendered at the device pixel ratio (
window.devicePixelRatio) so text stays sharp on high-DPI screens. Default: 24.CSS font weight, e.g.
"normal", "bold", "700". Default: "normal".The fill style for the text glyphs. Accepts any value valid for
CanvasRenderingContext2D.fillStyle. Default: "#000000".The stroke style drawn behind the fill. Only rendered when
strokeThickness is greater than 0. Leave unset to skip stroking.Stroke line width in logical pixels. A value of
0 disables the stroke. Default: 1.Horizontal alignment of the text within the texture. This also controls the
offsetX of the texture so that the draw position (x, y) acts as the intended anchor. Default: "left".Vertical alignment anchor. Accepted values are
"top", "middle", "bottom", "alphabetic", "hanging", and "ideographic". This controls the offsetY of the texture. Default: "top".ITextOptions also accepts an initial text string and any ITextureOptions fields (textureFilter, wrap, premultipliedAlpha, key).
Drawing Text
ATextTexture is a Texture subclass, so you draw it with drawSprite exactly as you would any other image:
rotation, scale, origin, etc.) work normally.
Updating Text and Style at Runtime
You can change the content or appearance of aTextTexture after creation by assigning to the .text and .style setters. Either assignment internally re-renders the canvas and re-uploads the texture to the GPU. The canvas is only resized when the new text requires a different pixel size, making small updates efficient.
Multi-line Text
Use\n in the text string to break text into multiple lines. TextTexture splits on newlines, measures each line independently, and stacks them vertically with the correct line height derived from the font metrics.
Anchor Behavior: align and baseline
Thealign and baseline style options do more than control how glyphs are laid out inside the texture — they also shift the texture’s offsetX and offsetY so that the (x, y) you pass to drawSprite becomes the logical anchor of the text, not its top-left pixel corner.
| align | Effect on offsetX |
|---|---|
"left" | offsetX = 0 (anchor at left edge) |
"center" | offsetX = -width / 2 (anchor at horizontal center) |
"right" | offsetX = -width (anchor at right edge) |
| baseline | Effect on offsetY |
|---|---|
"top" / others | offsetY = 0 (anchor at top edge) |
"middle" | offsetY = -height / 2 (anchor at vertical center) |
"bottom" | offsetY = -height (anchor at bottom edge) |
center/middle text label at (400, 300), the rendered text will be visually centered on that coordinate — which is the expected behavior for HUD elements and dialogue boxes.
Texture Filter Mode
By default Rapid usesTextureFilterMode.NEAREST, which is great for pixel-art sprites but produces jagged edges on text. Set textureFilter to TextureFilterMode.LINEAR when creating a TextTexture for cleaner, anti-aliased rendering.
Full Example
The pattern below comes directly from the Rapid.js text demo. It shows how to create aRapid instance configured for text, build multiple styled TextTexture objects up front, and update one dynamically each frame:
- Rapid is initialized with
textureFilter: TextureFilterMode.LINEARso all text textures get smooth sampling without per-texture configuration. - A factory function (
makeText) holds the shared default style, keeping individualcreateTextTexturecalls concise. - The dynamic label is updated at 10 Hz (
Math.floor(time * 10)bucket throttle) rather than every frame, which halves GPU texture upload frequency with no visible difference at 60 fps. rapid.flush()is called at the end of the loop to ensure the final batch is submitted before the frame is presented.