CanvasItem

Inherits: Node < Object

Inherited By: Control, Node2D

Base class of anything 2D.

Description

Base class of anything 2D. Canvas items are laid out in a tree; children inherit and extend their parent's transform. CanvasItem is extended by Control for anything GUI-related, and by Node2D for anything related to the 2D engine.

Any CanvasItem can draw. For this, update is called by the engine, then NOTIFICATION_DRAW will be received on idle time to request redraw. Because of this, canvas items don't need to be redrawn on every frame, improving the performance significantly. Several functions for drawing on the CanvasItem are provided (see draw_* functions). However, they can only be used inside _draw, its corresponding Object._notification or methods connected to the draw signal.

Canvas items are drawn in tree order. By default, children are on top of their parents so a root CanvasItem will be drawn behind everything. This behavior can be changed on a per-item basis.

A CanvasItem can also be hidden, which will also hide its children. It provides many ways to change parameters such as modulation (for itself and its children) and self modulation (only for itself), as well as its blend mode.

Ultimately, a transform notification can be requested, which will notify the node that its global position changed in case the parent tree changed.

Note: Unless otherwise specified, all methods that have angle parameters must have angles specified as radians. To convert degrees to radians, use @GDScript.deg2rad.

Tutorials

Properties

int

light_mask

1

Material

material

Color

modulate

Color( 1, 1, 1, 1 )

Color

self_modulate

Color( 1, 1, 1, 1 )

bool

show_behind_parent

false

bool

show_on_top

bool

use_parent_material

false

bool

visible

true

Methods

void

_draw ( ) virtual

void

draw_arc ( Vector2 center, float radius, float start_angle, float end_angle, int point_count, Color color, float width=1.0, bool antialiased=false )

float

draw_char ( Font font, Vector2 position, String char, String next, Color modulate=Color( 1, 1, 1, 1 ) )

void

draw_circle ( Vector2 position, float radius, Color color )

void

draw_colored_polygon ( PoolVector2Array points, Color color, PoolVector2Array uvs=PoolVector2Array( ), Texture texture=null, Texture normal_map=null, bool antialiased=false )

void

draw_line ( Vector2 from, Vector2 to, Color color, float width=1.0, bool antialiased=false )

void

draw_mesh ( Mesh mesh, Texture texture, Texture normal_map=null, Transform2D transform=Transform2D( 1, 0, 0, 1, 0, 0 ), Color modulate=Color( 1, 1, 1, 1 ) )

void

draw_multiline ( PoolVector2Array points, Color color, float width=1.0, bool antialiased=false )

void

draw_multiline_colors ( PoolVector2Array points, PoolColorArray colors, float width=1.0, bool antialiased=false )

void

draw_multimesh ( MultiMesh multimesh, Texture texture, Texture normal_map=null )

void

draw_polygon ( PoolVector2Array points, PoolColorArray colors, PoolVector2Array uvs=PoolVector2Array( ), Texture texture=null, Texture normal_map=null, bool antialiased=false )

void

draw_polyline ( PoolVector2Array points, Color color, float width=1.0, bool antialiased=false )

void

draw_polyline_colors ( PoolVector2Array points, PoolColorArray colors, float width=1.0, bool antialiased=false )

void

draw_primitive ( PoolVector2Array points, PoolColorArray colors, PoolVector2Array uvs, Texture texture=null, float width=1.0, Texture normal_map=null )

void

draw_rect ( Rect2 rect, Color color, bool filled=true, float width=1.0, bool antialiased=false )

void

draw_set_transform ( Vector2 position, float rotation, Vector2 scale )

void

draw_set_transform_matrix ( Transform2D xform )

void

draw_string ( Font font, Vector2 position, String text, Color modulate=Color( 1, 1, 1, 1 ), int clip_w=-1 )

void

draw_style_box ( StyleBox style_box, Rect2 rect )

void

draw_texture ( Texture texture, Vector2 position, Color modulate=Color( 1, 1, 1, 1 ), Texture normal_map=null )

void

