CanvasItem

    Inherited By: , Node2D

    Base class of anything 2D.

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

    Any CanvasItem can draw. For this, 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 , 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 .

    Tutorials

    Properties

    void ( ) virtual
    voiddraw_arc ( center, float radius, start_angle, float end_angle, point_count, Color color, width=1.0, bool antialiased=false )
    draw_char ( font, Vector2 position, char, String next, modulate=Color( 1, 1, 1, 1 ) )
    voiddraw_circle ( position, float radius, color )
    voiddraw_colored_polygon ( points, Color color, uvs=PoolVector2Array( ), Texture texture=null, normal_map=null, bool antialiased=false )
    void ( Vector2 from, to, Color color, width=1.0, bool antialiased=false )
    void ( Mesh mesh, texture, Texture normal_map=null, transform=Transform2D( 1, 0, 0, 1, 0, 0 ), Color modulate=Color( 1, 1, 1, 1 ) )
    void ( PoolVector2Array points, color, float width=1.0, antialiased=false )
    voiddraw_multiline_colors ( points, PoolColorArray colors, width=1.0, bool antialiased=false )
    void ( MultiMesh multimesh, texture, Texture normal_map=null )
    void ( PoolVector2Array points, colors, PoolVector2Array uvs=PoolVector2Array( ), texture=null, Texture normal_map=null, antialiased=false )
    voiddraw_polyline ( points, Color color, width=1.0, bool antialiased=false )
    void ( PoolVector2Array points, colors, float width=1.0, antialiased=false )
    voiddraw_primitive ( points, PoolColorArray colors, uvs, Texture texture=null, width=1.0, Texture normal_map=null )
    void ( Rect2 rect, color, bool filled=true, width=1.0, bool antialiased=false )
    void ( Vector2 position, rotation, Vector2 scale )
    void ( Transform2D xform )
    void ( Font font, position, String text, modulate=Color( 1, 1, 1, 1 ), int clip_w=-1 )
    void ( StyleBox style_box, rect )
    voiddraw_texture ( texture, Vector2 position, modulate=Color( 1, 1, 1, 1 ), Texture normal_map=null )
    void ( Texture texture, rect, bool tile, modulate=Color( 1, 1, 1, 1 ), bool transpose=false, normal_map=null )
    voiddraw_texture_rect_region ( texture, Rect2 rect, src_rect, Color modulate=Color( 1, 1, 1, 1 ), transpose=false, Texture normal_map=null, clip_uv=true )
    voidforce_update_transform ( )
    get_canvas ( ) const
    get_canvas_item ( ) const
    get_canvas_transform ( ) const
    get_global_mouse_position ( ) const
    get_global_transform ( ) const
    get_global_transform_with_canvas ( ) const
    get_local_mouse_position ( ) const
    get_transform ( ) const
    get_viewport_rect ( ) const
    get_viewport_transform ( ) const
    get_world_2d ( ) const
    void ( )
    bool ( ) const
    bool ( ) const
    bool ( ) const
    bool ( ) const
    Vector2 ( Vector2 screen_point ) const
    make_input_local ( event ) const
    voidset_as_toplevel ( enable )
    voidset_notify_local_transform ( enable )
    voidset_notify_transform ( enable )
    voidshow ( )
    void ( )

    Signals

    • draw ( )

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


    • 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 ).


    • visibility_changed ( )

    Emitted when the visibility (hidden/visible) changes.

    Enumerations

    enum BlendMode:

    • BLEND_MODE_MIX = 0 —- Mix blending mode. Colors are assumed to be independent of the alpha (opacity) value.
    • BLEND_MODE_ADD = 1 —- Additive blending mode.
    • BLEND_MODE_SUB = 2 —- Subtractive blending mode.
    • BLEND_MODE_MUL = 3 —- Multiplicative blending mode.
    • BLEND_MODE_PREMULT_ALPHA = 4 —- Mix blending mode. Colors are assumed to be premultiplied by the alpha (opacity) value.
    • 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.
    • NOTIFICATION_TRANSFORM_CHANGED = 2000 —- The CanvasItem‘s transform has changed. This notification is only received if enabled by set_notify_transform or .
    • 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.

    Property Descriptions

    • int light_mask
    Default1
    Setterset_light_mask(value)
    Getterget_light_mask()

    The rendering layers in which this CanvasItem responds to nodes.


    The material applied to textures on this CanvasItem.


    • modulate
    DefaultColor( 1, 1, 1, 1 )
    Setterset_modulate(value)
    Getterget_modulate()

    The color applied to textures on this CanvasItem.


    DefaultColor( 1, 1, 1, 1 )
    Setterset_self_modulate(value)
    Getterget_self_modulate()

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


    • show_behind_parent

    If true, the object draws behind its parent.


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


    • use_parent_material
    Defaultfalse
    Setterset_use_parent_material(value)
    Getterget_use_parent_material()

    If , the parent CanvasItem‘s material property is used as this one’s material.


    • visible
    Defaulttrue
    Setterset_visible(value)
    Getteris_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 , 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_arc ( Vector2 center, radius, float start_angle, end_angle, int point_count, color, float width=1.0, antialiased=false )

    • float draw_char ( font, Vector2 position, char, String next, 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.


    Draws a colored circle.


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

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


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

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


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

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


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

    Draws multiple, parallel lines with a uniform color.

    Note: width and antialiased are currently not implemented and have no effect.


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

    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.

    Note: width and antialiased are currently not implemented and have no effect.


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

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


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


    Draws interconnected line segments with a uniform color and width and optional antialiasing.


    Draws interconnected line segments with a uniform width, segment-by-segment coloring, and optional antialiasing. 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.


    • void draw_rect ( Rect2 rect, color, bool filled=true, 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 be antialiased.

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


    • void draw_set_transform ( position, float rotation, scale )

    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.


    • void draw_string ( font, Vector2 position, text, Color modulate=Color( 1, 1, 1, 1 ), clip_w=-1 )

    Draws text 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.

    Example using the default project font:

    See also Font.draw.


    Draws a styled rectangle.


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

    Draws a texture at a given position.


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

    • void draw_texture_rect_region ( texture, Rect2 rect, src_rect, Color modulate=Color( 1, 1, 1, 1 ), transpose=false, Texture normal_map=null, 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 of the World2D canvas where this item is in.


    • get_canvas_item ( ) const

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


    • get_canvas_transform ( ) const

    Returns the transform matrix of this item’s canvas.


    • Vector2 get_global_mouse_position ( ) const

    Returns the global position of the mouse.


    • get_global_transform ( ) const

    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.


    • get_local_mouse_position ( ) const

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


    Returns the transform matrix of this item.


    • get_viewport_rect ( ) const

    Returns the viewport’s boundaries as a Rect2.


    • get_viewport_transform ( ) const

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


    Returns the 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.


    • is_set_as_toplevel ( ) const

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


    • 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 , 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.


    • make_canvas_position_local ( Vector2 screen_point ) const

    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_as_toplevel ( enable )

    If enable is true, the node won’t inherit its transform from parent canvas items.


    • void set_notify_local_transform ( bool enable )

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


    • void set_notify_transform ( enable )

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


    • void show ( )

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