Skip to main content
Version: latest - a1.172.0 โš–๏ธ

๐Ÿ–ผ๏ธ Canvas

Canvas is an entity which you can draw onto it

๐Ÿ‘ชInheritance
This class shares methods and events from Base Entity.

๐ŸŽ’ Examplesโ€‹

Client/Index.lua
-- Spawns a Canvas
local my_canvas = Canvas(
true,
Color.TRANSPARENT,
0,
true
)

-- Subscribes for Update, we can only Draw inside this event
my_canvas:Subscribe("Update", function(self, width, height)
-- Draws a Text in the middle of the screen
self:DrawText("Hello World!", Vector2D(width / 2, height / 2))

-- Draws a red line Horizontally
self:DrawLine(Vector2D(0, height / 2), Vector2D(width, height / 2), 10, Color.RED)
end)

-- Forces the canvas to repaint, this will make it trigger the Update event
my_canvas:Repaint()

-- Applies the Canvas as the material of a (client side) StaticMesh
local my_mesh = StaticMesh(Vector(0, 0, 100), Rotator(), "nanos-world::SM_Cube")
my_mesh:SetMaterialFromCanvas(my_canvas)
tip

You can use the output Texture from a Canvas with :SetMaterialFromCanvas() method!

๐Ÿ›  Constructorsโ€‹

Default Constructorโ€‹

No description provided

local my_canvas = Canvas(is_visible?, clear_color?, auto_repaint_rate?, should_clear_before_update?, auto_resize?, width?, height?, screen_position?)
Parameters
NameTypeDescription
is_visible?booleanWhether to draw it on screen Default true
clear_color?ColorColor to clear with (background color) Default Color.TRANSPARENT
auto_repaint_rate?floatRate to auto repaint (call Update event), pass 0 for every frame, -1 to disable Default -1
should_clear_before_update?booleanWhether to clear with Clear Color before updates Default true
auto_resize?booleanAuto resize with screen's size Default true
width?integerIf not using auto_resize Default 0
height?integerIf not using auto_resize Default 0
screen_position?Vector2DIf not using auto_resize, offset when drawing to screen Default Vector2D(0, 0)

๐Ÿ—ฟ Static Functionsโ€‹

This class doesn't have own static functions.

Inherited static functions

๐Ÿฆ  Functionsโ€‹

Drawing
DrawBoxDraws an unfilled box on the Canvas
DrawLineDraws a line on the Canvas
DrawMaterialDraws a Material on the Canvas
DrawMaterialFromSceneCaptureDraws a SceneCapture on the Canvas
DrawMaterialFromWebUIDraws a WebUI on the Canvas
DrawPolygonDraws a N-Polygon on the Canvas
DrawRectDraws a filled Rect on the Canvas
DrawTextDraws a Text on the Canvas
DrawTextureDraws a Texture on the Canvas
Layout
GetSize: Vector2DGets the Canvas Size
ResizeResizes the Canvas if not using auto_resize
SetAutoResizeSets if the canvas should auto resize to screen size
SetScreenPositionSets the Canvas Screen Position offset
SetVisibilitySets if it's visible on screen
Rendering
ClearClear the Canvas with a specific Color
RepaintForces the repaint
SetAutoRepaintRateSets the repaint rate

Drawingfunctionsโ€‹

DrawBoxModerateโ€‹

Draws an unfilled box on the Canvas

This method can only be called from inside Update event

my_canvas:DrawBox(screen_position, screen_size, thickness, render_color?, blend_mode?)
Parameters
NameTypeDescription
screen_positionVector2DPosition on the Canvas, in pixels
screen_sizeVector2DSize on the Canvas, in pixels
thicknessfloatLine thickness
render_color?ColorColor to tint the drawing with Default Color.WHITE
blend_mode?BlendModeHow it blends with what is already drawn Default BlendMode.Opaque

DrawLineModerateโ€‹

Draws a line on the Canvas

This method can only be called from inside Update event

my_canvas:DrawLine(screen_position_a, screen_position_b, thickness, render_color, blend_mode?)
Parameters
NameTypeDescription
screen_position_aVector2DStart position of the line, in pixels
screen_position_bVector2DEnd position of the line, in pixels
thicknessfloatLine thickness
render_colorColorColor to tint the drawing with
blend_mode?BlendModeHow it blends with what is already drawn Default BlendMode.Opaque

DrawMaterialModerateโ€‹

Draws a Material on the Canvas

This method can only be called from inside Update event

Note: Due how Unreal handles Render Targets, drawing material on Canvas result on a weird translucent effect. Hope in the future to be improved

