Vital.sandbox
Engine

engine.draw_text

Client

Draws text within a bounding box on the canvas


Syntax

local status = engine.draw_text(
    text, 
    start_at, 
    end_at, 
    font, 
    font_size, 
    color = {1, 1, 1, 1}, 
    alignment = {"left", "top"}, 
    clip = false, 
    wordwrap = false, 
    stroke = 0, 
    stroke_color = {1, 1, 1, 1}, 
    rotation = 0, 
    pivot = {0, 0}
)

Must be called within the vital.sandbox:draw event. Invoking this function outside of that event will have no effect.

  • Inside the event — executes as expected, rendering text to the canvas each frame
  • Outside the event — the call will be silently ignored and nothing will be drawn
  • Recommended usage — register a handler via network:fetch("vital.sandbox:draw", true):on(...) and place all canvas draw calls inside it

Parameters

TypeNameDescription
stringtextText string to draw
vector2start_atTop-left corner of the bounding box
vector2end_atBottom-right corner of the bounding box
fontfontFont to render the text with
intfont_sizeFont size in points
colorcolorColor of the text
alignment[horizontal, vertical]alignmentHorizontal alignment:
"left" / 0
"center" / 1
"right" / 2
"fill" / 3

Vertical alignment:
"top" / 0
"center" / 1
"bottom" / 2
"fill" / 3
boolclipWhen true, text is clipped to the bounding box
boolwordwrapWhen true, text wraps at the bounding box width
intstrokeOutline thickness in pixels — set to 0 to disable
colorstroke_colorColor of the text outline
floatrotationRotation angle in radians
vector2pivotPivot point for rotation, relative to start_at

Returns

TypeNameDescription
boolstatustrue on successful execution, or false on failure

Examples

Draw text with various styles
local my_font = font.create("fonts/Roboto-Regular.ttf")

network:fetch("vital.sandbox:draw", true):on(function()
    --Draw simple text
    engine.draw_text("Hello World", {100, 100}, {400, 150}, my_font, 24)

    --Draw centered text with a custom color
    engine.draw_text("Centered", {100, 100}, {500, 200}, my_font, 32, {1, 1, 0, 1}, {"center", "center"})

    --Draw word-wrapped text clipped to its box
    engine.draw_text("This is a long string that will wrap.", {50, 50}, {300, 300}, my_font, 18, {1, 1, 1, 1}, {"left", "top"}, true, true)

    --Draw text with an outline
    engine.draw_text("Outlined", {100, 100}, {400, 160}, my_font, 28, {1, 1, 1, 1}, {"center", "center"}, false, false, 2, {0, 0, 0, 1})

    --Draw rotated text
    engine.draw_text("Rotated", {200, 200}, {400, 250}, my_font, 20, {1, 1, 1, 1}, {"left", "top"}, false, false, 0, {1, 1, 1, 1}, math.pi/6, {0, 0})
end)

On this page