see more...
The base Component class has a setText() method but it
gives you very limited control over appearance.
Tctxt solves this by accepting eleven constructor parameters that cover
every aspect of canvas text rendering โ and because it extends Component you
register it with display.add() exactly like any other game object.
The background fill draws a filled rectangle behind the text using the
paddingX and paddingY values to add space around it,
which is what makes HUD elements like score boxes look clean and readable against
any background colour.
Call scoreText.fixed() inside update() every frame to
lock the text to a fixed screen position even when the camera is scrolling โ
without this call the text scrolls with the world and falls off screen when the
player moves far enough.
// Tctxt(size, font, color, x, y, align, stroke, baseline, background, padX, padY)
const scoreText = new Tctxt(
"22px", // font size
"Arial", // font family
"white", // text colour
20, 36, // x, y position on screen
"left", // alignment: "left", "center", or "right"
false, // stroke mode โ false = fill, true = outline only
"alphabetic", // text baseline
"rgba(0,0,0,0.6)", // background fill colour (null to disable)
14, 6 // paddingX, paddingY around the text
);
scoreText.setText("Score: 0");
display.add(scoreText);
let score = 0;
function update() {
score++;
scoreText.setText("Score: " + Math.floor(score / 60));
// Keep the score box locked to the screen when the camera moves
scoreText.fixed();
}
๐ก fixed() works by adding the current camera offset to
the Component's stored aX / aY anchor position every
frame, keeping it at the same screen coordinates no matter where the camera is.
You must call it every frame โ not just once.
| Parameter | Example | Purpose |
| size | "22px" | Font size as a CSS string |
| font | "Arial" | Font family name |
| align | "left" | "left", "center", or "right" |
| stroke | false | false = filled text, true = outlined text only |
| background | "rgba(0,0,0,0.6)" | Background fill โ null disables it |
| padX / padY | 14, 6 | Pixel padding around the text inside the background |
setText(txt) | "Score: 0" | Update the displayed string โ returns the string |