my_canvas:DrawMaterial(material_path, screen_position, screen_size, coordinate_position, coordinate_size?, rotation?, pivot_point?, blend_mode?)
Parameters
NameTypeDescription
material_pathMaterial ReferenceThe Material asset to use
screen_positionVector2DPosition on the Canvas, in pixels
screen_sizeVector2DSize on the Canvas, in pixels
coordinate_positionVector2DTop-left UV coordinate of the source to draw (0 to 1)
coordinate_size?Vector2DUV size of the source to draw (0 to 1), use (1, 1) for the whole source Default Vector2D(1, 1)
rotation?floatRotation in degrees Default 0
pivot_point?Vector2DPivot of the rotation, relative to the drawing size (0.5, 0.5 is the center) Default Vector2D(0.5, 0.5)
blend_mode?BlendModeHow it blends with what is already drawn Default BlendMode.Opaque

DrawMaterialFromSceneCaptureModerateโ€‹

Draws a SceneCapture on the Canvas

This method can only be called from inside Update event

Note: Due how Unreal handles Render Targets, drawing material on Canvas result on a weird translucent effect. Hope in the future to be improved

my_canvas:DrawMaterialFromSceneCapture(scenecapture_entity, screen_position, screen_size, coordinate_position, coordinate_size?, rotation?, pivot_point?, blend_mode?)
Parameters
NameTypeDescription
scenecapture_entitySceneCaptureThe SceneCapture to draw
screen_positionVector2DPosition on the Canvas, in pixels
screen_sizeVector2DSize on the Canvas, in pixels
coordinate_positionVector2DTop-left UV coordinate of the source to draw (0 to 1)
coordinate_size?Vector2DUV size of the source to draw (0 to 1), use (1, 1) for the whole source Default Vector2D(1, 1)
rotation?floatRotation in degrees Default 0
pivot_point?Vector2DPivot of the rotation, relative to the drawing size (0.5, 0.5 is the center) Default Vector2D(0.5, 0.5)
blend_mode?BlendModeHow it blends with what is already drawn Default BlendMode.Opaque

DrawMaterialFromWebUIModerateโ€‹

Draws a WebUI on the Canvas

This method can only be called from inside Update event

Note: Due how Unreal handles Render Targets, drawing material on Canvas result on a weird translucent effect. Hope in the future to be improved

my_canvas:DrawMaterialFromWebUI(webui_entity, screen_position, screen_size, coordinate_position, coordinate_size?, rotation?, pivot_point?, blend_mode?)
Parameters
NameTypeDescription
webui_entityWebUIThe WebUI to draw
screen_positionVector2DPosition on the Canvas, in pixels
screen_sizeVector2DSize on the Canvas, in pixels
coordinate_positionVector2DTop-left UV coordinate of the source to draw (0 to 1)
coordinate_size?Vector2DUV size of the source to draw (0 to 1), use (1, 1) for the whole source Default Vector2D(1, 1)
rotation?floatRotation in degrees Default 0
pivot_point?Vector2DPivot of the rotation, relative to the drawing size (0.5, 0.5 is the center) Default Vector2D(0.5, 0.5)
blend_mode?BlendModeHow it blends with what is already drawn Default BlendMode.Opaque

DrawPolygonModerateโ€‹

Draws a N-Polygon on the Canvas

This method can only be called from inside Update event

my_canvas:DrawPolygon(texture_path, screen_position, radius?, number_of_sides?, render_color?, blend_mode?)
Parameters
NameTypeDescription
texture_pathImage PathPass empty to use default white Texture
screen_positionVector2DPosition on the Canvas, in pixels
radius?Vector2DRadius of the polygon on each axis, in pixels Default Vector2D(1, 1)
number_of_sides?integerHow many sides the polygon has Default 3
render_color?ColorColor to tint the drawing with Default Color.WHITE
blend_mode?BlendModeHow it blends with what is already drawn Default BlendMode.Opaque

DrawRectModerateโ€‹

Draws a filled Rect on the Canvas

Draws a fille Rect on the Canvas

This method can only be called from inside Update event

my_canvas:DrawRect(texture_path, screen_position, screen_size, render_color?, blend_mode?)
Parameters
NameTypeDescription
texture_pathImage PathPass empty to use default white Texture
screen_positionVector2DPosition on the Canvas, in pixels
screen_sizeVector2DSize on the Canvas, in pixels
render_color?ColorColor to tint the drawing with Default Color.WHITE
blend_mode?BlendModeHow it blends with what is already drawn Default BlendMode.Opaque