draw_texture_rect ( Texture texture, Rect2 rect, bool tile, Color modulate=Color( 1, 1, 1, 1 ), bool transpose=false, Texture normal_map=null )

void

draw_texture_rect_region ( Texture texture, Rect2 rect, Rect2 src_rect, Color modulate=Color( 1, 1, 1, 1 ), bool transpose=false, Texture normal_map=null, bool clip_uv=true )

void

force_update_transform ( )

RID

get_canvas ( ) const

RID

get_canvas_item ( ) const

Transform2D

get_canvas_transform ( ) const

Vector2

get_global_mouse_position ( ) const

Transform2D

get_global_transform ( ) const

Transform2D

get_global_transform_with_canvas ( ) const

Vector2

get_local_mouse_position ( ) const

Transform2D

get_transform ( ) const

Rect2

get_viewport_rect ( ) const

Transform2D

get_viewport_transform ( ) const

World2D

get_world_2d ( ) const

void

hide ( )

bool

is_local_transform_notification_enabled ( ) const

bool

is_set_as_toplevel ( ) const

bool

is_transform_notification_enabled ( ) const

bool

is_visible_in_tree ( ) const

Vector2

make_canvas_position_local ( Vector2 screen_point ) const

InputEvent

make_input_local ( InputEvent event ) const

void

set_as_toplevel ( bool enable )

void

set_notify_local_transform ( bool enable )

void

set_notify_transform ( bool enable )

void

show ( )

void

update ( )


Signals

draw ( )

Emitted when the CanvasItem must redraw, after the related NOTIFICATION_DRAW notification, and before _draw is called.

Note: Deferred connections do not allow drawing through the draw_* methods.


hide ( )

Emitted when becoming hidden.


item_rect_changed ( )

Emitted when the item's Rect2 boundaries (position or size) have changed, or when an action is taking place that may have impacted these boundaries (e.g. changing Sprite.texture).


visibility_changed ( )

Emitted when the visibility (hidden/visible) changes.


Enumerations

enum BlendMode:

BlendMode BLEND_MODE_MIX = 0

Mix blending mode. Colors are assumed to be independent of the alpha (opacity) value.

BlendMode BLEND_MODE_ADD = 1

Additive blending mode.

BlendMode BLEND_MODE_SUB = 2

Subtractive blending mode.

BlendMode BLEND_MODE_MUL = 3

Multiplicative blending mode.

BlendMode BLEND_MODE_PREMULT_ALPHA = 4

Mix blending mode. Colors are assumed to be premultiplied by the alpha (opacity) value.

BlendMode BLEND_MODE_DISABLED = 5

Disables blending mode. Colors including alpha are written as-is. Only applicable for render targets with a transparent background. No lighting will be applied.


Constants

NOTIFICATION_TRANSFORM_CHANGED = 2000

The CanvasItem's global transform has changed. This notification is only received if enabled by set_notify_transform.

NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 35

The CanvasItem's local transform has changed. This notification is only received if enabled by set_notify_local_transform.

NOTIFICATION_DRAW = 30

The CanvasItem is requested to draw (see _draw).

NOTIFICATION_VISIBILITY_CHANGED = 31

The CanvasItem's visibility has changed.

NOTIFICATION_ENTER_CANVAS = 32

The CanvasItem has entered the canvas.

NOTIFICATION_EXIT_CANVAS = 33

The CanvasItem has exited the canvas.


Property Descriptions

int light_mask = 1

  • void set_light_mask ( int value )

  • int get_light_mask ( )

The rendering layers in which this CanvasItem responds to Light2D nodes.


Material material

The material applied to textures on this CanvasItem.


Color modulate = Color( 1, 1, 1, 1 )

  • void set_modulate ( Color value )

  • Color get_modulate ( )

The color applied to textures on this CanvasItem.


Color self_modulate = Color( 1, 1, 1, 1 )

  • void set_self_modulate ( Color value )

  • Color get_self_modulate ( )

The color applied to textures on this CanvasItem. This is not inherited by children CanvasItems.


