Create a new ElementComponent instance.
The ComponentSystem that created this Component.
The Entity that this Component is attached to.
The Entity that this Component is attached to.
The Entity with a ScreenComponent that this component belongs to. This is automatically set when the component is a child of a ScreenComponent.
The ComponentSystem used to create this Component.
Gets the world space axis-aligned bounding box for this element component.
Gets the horizontal and vertical alignment of the text.
Sets the horizontal and vertical alignment of the text. Values range from 0 to 1 where
[0, 0]
is the bottom left and [1, 1]
is the top right. Only works for
ELEMENTTYPE_TEXT types.
Gets the anchor for this element component.
Sets the anchor for this element component. Specifies where the left, bottom, right and top
edges of the component are anchored relative to its parent. Each value ranges from 0 to 1.
e.g. a value of [0, 0, 0, 0]
means that the element will be anchored to the bottom left of
its parent. A value of [1, 1, 1, 1]
means it will be anchored to the top right. A split
anchor is when the left-right or top-bottom pairs of the anchor are not equal. In that case,
the component will be resized to cover that entire area. For example, a value of [0, 0, 1, 1]
will make the component resize exactly as its parent.
Gets whether the font size and line height will scale so that the text fits inside the height of the Element.
Sets whether the font size and line height will scale so that the text fits inside the height of the Element. The font size will be scaled between minFontSize and maxFontSize. The value of autoFitHeight will be ignored if autoHeight is true.
Gets whether the font size and line height will scale so that the text fits inside the width of the Element.
Sets whether the font size and line height will scale so that the text fits inside the width of the Element. The font size will be scaled between minFontSize and maxFontSize. The value of autoFitWidth will be ignored if autoWidth is true.
Gets whether to automatically set the height of the component to be the same as the textHeight.
Sets whether to automatically set the height of the component to be the same as the textHeight. Only works for ELEMENTTYPE_TEXT types.
Gets whether to automatically set the width of the component to be the same as the textWidth.
Sets whether to automatically set the width of the component to be the same as the textWidth. Only works for ELEMENTTYPE_TEXT types.
Gets the batch group (see BatchGroup) for this element.
Sets the batch group (see BatchGroup) for this element. Default is -1 (no group).
Gets the distance from the bottom edge of the anchor.
Sets the distance from the bottom edge of the anchor. Can be used in combination with a split anchor to make the component's top edge always be 'top' units away from the top.
Gets the height at which the element will be rendered.
Sets the height at which the element will be rendered. In most cases this will be the same
as height. However, in some cases the engine may calculate a different height for
the element, such as when the element is under the control of a LayoutGroupComponent.
In these scenarios, calculatedHeight
may be smaller or larger than the height that was set
in the editor.
Gets the width at which the element will be rendered.
Sets the width at which the element will be rendered. In most cases this will be the same as
width. However, in some cases the engine may calculate a different width for the
element, such as when the element is under the control of a LayoutGroupComponent. In
these scenarios, calculatedWidth
may be smaller or larger than the width that was set in
the editor.
Gets the color of the element.
Sets the color of the image for ELEMENTTYPE_IMAGE types or the color of the text for ELEMENTTYPE_TEXT types.
Gets the draw order of the component.
Sets the draw order of the component. A higher value means that the component will be rendered on top of other components.
Gets the enabled state of the component.
Sets the enabled state of the component.
Gets whether markup processing is enabled for this element.
Sets whether markup processing is enabled for this element. Only works for ELEMENTTYPE_TEXT types. Defaults to false.
Gets the fit mode of the element.
Sets the fit mode of the element. Controls how the content should be fitted and preserve the aspect ratio of the source texture or sprite. Only works for ELEMENTTYPE_IMAGE types. Can be:
Gets the font used for rendering the text.
Sets the font used for rendering the text. Only works for ELEMENTTYPE_TEXT types.
Gets the id of the font asset used for rendering the text.
Sets the id of the font asset used for rendering the text. Only works for ELEMENTTYPE_TEXT types.
Gets the size of the font.
Sets the size of the font. Only works for ELEMENTTYPE_TEXT types.
Gets the height of the element.
Sets the height of the element as set in the editor. Note that in some cases this may not reflect the true height at which the element is rendered, such as when the element is under the control of a LayoutGroupComponent. See calculatedHeight in order to ensure you are reading the true height at which the element will be rendered.
Gets the localization key to use to get the localized text from Application#i18n.
Sets the localization key to use to get the localized text from Application#i18n. Only works for ELEMENTTYPE_TEXT types.
Gets the array of layer IDs (Layer#id) to which this element belongs.
Sets the array of layer IDs (Layer#id) to which this element should belong. Don't push, pop, splice or modify this array. If you want to change it, set a new one instead.
Gets the distance from the left edge of the anchor.
Sets the distance from the left edge of the anchor. Can be used in combination with a split anchor to make the component's left edge always be 'left' units away from the left.
Gets the height of each line of text.
Sets the height of each line of text. Only works for ELEMENTTYPE_TEXT types.
Gets the distance from the left, bottom, right and top edges of the anchor.
Sets the distance from the left, bottom, right and top edges of the anchor. For example, if
we are using a split anchor like [0, 0, 1, 1]
and the margin is [0, 0, 0, 0]
then the
component will be the same width and height as its parent.
Gets whether the Image Element should be treated as a mask.
Sets whether the Image Element should be treated as a mask. Masks do not render into the scene, but instead limit child elements to only be rendered where this element is rendered.
Gets the material to use when rendering an image.
Sets the material to use when rendering an image. Only works for ELEMENTTYPE_IMAGE types.
Gets the id of the material asset to use when rendering an image.
Sets the id of the material asset to use when rendering an image. Only works for ELEMENTTYPE_IMAGE types.
Gets the maximum size that the font can scale to when autoFitWidth or autoFitHeight are true.
Sets the maximum size that the font can scale to when autoFitWidth or autoFitHeight are true.
Gets the maximum number of lines that the Element can wrap to. Returns null for unlimited lines.
Sets the maximum number of lines that the Element can wrap to. Any leftover text will be appended to the last line. Set this to null to allow unlimited lines.
Gets the minimum size that the font can scale to when autoFitWidth or autoFitHeight are true.
Sets the minimum size that the font can scale to when autoFitWidth or autoFitHeight are true.
Gets the opacity of the element.
Sets the opacity of the element. This works for both ELEMENTTYPE_IMAGE and ELEMENTTYPE_TEXT element types.
Gets the text outline effect color and opacity.
Sets the text outline effect color and opacity. Only works for ELEMENTTYPE_TEXT types.
Gets the width of the text outline effect.
Sets the width of the text outline effect. Only works for ELEMENTTYPE_TEXT types.
Gets the position of the pivot of the component relative to its anchor.
Sets the position of the pivot of the component relative to its anchor. Each value ranges
from 0 to 1 where [0, 0]
is the bottom left and [1, 1]
is the top right.
Gets the number of pixels that map to one PlayCanvas unit.
Sets the number of pixels that map to one PlayCanvas unit. Only works for ELEMENTTYPE_IMAGE types who have a sliced sprite assigned.
Gets the index of the last character to render.
Sets the index of the last character to render. Only works for ELEMENTTYPE_TEXT types.
Gets the index of the first character to render.
Sets the index of the first character to render. Only works for ELEMENTTYPE_TEXT types.
Gets the region of the texture to use in order to render an image.
Sets the region of the texture to use in order to render an image. Values range from 0 to 1 and indicate u, v, width, height. Only works for ELEMENTTYPE_IMAGE types.
Gets the distance from the right edge of the anchor.
Sets the distance from the right edge of the anchor. Can be used in combination with a split anchor to make the component's right edge always be 'right' units away from the right.
Gets whether to reorder the text for RTL languages.
Sets whether to reorder the text for RTL languages. The reordering uses a function
registered by app.systems.element.registerUnicodeConverter
.
Gets the array of 4 Vec3s that represent the bottom left, bottom right, top right and top left corners of the component relative to its parent ScreenComponent.
Gets the text shadow effect color and opacity.
Sets the text shadow effect color and opacity. Only works for ELEMENTTYPE_TEXT types.
Gets the text shadow effect shift amount from original text.
Sets the text shadow effect shift amount from original text. Only works for ELEMENTTYPE_TEXT types.
Gets the spacing between the letters of the text.
Sets the spacing between the letters of the text. Only works for ELEMENTTYPE_TEXT types.
Gets the sprite to render.
Sets the sprite to render. Only works for ELEMENTTYPE_IMAGE types which can render either a texture or a sprite.
Gets the id of the sprite asset to render.
Sets the id of the sprite asset to render. Only works for ELEMENTTYPE_IMAGE types which can render either a texture or a sprite.
Gets the frame of the sprite to render.
Sets the frame of the sprite to render. Only works for ELEMENTTYPE_IMAGE types who have a sprite assigned.
Gets the text to render.
Sets the text to render. Only works for ELEMENTTYPE_TEXT types. To override certain text styling properties on a per-character basis, the text can optionally include markup tags contained within square brackets. Supported tags are:
color
- override the element's color property. Examples:
[color="#ff0000"]red text[/color]
[color="#00ff00"]green text[/color]
[color="#0000ff"]blue text[/color]
outline
- override the element's outlineColor and outlineThickness
properties. Example:
[outline color="#ffffff" thickness="0.5"]text[/outline]
shadow
- override the element's shadowColor and shadowOffset
properties. Examples:
[shadow color="#ffffff" offset="0.5"]text[/shadow]
[shadow color="#000000" offsetX="0.1" offsetY="0.2"]text[/shadow]
Note that markup tags are only processed if the text element's enableMarkup property is set to true.
Gets the height of the text rendered by the component. Only works for ELEMENTTYPE_TEXT types.
Gets the texture to render.
Sets the texture to render. Only works for ELEMENTTYPE_IMAGE types.
Gets the id of the texture asset to render.
Sets the id of the texture asset to render. Only works for ELEMENTTYPE_IMAGE types.
Gets the width of the text rendered by the component. Only works for ELEMENTTYPE_TEXT types.
Gets the distance from the top edge of the anchor.
Sets the distance from the top edge of the anchor. Can be used in combination with a split anchor to make the component's bottom edge always be 'bottom' units away from the bottom.
Gets the type of the ElementComponent.
Sets the type of the ElementComponent. Can be:
Gets whether to convert unicode characters.
Sets whether to convert unicode characters. This uses a function registered by
app.systems.element.registerUnicodeConverter
.
Gets whether the component will receive mouse and touch input events.
Sets whether the component will receive mouse and touch input events.
Gets the width of the element.
Sets the width of the element as set in the editor. Note that in some cases this may not reflect the true width at which the element is rendered, such as when the element is under the control of a LayoutGroupComponent. See calculatedWidth in order to ensure you are reading the true width at which the element will be rendered.
Gets whether to automatically wrap lines based on the element width.
Sets whether to automatically wrap lines based on the element width. Only works for ELEMENTTYPE_TEXT types, and when autoWidth is set to false.
Fire an event, all additional arguments are passed on to the event listener.
Name of event to fire.
Optional
arg1: anyFirst argument that is passed to the event handler.
Optional
arg2: anySecond argument that is passed to the event handler.
Optional
arg3: anyThird argument that is passed to the event handler.
Optional
arg4: anyFourth argument that is passed to the event handler.
Optional
arg5: anyFifth argument that is passed to the event handler.
Optional
arg6: anySixth argument that is passed to the event handler.
Optional
arg7: anySeventh argument that is passed to the event handler.
Optional
arg8: anyEighth argument that is passed to the event handler.
Self for chaining.
Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.
Optional
name: stringName of the event to unbind.
Optional
callback: HandleEventCallbackFunction to be unbound.
Optional
scope: objectScope that was used as the this when the event is fired.
Self for chaining.
const handler = function () {
};
obj.on('test', handler);
obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all handler functions, called 'test' with scope this
Attach an event handler to an event.
Name of the event to bind the callback to.
Function that is called when event is fired. Note the callback is limited to 8 arguments.
Optional
scope: object = ...Object to use as 'this' when the event is fired, defaults to current this.
Can be used for removing event in the future.
Attach an event handler to an event. This handler will be removed after being fired once.
Name of the event to bind the callback to.
Function that is called when event is fired. Note the callback is limited to 8 arguments.
Optional
scope: object = ...Object to use as 'this' when the event is fired, defaults to current this.
Static
EVENT_Fired when the mouse is pressed and released on the component or when a touch starts and ends on the component. Only fired when useInput is true. The handler is passed an ElementMouseEvent or ElementTouchEvent.
Static
EVENT_Fired when the mouse is pressed while the cursor is on the component. Only fired when useInput is true. The handler is passed an ElementMouseEvent.
Static
EVENT_Fired when the mouse cursor enters the component. Only fired when useInput is true. The handler is passed an ElementMouseEvent.
Static
EVENT_Fired when the mouse cursor leaves the component. Only fired when useInput is true. The handler is passed an ElementMouseEvent.
Static
EVENT_Fired when the mouse cursor is moved on the component. Only fired when useInput is true. The handler is passed an ElementMouseEvent.
Static
EVENT_Fired when the mouse is released while the cursor is on the component. Only fired when useInput is true. The handler is passed an ElementMouseEvent.
Static
EVENT_Fired when the mouse wheel is scrolled on the component. Only fired when useInput is true. The handler is passed an ElementMouseEvent.
Static
EVENT_Fired when a touch is canceled on the component. Only fired when useInput is true. The handler is passed an ElementTouchEvent.
Static
EVENT_Fired when a touch ends on the component. Only fired when useInput is true. The handler is passed an ElementTouchEvent.
Static
EVENT_Fired when a touch moves after it started touching the component. Only fired when useInput is true. The handler is passed an ElementTouchEvent.
Static
EVENT_Fired when a touch starts on the component. Only fired when useInput is true. The handler is passed an ElementTouchEvent.
ElementComponents are used to construct user interfaces. The ElementComponent#type property can be configured in 3 main ways: as a text element, as an image element or as a group element. If the ElementComponent has a ScreenComponent ancestor in the hierarchy, it will be transformed with respect to the coordinate system of the screen. If there is no ScreenComponent ancestor, the ElementComponent will be transformed like any other entity.
You should never need to use the ElementComponent constructor. To add an ElementComponent to a Entity, use Entity#addComponent:
To create a simple text-based element:
Once the ElementComponent is added to the entity, you can set and get any of its properties:
Relevant 'Engine-only' examples: