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 must be called, 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 the Object._notification, signal or _draw virtual functions.

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 @GlobalScope.deg2rad.

Methods

void

_draw ( ) virtual

void

draw_animation_slice ( float animation_length, float slice_begin, float slice_end, float offset=0.0 )

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 pos, String char, String next="", int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0) ) const

void

draw_circle ( Vector2 position, float radius, Color color )

void

draw_colored_polygon ( PackedVector2Array points, Color color, PackedVector2Array uvs=PackedVector2Array(), Texture2D texture=null )

void

draw_end_animation ( )

void

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

void

draw_mesh ( Mesh mesh, Texture2D texture, Transform2D transform=Transform2D(1, 0, 0, 1, 0, 0), Color modulate=Color(1, 1, 1, 1) )

void

draw_msdf_texture_rect_region ( Texture2D texture, Rect2 rect, Rect2 src_rect, Color modulate=Color(1, 1, 1, 1), float outline=0.0, float pixel_range=4.0 )

void

draw_multiline ( PackedVector2Array points, Color color, float width=1.0 )

void

draw_multiline_colors ( PackedVector2Array points, PackedColorArray colors, float width=1.0 )

void

draw_multiline_string ( Font font, Vector2 pos, String text, HAlign align=0, float width=-1, int max_lines=-1, int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0), int flags=99 ) const

void

draw_multimesh ( MultiMesh multimesh, Texture2D texture )

void

draw_polygon ( PackedVector2Array points, PackedColorArray colors, PackedVector2Array uvs=PackedVector2Array(), Texture2D texture=null )

void

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

void

draw_polyline_colors ( PackedVector2Array points, PackedColorArray colors, float width=1.0, bool antialiased=false )

void

draw_primitive ( PackedVector2Array points, PackedColorArray colors, PackedVector2Array uvs, Texture2D texture=null, float width=1.0 )

void

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

void

draw_set_transform ( Vector2 position, float rotation=0.0, Vector2 scale=Vector2(1, 1) )

void

draw_set_transform_matrix ( Transform2D xform )

void

draw_string ( Font font, Vector2 pos, String text, HAlign align=0, float width=-1, int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0), int flags=3 ) const

void

draw_style_box ( StyleBox style_box, Rect2 rect )

void

draw_texture ( Texture2D texture, Vector2 position, Color modulate=Color(1, 1, 1, 1) )

void

draw_texture_rect ( Texture2D texture, Rect2 rect, bool tile, Color modulate=Color(1, 1, 1, 1), bool transpose=false )

void

draw_texture_rect_region ( Texture2D texture, Rect2 rect, Rect2 src_rect, Color modulate=Color(1, 1, 1, 1), bool transpose=false, 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_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_notify_local_transform ( bool enable )

void

set_notify_transform ( bool enable )

void

show ( )

void

update ( )

Signals

  • draw ( )

Emitted when the CanvasItem must redraw. This can only be connected realtime, as deferred will not allow drawing.


  • hidden ( )

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 Sprite2D.texture).


  • visibility_changed ( )

Emitted when the visibility (hidden/visible) changes.

Enumerations

enum TextureFilter:

  • TEXTURE_FILTER_PARENT_NODE = 0 --- The CanvasItem will inherit the filter from its parent.

  • TEXTURE_FILTER_NEAREST = 1 --- The texture filter reads from the nearest pixel only. The simplest and fastest method of filtering. Useful for pixel art.

  • TEXTURE_FILTER_LINEAR = 2 --- The texture filter blends between the nearest four pixels. Use this for most cases where you want to avoid a pixelated style.

  • TEXTURE_FILTER_NEAREST_WITH_MIPMAPS = 3 --- The texture filter reads from the nearest pixel in the nearest mipmap. This is the fastest way to read from textures with mipmaps.

  • TEXTURE_FILTER_LINEAR_WITH_MIPMAPS = 4 --- The texture filter blends between the nearest 4 pixels and between the nearest 2 mipmaps. Use this for non-pixel art textures that may be viewed at a low scale (e.g. due to Camera2D zoom), as mipmaps are important to smooth out pixels that are smaller than on-screen pixels.

  • TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC = 5 --- The texture filter reads from the nearest pixel, but selects a mipmap based on the angle between the surface and the camera view. This reduces artifacts on surfaces that are almost in line with the camera.

Note: This texture filter is rarely useful in 2D projects. TEXTURE_FILTER_NEAREST_WITH_MIPMAPS is usually more appropriate.

  • TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC = 6 --- The texture filter blends between the nearest 4 pixels and selects a mipmap based on the angle between the surface and the camera view. This reduces artifacts on surfaces that are almost in line with the camera. This is the slowest of the filtering options, but results in the highest quality texturing.

Note: This texture filter is rarely useful in 2D projects. TEXTURE_FILTER_LINEAR_WITH_MIPMAPS is usually more appropriate.

  • TEXTURE_FILTER_MAX = 7 --- Represents the size of the TextureFilter enum.


enum TextureRepeat:

  • TEXTURE_REPEAT_PARENT_NODE = 0 --- The CanvasItem will inherit the filter from its parent.

  • TEXTURE_REPEAT_DISABLED = 1 --- Texture will not repeat.

  • TEXTURE_REPEAT_ENABLED = 2 --- Texture will repeat normally.

  • TEXTURE_REPEAT_MIRROR = 3 --- Texture will repeat in a 2x2 tiled mode, where elements at even positions are mirrored.

  • TEXTURE_REPEAT_MAX = 4 --- Represents the size of the TextureRepeat enum.