bool show_behind_parent = false

  • void set_draw_behind_parent ( bool value )

  • bool is_draw_behind_parent_enabled ( )

If true, the object draws behind its parent.


bool show_on_top

If true, the object draws on top of its parent.


bool use_parent_material = false

  • void set_use_parent_material ( bool value )

  • bool get_use_parent_material ( )

If true, the parent CanvasItem's material property is used as this one's material.


bool visible = true

  • void set_visible ( bool value )

  • bool is_visible ( )

If true, this CanvasItem is drawn. The node is only visible if all of its ancestors are visible as well (in other words, is_visible_in_tree must return true).

Note: For controls that inherit Popup, the correct way to make them visible is to call one of the multiple popup*() functions instead.


Method Descriptions

void _draw ( ) virtual

Called when CanvasItem has been requested to redraw (when update is called, either manually or by the engine).

Corresponds to the NOTIFICATION_DRAW notification in Object._notification.


void draw_arc ( Vector2 center, float radius, float start_angle, float end_angle, int point_count, Color color, float width=1.0, bool antialiased=false )

Draws a unfilled arc between the given angles. The larger the value of point_count, the smoother the curve. See also draw_circle.

Note: Line drawing is not accelerated by batching if antialiased is true.

Note: Due to how it works, built-in antialiasing will not look correct for translucent lines and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedRegularPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing. 2D batching is also still supported with those antialiased lines.


float draw_char ( Font font, Vector2 position, String char, String next, Color modulate=Color( 1, 1, 1, 1 ) )

Draws a string character using a custom font. Returns the advance, depending on the character width and kerning with an optional next character.


void draw_circle ( Vector2 position, float radius, Color color )

Draws a colored, filled circle. See also draw_arc, draw_polyline and draw_polygon.

Note: Built-in antialiasing is not provided for draw_circle. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedRegularPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing.


void draw_colored_polygon ( PoolVector2Array points, Color color, PoolVector2Array uvs=PoolVector2Array( ), Texture texture=null, Texture normal_map=null, bool antialiased=false )

Draws a colored polygon of any amount of points, convex or concave. Unlike draw_polygon, a single color must be specified for the whole polygon.

Note: Due to how it works, built-in antialiasing will not look correct for translucent polygons and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing.


void draw_line ( Vector2 from, Vector2 to, Color color, float width=1.0, bool antialiased=false )

Draws a line from a 2D point to another, with a given color and width. It can be optionally antialiased. See also draw_multiline and draw_polyline.

Note: Line drawing is not accelerated by batching if antialiased is true.

Note: Due to how it works, built-in antialiasing will not look correct for translucent lines and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedLine2D node. That node relies on a texture with custom mipmaps to perform antialiasing. 2D batching is also still supported with those antialiased lines.


void draw_mesh ( Mesh mesh, Texture texture, Texture normal_map=null, Transform2D transform=Transform2D( 1, 0, 0, 1, 0, 0 ), Color modulate=Color( 1, 1, 1, 1 ) )

Draws a Mesh in 2D, using the provided texture. See MeshInstance2D for related documentation.


void draw_multiline ( PoolVector2Array points, Color color, float width=1.0, bool antialiased=false )

Draws multiple disconnected lines with a uniform color. When drawing large amounts of lines, this is faster than using individual draw_line calls. To draw interconnected lines, use draw_polyline instead.

Note: width and antialiased are currently not implemented and have no effect. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedLine2D node. That node relies on a texture with custom mipmaps to perform antialiasing. 2D batching is also still supported with those antialiased lines.


void draw_multiline_colors ( PoolVector2Array points, PoolColorArray colors, float width=1.0, bool antialiased=false )

Draws multiple disconnected lines with a uniform width and segment-by-segment coloring. Colors assigned to line segments match by index between points and colors. When drawing large amounts of lines, this is faster than using individual draw_line calls. To draw interconnected lines, use draw_polyline_colors instead.

