core.engine.draw_rectangle

Client

Draws a filled rectangle on the canvas


Syntax

local status = core.engine.draw_rectangle(
    position, 
    size, 
    color = {1, 1, 1, 1}, 
    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 the filled rectangle 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
vector2positionTop-left corner of the rectangle
vector2sizeWidth and height of the rectangle
colorcolorFill color of the rectangle
floatstrokeOutline thickness in pixels
Set to 0 to disable
colorstroke_colorColor of the outline
floatrotationRotation angle in degrees
vector2pivotPivot point for rotation, relative to the rectangle's position

Returns

TypeNameDescription
boolstatustrue on successful execution, false otherwise

Examples

Draw a plain white rectangle
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local size = {200, 100}

util.event.on("sandbox:draw", function()
    core.engine.draw_rectangle(
        {center[1] - size[1]*0.5, center[2] - 200},
        size
    )
end)
Draw a filled rectangle with an outline
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local size = {200, 100}

util.event.on("sandbox:draw", function()
    core.engine.draw_rectangle(
        {center[1] - size[1]*0.5, center[2] - size[2]*0.5},
        size,
        {0, 1, 0, 1},
        2,
        {0, 0, 0, 1}
    )
end)
Draw a rectangle rotated around its center
local resolution = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local size = {200, 100}

util.event.on("sandbox:draw", function()
    core.engine.draw_rectangle(
        {center[1] - size[1]*0.5, center[2] + 120},
        size,
        {1, 0.5, 0, 1},
        0,
        {1, 1, 1, 1},
        30,
        {size[1]*0.5, size[2]*0.5}
    )
end)

On this page