DrawTextModerateโ€‹

Draws a Text on the Canvas

This method can only be called from inside Update event

Shadow and Outline won't work properly with Transparent clear_color

my_canvas:DrawText(text, screen_position, font_type?, font_size?, text_color?, kerning?, center_x?, center_y?, shadow_color?, shadow_offset?, outlined?, outline_color?)
Parameters
NameTypeDescription
textstringThe text to draw
screen_positionVector2DPosition on the Canvas, in pixels
font_type?FontTypeFont to use Default FontType.Roboto
font_size?integerFont size Default 12
text_color?ColorColor of the text Default Color.WHITE
kerning?floatExtra spacing between characters Default 0
center_x?booleanWhether to center the text horizontally on the position Default false
center_y?booleanWhether to center the text vertically on the position Default false
shadow_color?ColorColor of the text shadow, transparent to disable it Default Color.TRANSPARENT
shadow_offset?Vector2DOffset of the shadow, in pixels Default Vector2D(1, 1)
outlined?booleanWhether to draw an outline around the text Default false
outline_color?ColorColor of the outline Default Color.BLACK

DrawTextureModerateโ€‹

Draws a Texture on the Canvas

This method can only be called from inside Update event

my_canvas:DrawTexture(texture_path, screen_position, screen_size, coordinate_position, coordinate_size?, render_color?, blend_mode?, rotation?, pivot_point?)
Parameters
NameTypeDescription
texture_pathImage PathThe image to draw
screen_positionVector2DPosition on the Canvas, in pixels
screen_sizeVector2DSize on the Canvas, in pixels
coordinate_positionVector2DTop-left UV coordinate of the source to draw (0 to 1)
coordinate_size?Vector2DUV size of the source to draw (0 to 1), use (1, 1) for the whole source Default Vector2D(1, 1)
render_color?ColorColor to tint the drawing with Default Color.WHITE
blend_mode?BlendModeAlphaBlend is recommended for textures with transparency. Otherwise you may use Opaque Default BlendMode.AlphaBlend
rotation?floatRotation in degrees Default 0
pivot_point?Vector2DPivot of the rotation, relative to the drawing size (0.5, 0.5 is the center) Default Vector2D(0.5, 0.5)

Layoutfunctionsโ€‹

GetSizeOptimalโ€‹

Gets the Canvas Size

local size = my_canvas:GetSize()
Returns
TypeDescription
Vector2Dthe current size

ResizeModerateโ€‹

Resizes the Canvas if not using auto_resize

my_canvas:Resize(width, height)
Parameters
NameTypeDescription
widthintegerWidth in pixels
heightintegerHeight in pixels

SetAutoResizeOptimalโ€‹

Sets if the canvas should auto resize to screen size

my_canvas:SetAutoResize(auto_resize)
Parameters
NameTypeDescription
auto_resizebooleanWhether the canvas should auto resize to screen size

SetScreenPositionOptimalโ€‹

Sets the Canvas Screen Position offset

my_canvas:SetScreenPosition(screen_position)
Parameters
NameTypeDescription
screen_positionVector2DThe new Canvas Screen Position offset

SetVisibilityOptimalโ€‹

Sets if it's visible on screen

my_canvas:SetVisibility(visible)
Parameters
NameTypeDescription
visiblebooleanWhether it's visible on screen

Renderingfunctionsโ€‹

ClearOptimalโ€‹

Clear the Canvas with a specific Color

my_canvas:Clear(clear_color)
Parameters
NameTypeDescription
clear_colorColorThe color to fill the Canvas with

RepaintModerateโ€‹

Forces the repaint

This triggers the Update event

my_canvas:Repaint()

SetAutoRepaintRateOptimalโ€‹

Sets the repaint rate

Sets it to -1 to stop auto repainting or 0 to repaint every frame

my_canvas:SetAutoRepaintRate(auto_repaint_rate)
Parameters
NameTypeDescription
auto_repaint_ratefloatRepaint interval in seconds, 0 to repaint every frame or -1 to stop auto repainting

๐Ÿš€ Eventsโ€‹

UpdateCalled when the Canvas needs to be painted

You can only call :Draw...() methods from inside this event

Inherited events

From Entity4 events

Updateโ€‹

Called when the Canvas needs to be painted

You can only call :Draw...() methods from inside this event

Canvas.Subscribe("Update", function(self, width, height)
-- Update was called
end)
Arguments
NameTypeDescription
selfCanvasThe Canvas that triggered the event
widthintegerWidth in pixels
heightintegerHeight in pixels