Note: width and antialiased are currently not implemented and have no effect. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedLine2D node. That node relies on a texture with custom mipmaps to perform antialiasing. 2D batching is also still supported with those antialiased lines.


void draw_multimesh ( MultiMesh multimesh, Texture texture, Texture normal_map=null )

Draws a MultiMesh in 2D with the provided texture. See MultiMeshInstance2D for related documentation.


void draw_polygon ( PoolVector2Array points, PoolColorArray colors, PoolVector2Array uvs=PoolVector2Array( ), Texture texture=null, Texture normal_map=null, bool antialiased=false )

Draws a solid polygon of any amount of points, convex or concave. Unlike draw_colored_polygon, each point's color can be changed individually. See also draw_polyline and draw_polyline_colors.

Note: Due to how it works, built-in antialiasing will not look correct for translucent polygons and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing.


void draw_polyline ( PoolVector2Array points, Color color, float width=1.0, bool antialiased=false )

Draws interconnected line segments with a uniform color and width and optional antialiasing. When drawing large amounts of lines, this is faster than using individual draw_line calls. To draw disconnected lines, use draw_multiline instead. See also draw_polygon.

Note: Due to how it works, built-in antialiasing will not look correct for translucent polygons and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing.


void draw_polyline_colors ( PoolVector2Array points, PoolColorArray colors, float width=1.0, bool antialiased=false )

Draws interconnected line segments with a uniform width and segment-by-segment coloring, and optional antialiasing. Colors assigned to line segments match by index between points and colors. When drawing large amounts of lines, this is faster than using individual draw_line calls. To draw disconnected lines, use draw_multiline_colors instead. See also draw_polygon.

Note: Due to how it works, built-in antialiasing will not look correct for translucent polygons and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing.


void draw_primitive ( PoolVector2Array points, PoolColorArray colors, PoolVector2Array uvs, Texture texture=null, float width=1.0, Texture normal_map=null )

Draws a custom primitive. 1 point for a point, 2 points for a line, 3 points for a triangle, and 4 points for a quad. If 0 points or more than 4 points are specified, nothing will be drawn and an error message will be printed. See also draw_line, draw_polyline, draw_polygon, and draw_rect.


void draw_rect ( Rect2 rect, Color color, bool filled=true, float width=1.0, bool antialiased=false )

Draws a rectangle. If filled is true, the rectangle will be filled with the color specified. If filled is false, the rectangle will be drawn as a stroke with the color and width specified. If antialiased is true, the lines will attempt to perform antialiasing using OpenGL line smoothing.

Note: width and antialiased are only effective if filled is false.

Note: Due to how it works, built-in antialiasing will not look correct for translucent polygons and may not work on certain platforms. As a workaround, install the Antialiased Line2D add-on then create an AntialiasedPolygon2D node. That node relies on a texture with custom mipmaps to perform antialiasing.


void draw_set_transform ( Vector2 position, float rotation, Vector2 scale )

Sets a custom transform for drawing via components. Anything drawn afterwards will be transformed by this.


void draw_set_transform_matrix ( Transform2D xform )

Sets a custom transform for drawing via matrix. Anything drawn afterwards will be transformed by this.


void draw_string ( Font font, Vector2 position, String text, Color modulate=Color( 1, 1, 1, 1 ), int clip_w=-1 )

Draws text using the specified font at the position (bottom-left corner using the baseline of the font). The text will have its color multiplied by modulate. If clip_w is greater than or equal to 0, the text will be clipped if it exceeds the specified width.

Example using the default project font:

# If using this method in a script that redraws constantly, move the
# `default_font` declaration to a member variable assigned in `_ready()`
# so the Control is only created once.
var default_font = Control.new().get_font("font")
draw_string(default_font, Vector2(64, 64), "Hello world")

See also Font.draw.


void draw_style_box ( StyleBox style_box, Rect2 rect )

Draws a styled rectangle.


void draw_texture ( Texture texture, Vector2 position, Color modulate=Color( 1, 1, 1, 1 ), Texture normal_map=null )

Draws a texture at a given position.


