import{_ as i,a,b as n,ag as t}from"./chunks/framework.BcrMLAmg.js";const o=JSON.parse('{"title":"Canvas Plugin Guide","description":"","frontmatter":{},"headers":[],"relativePath":"guides/canvas.md","filePath":"guides/canvas.md","lastUpdated":1745610868000}'),h={name:"guides/canvas.md"};function l(e,s,k,p,d,r){return n(),a("div",null,s[0]||(s[0]=[t(`
The Canvas plugin allows you to draw various shapes and text on your elements. It provides a simple yet powerful way to create custom graphics.
First, you need to get the canvas object:
local main = basalt.createFrame()
local canvas = main:getCanvas()Draw a line from point (x1,y1) to (x2,y2):
canvas:line(1, 1, 10, 10, "*", colors.red, colors.black) -- Draws a red lineCreate rectangles with specified dimensions:
canvas:rect(5, 5, 10, 5, "#", colors.blue, colors.white) -- Creates a blue rectangleDraw ellipses/circles:
canvas:ellipse(10, 10, 5, 3, "@", colors.green, colors.black) -- Creates an ellipseWrite text at specific coordinates:
canvas:text(1, 1, "Hello World!", colors.yellow, colors.black)Set background color at specific coordinates:
canvas:bg(1, 1, colors.blue)Set foreground color at specific coordinates:
canvas:fg(1, 1, colors.white)The canvas plugin supports two different render types that control when the canvas commands are executed:
canvas:setType(type)Parameters:
type: String, either "pre" or "post" "pre": Commands are executed before the main object rendering (default)"post": Commands are executed after the main object renderingExample:
-- Create a canvas that renders after the main object
local canvas = main:addCanvas()
canvas:setType("post")
-- Add commands that will now execute after main rendering
canvas:addCommand(function()
canvas:drawText(1, 1, "Overlay Text")
end)
-- or
canvas:line(1, 1, 10, 10, " ", colors.red, colors.green)Note: Changing the render type affects all subsequent commands added to the canvas. You can have multiple canvas objects with different render types on the same base object.
Canvas commands allow you to create custom drawing operations using low-level drawing functions. Available commands are:
textFg - Set text foreground colortextBg - Set text background colordrawText - Draw textdrawFg - Draw foreground colorsdrawBg - Draw background colorsblit - Draw text with foreground and background colorsmultiBlit - Draw multiple blit operations-- Example of proper command usage
canvas:addCommand(function()
-- Draw red text on black background
canvas:drawText(1, 1, "Hello")
canvas:drawFg(1, 1, colors.red)
canvas:drawBg(1, 1, colors.black)
-- Or use blit for more efficient drawing
canvas:blit(1, 2, "Hello", "fffff", "00000") -- white on black
end)The multiBlit function allows you to render text efficiently in a rectangular area:
canvas:addCommand(function()
-- Parameters: x, y, width, height, text, fg, bg
canvas:multiBlit(1, 1, 5, 3,
"HelloWorld!Hello", -- text (width * height characters)
"ffffffffff", -- fg colors (width * height characters)
"0000000000" -- bg colors (width * height characters)
)
end)Note: Do not use drawing functions like line, rect, or ellipse inside commands as this will cause render queue issues. Use the low-level drawing functions instead.