🖋️ drawText(text, x, y, options)

📖 Description

Quickly draws text on screen with advanced styling options (color, font, alignment, outline, shadow, etc.) and returns an object containing all information about the rendered text, including its size and a hit-detection function (contains()).

🧠 Syntax

let info = drawText(text, x, y, options);

📥 Parameters

Name Type Description Default
textstringText to display.
xnumberHorizontal screen position.
ynumberVertical screen position.
optionsobjectVisual styling options (see table below).{}

🎨 options Object Properties

PropertyTypeDescriptionDefault
colorstringText color."#fff"
fontstringFont and size (e.g. "20px Arial")."20px Arial"
alignstringAlignment ("left", "center", "right")."left"
strokestringText outline color.null
lineWidthnumberOutline thickness.2
shadowColorstringShadow color.null
shadowBlurnumberShadow blur amount.0
shadowOffsetXnumberHorizontal shadow offset.2
shadowOffsetYnumberVertical shadow offset.2

🔁 Return Value

Returns an object containing:

PropertyTypeDescription
textstringThe rendered text.
x, ynumberScreen position.
width, heightnumberAutomatically calculated text dimensions.
color, font, align, stroke...variousAll applied style properties.
contains(px, py)functionReturns true if point (px, py) is inside the text area.

✅ Basic Example

function LateUpdate(obj, dt) {
  drawText("Hello GameCrom!", 250, 250);
}

🌈 Example with Styles & Detection

function LateUpdate(obj, dt) {
  const info = drawText("LEVEL UP!", 250, 250, {
    color: "#ff0",
    font: "bold 36px 'Press Start 2P'",
    align: "center",
    stroke: "#000",
    lineWidth: 4,
    shadowColor: "rgba(0,0,0,0.6)",
    shadowBlur: 8
  });

  // Detect if mouse is over the text
  if (info.contains(mouseX, mouseY)) {
    drawText("🖱️ Mouse Over!", 250, 290, { color: "#0f0", align: "center" });
  }
}

🧠 Technical Details

💡 Tip:
Use drawText() for HUDs, temporary messages, score displays or custom interfaces. The returned object allows you to detect clicks, show tooltips or animate text easily.