Constants

  • NOTIFICATION_TRANSFORM_CHANGED = 2000 --- The CanvasItem's transform has changed. This notification is only received if enabled by set_notify_transform or set_notify_local_transform.

  • NOTIFICATION_DRAW = 30 --- The CanvasItem is requested to 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

  • bool clip_children

Default

false

Setter

set_clip_children(value)

Getter

is_clipping_children()


  • int light_mask

Default

1

Setter

set_light_mask(value)

Getter

get_light_mask()

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


Setter

set_material(value)

Getter

get_material()

The material applied to textures on this CanvasItem.


Default

Color(1, 1, 1, 1)

Setter

set_modulate(value)

Getter

get_modulate()

The color applied to textures on this CanvasItem.


Default

Color(1, 1, 1, 1)

Setter

set_self_modulate(value)

Getter

get_self_modulate()

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


  • bool show_behind_parent

Default

false

Setter

set_draw_behind_parent(value)

Getter

is_draw_behind_parent_enabled()

If true, the object draws behind its parent.


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


Default

0

Setter

set_texture_filter(value)

Getter

get_texture_filter()

The texture filtering mode to use on this CanvasItem.


Default

0

Setter

set_texture_repeat(value)

Getter

get_texture_repeat()

The texture repeating mode to use on this CanvasItem.


Default

false

Setter

set_as_top_level(value)

Getter

is_set_as_top_level()

If true, the node will not inherit its transform from parent CanvasItems.


  • bool use_parent_material

Default

false

Setter

set_use_parent_material(value)

Getter

get_use_parent_material()

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


Default

true

Setter

set_visible(value)

Getter

is_visible()

If true, this CanvasItem is drawn. The node is only visible if all of its antecedents 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

Overridable function called by the engine (if defined) to draw the canvas item.


  • void draw_animation_slice ( float animation_length, float slice_begin, float slice_end, float offset=0.0 )

Subsequent drawing commands will be ignored unless they fall within the specified animation slice. This is a faster way to implement animations that loop on background rather than redrawing constantly.


Draws an arc between the given angles. The larger the value of point_count, the smoother the curve.


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


Draws a colored circle.


Draws a colored polygon of any amount of points, convex or concave.


  • void draw_end_animation ( )

After submitting all animations slices via draw_animation_slice, this function can be used to revert drawing to its default state (all subsequent drawing commands will be visible). If you don't care about this particular use case, usage of this function after submitting the slices is not required.


Draws a line from a 2D point to another, with a given color and width.


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


Draws a textured rectangle region of the multi-channel signed distance field texture at a given position, optionally modulated by a color. See FontData.set_multichannel_signed_distance_field for more information and caveats about MSDF font rendering.

If outline is positive, each alpha channel value of pixel in region is set to maximum value of true distance in the outline radius.

Value of the pixel_range should the same that was used during distance field texture generation.


Draws multiple, parallel lines with a uniform color.


Draws multiple, parallel lines with a uniform width and segment-by-segment coloring. Colors assigned to line segments match by index between points and colors.


Breaks text to the lines and draws it using the specified font at the position (top-left corner). 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.


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


Draws a polygon of any amount of points, convex or concave.


Draws interconnected line segments with a uniform color and width.


Draws interconnected line segments with a uniform width and segment-by-segment coloring. Colors assigned to line segments match by index between points and colors.


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.


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.

Note: width is only effective if filled is false.


  • void draw_set_transform ( Vector2 position, float rotation=0.0, Vector2 scale=Vector2(1, 1) )

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


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


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")
var default_font_size = Control.new().get_font_size("font_size")
draw_string(default_font, Vector2(64, 64), "Hello world", HALIGN_LEFT, -1, font_size)

See also Font.draw_string.


Draws a styled rectangle.


Draws a texture at a given position.


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.


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 RenderingServer for this item.


Returns the transform matrix of this item's canvas.


  • Vector2 get_global_mouse_position ( ) const

Returns the global position of the mouse.


Returns the global transform matrix of this item.


  • Transform2D get_global_transform_with_canvas ( ) const

Returns the global transform matrix of this item in relation to the canvas.


  • Vector2 get_local_mouse_position ( ) const

Returns the mouse position relative to this item's position.


Returns the transform matrix of this item.


  • Rect2 get_viewport_rect ( ) const

Returns the viewport's boundaries as a Rect2.


Returns this item's transform in relation to the viewport.


Returns the World2D where this item is in.


  • void hide ( )

Hide the CanvasItem if it's currently visible.


  • bool is_local_transform_notification_enabled ( ) const

Returns true if local transform notifications are communicated to children.


  • 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 antecedents are also visible. If any antecedent is hidden, this node will not be visible in the scene tree.


Assigns screen_point as this node's new local transform.


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


  • void set_notify_local_transform ( bool enable )

If enable is true, children will be updated with local transform data.


  • void set_notify_transform ( bool enable )

If enable is true, children will be updated with global transform data.


  • void show ( )

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


  • void update ( )

Queue the CanvasItem for update. NOTIFICATION_DRAW will be called on idle time to request redraw.