core.engine.draw_text

Client

Draws text within a bounding box on the canvas


Syntax

local status = core.engine.draw_text(
    text, 
    start_at, 
    end_at, 
    font, 
    font_size, 
    color = {1, 1, 1, 1}, 
    horizontal_alignment = core.engine.horizontal_alignment.LEFT,
    vertical_alignment = core.engine.vertical_alignment.TOP,
    clip = false, 
    wordwrap = false, 
    stroke = 0, 
    stroke_color = {1, 1, 1, 1}, 
    rotation = 0, 
    pivot = {0, 0}
)

Must be called within the "sandbox:draw" util.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 util.event.on("sandbox:draw", ...) and place all 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
core.engine.horizontal_alignmenthorizontal_alignmentHorizontal alignment of the text within the bounding box
core.engine.vertical_alignmentvertical_alignmentVertical alignment of the text within the bounding box
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 degrees
vector2pivotPivot point for rotation, relative to start_at

Returns

TypeNameDescription
boolstatustrue on successful execution, false otherwise

Examples

Draw simple left-aligned text
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local font = core.font.create("fonts/Roboto-Regular.ttf")

util.event.on("sandbox:draw", function()
    core.engine.draw_text(
        "Hello World",
        {center[1] - 150, center[2] - 250},
        {center[1] + 150, center[2] - 200},
        font,
        24
    )
end)
Draw centered text with a custom color
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local font = core.font.create("fonts/Roboto-Regular.ttf")

util.event.on("sandbox:draw", function()
    core.engine.draw_text(
        "Centered",
        {center[1] - 200, center[2] - 150},
        {center[1] + 200, center[2] - 90},
        font,
        32,
        {1, 1, 0, 1},
        core.engine.horizontal_alignment.CENTER,
        core.engine.vertical_alignment.CENTER
    )
end)
Draw word-wrapped text clipped to a bounding box
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local font = core.font.create("fonts/Roboto-Regular.ttf")

util.event.on("sandbox:draw", function()
    core.engine.draw_text(
        "This is a long string that will wrap inside its bounding box.",
        {center[1] - 400, center[2] - 20},
        {center[1] - 150, center[2] + 180},
        font,
        18,
        {1, 1, 1, 1},
        core.engine.horizontal_alignment.LEFT,
        core.engine.vertical_alignment.TOP,
        true,
        true
    )
end)
Draw text with an outline stroke
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local font = core.font.create("fonts/Roboto-Regular.ttf")

util.event.on("sandbox:draw", function()
    core.engine.draw_text(
        "Outlined",
        {center[1] + 100, center[2] - 20},
        {center[1] + 400, center[2] + 60},
        font,
        28,
        {1, 1, 1, 1},
        core.engine.horizontal_alignment.CENTER,
        core.engine.vertical_alignment.CENTER,
        false,
        false,
        2,
        {0, 0, 0, 1}
    )
end)
Draw rotated text around a pivot point
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local font = core.font.create("fonts/Roboto-Regular.ttf")

util.event.on("sandbox:draw", function()
    core.engine.draw_text(
        "Rotated",
        {center[1] - 100, center[2] + 120},
        {center[1] + 100, center[2] + 170},
        font,
        20,
        {1, 1, 1, 1},
        core.engine.horizontal_alignment.LEFT,
        core.engine.vertical_alignment.TOP,
        false,
        false,
        0,
        {1, 1, 1, 1},
        15,
        {0, 0}
    )
end)

On this page