void draw_texture_rect ( Texture texture, Rect2 rect, bool tile, Color modulate=Color( 1, 1, 1, 1 ), bool transpose=false, Texture normal_map=null )

Draws a textured rectangle at a given position, optionally modulated by a color. If transpose is true, the texture will have its X and Y coordinates swapped.


void draw_texture_rect_region ( Texture texture, Rect2 rect, Rect2 src_rect, Color modulate=Color( 1, 1, 1, 1 ), bool transpose=false, Texture normal_map=null, bool clip_uv=true )

Draws a textured rectangle region at a given position, optionally modulated by a color. If transpose is true, the texture will have its X and Y coordinates swapped.


void force_update_transform ( )

Forces the transform to update. Transform changes in physics are not instant for performance reasons. Transforms are accumulated and then set. Use this if you need an up-to-date transform when doing physics operations.


RID get_canvas ( ) const

Returns the RID of the World2D canvas where this item is in.


RID get_canvas_item ( ) const

Returns the canvas item RID used by VisualServer for this item.


Transform2D get_canvas_transform ( ) const

Returns the transform from the coordinate system of the canvas, this item is in, to the Viewports coordinate system.


Vector2 get_global_mouse_position ( ) const

Returns the mouse's position in the CanvasLayer that this CanvasItem is in using the coordinate system of the CanvasLayer.


Transform2D get_global_transform ( ) const

Returns the global transform matrix of this item.


Transform2D get_global_transform_with_canvas ( ) const

Returns the transform from the local coordinate system of this CanvasItem to the Viewports coordinate system.


Vector2 get_local_mouse_position ( ) const

Returns the mouse's position in this CanvasItem using the local coordinate system of this CanvasItem.


Transform2D get_transform ( ) const

Returns the transform matrix of this item.


Rect2 get_viewport_rect ( ) const

Returns the viewport's boundaries as a Rect2.


Transform2D get_viewport_transform ( ) const

Returns the transform from the coordinate system of the canvas, this item is in, to the Viewports embedders coordinate system.


World2D get_world_2d ( ) const

Returns the World2D where this item is in.


void hide ( )

Hide the CanvasItem if it's currently visible. This is equivalent to setting visible to false.


bool is_local_transform_notification_enabled ( ) const

Returns true if local transform notifications are communicated to children.


bool is_set_as_toplevel ( ) const

Returns true if the node is set as top-level. See set_as_toplevel.


bool is_transform_notification_enabled ( ) const

Returns true if global transform notifications are communicated to children.


bool is_visible_in_tree ( ) const

Returns true if the node is present in the SceneTree, its visible property is true and all its ancestors are also visible. If any ancestor is hidden, this node will not be visible in the scene tree, and is consequently not drawn (see _draw).


Vector2 make_canvas_position_local ( Vector2 screen_point ) const

Assigns screen_point as this node's new local transform.


InputEvent make_input_local ( InputEvent event ) const

Transformations issued by event's inputs are applied in local space instead of global space.


void set_as_toplevel ( bool enable )

If enable is true, this CanvasItem will not inherit its transform from parent CanvasItems. Its draw order will also be changed to make it draw on top of other CanvasItems that are not set as top-level. The CanvasItem will effectively act as if it was placed as a child of a bare Node. See also is_set_as_toplevel.


void set_notify_local_transform ( bool enable )

If enable is true, this node will receive NOTIFICATION_LOCAL_TRANSFORM_CHANGED when its local transform changes.


void set_notify_transform ( bool enable )

If enable is true, this node will receive NOTIFICATION_TRANSFORM_CHANGED when its global transform changes.


void show ( )

Show the CanvasItem if it's currently hidden. This is equivalent to setting visible to true. For controls that inherit Popup, the correct way to make them visible is to call one of the multiple popup*() functions instead.


void update ( )

Queues the CanvasItem to redraw. During idle time, if CanvasItem is visible, NOTIFICATION_DRAW is sent and _draw is called. This only occurs once per frame, even if this method has been called multiple times.