API Reference

Class List

ElementComponent

Extends: Component

ElementComponents are used to construct user interfaces. An ElementComponent's 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:

// Add an element component to an entity with the default options
let entity = pc.Entity();
entity.addComponent("element"); // This defaults to a 'group' element

To create a simple text-based element:

entity.addComponent("element", {
    anchor: new pc.Vec4(0.5, 0.5, 0.5, 0.5), // centered anchor
    fontAsset: fontAsset,
    fontSize: 128,
    pivot: new pc.Vec2(0.5, 0.5),            // centered pivot
    text: "Hello World!",
    type: pc.ELEMENTTYPE_TEXT
});

Once the ElementComponent is added to the entity, you can set and get any of its properties:

entity.element.color = pc.Color.RED; // Set the element's color to red

console.log(entity.element.color);   // Get the element's color and print it

Relevant 'Engine-only' examples:

Summary

Properties

alignment

The horizontal and vertical alignment of the text.

anchor

Specifies where the left, bottom, right and top edges of the component are anchored relative to its parent.

autoFitHeight

When true the font size and line height will scale so that the text fits inside the height of the Element.

autoFitWidth

When true the font size and line height will scale so that the text fits inside the width of the Element.

autoHeight

Automatically set the height of the component to be the same as the textHeight.

autoWidth

Automatically set the width of the component to be the same as the textWidth.

batchGroupId

Assign element to a specific batch group (see BatchGroup).

bottom

The distance from the bottom edge of the anchor.

calculatedHeight

The height at which the element will be rendered.

calculatedWidth

The width at which the element will be rendered.

canvasCorners

An array of 4 Vec2s that represent the bottom left, bottom right, top right and top left corners of the component in canvas pixels.

color

The color of the image for ELEMENTTYPE_IMAGE types or the color of the text for ELEMENTTYPE_TEXT types.

drawOrder

The draw order of the component.

enableMarkup

Flag for enabling markup processing.

fitMode

Set how the content should be fitted and preserve the aspect ratio of the source texture or sprite.

font

The font used for rendering the text.

fontAsset

The id of the font asset used for rendering the text.

fontSize

The size of the font.

height

The height of the element as set in the editor.

key

The localization key to use to get the localized text from Application#i18n.

layers

An array of layer IDs (Layer#id) to which this element should belong.

left

The distance from the left edge of the anchor.

lineHeight

The height of each line of text.

margin

The distance from the left, bottom, right and top edges of the anchor.

mask

Switch Image Element into a mask.

material

The material to use when rendering an image.

materialAsset

The id of the material asset to use when rendering an image.

maxFontSize

The maximum size that the font can scale to when autoFitWidth or autoFitHeight are true.

maxLines

The maximum number of lines that the Element can wrap to.

minFontSize

The minimum size that the font can scale to when autoFitWidth or autoFitHeight are true.

opacity

The opacity of the image for ELEMENTTYPE_IMAGE types or the text for ELEMENTTYPE_TEXT types.

outlineColor

The text outline effect color and opacity.

outlineThickness

The width of the text outline effect.

pivot

The position of the pivot of the component relative to its anchor.

pixelsPerUnit

The number of pixels that map to one PlayCanvas unit.

rangeEnd

Index of the last character to render.

rangeStart

Index of the first character to render.

rect

Specifies which region of the texture to use in order to render an image.

right

The distance from the right edge of the anchor.

rtlReorder

Reorder the text for RTL languages using a function registered by app.

screen

The Entity with a ScreenComponent that this component belongs to.

screenCorners

An 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.

shadowColor

The text shadow effect color and opacity.

shadowOffset

The text shadow effect shift amount from original text.

spacing

The spacing between the letters of the text.

sprite

The sprite to render.

spriteAsset

The id of the sprite asset to render.

spriteFrame

The frame of the sprite to render.

text

The text to render.

textHeight

The height of the text rendered by the component.

texture

The texture to render.

textureAsset

The id of the texture asset to render.

textWidth

The width of the text rendered by the component.

top

The distance from the top edge of the anchor.

type

The type of the ElementComponent.

unicodeConverter

Convert unicode characters using a function registered by app.

useInput

If true then the component will receive Mouse or Touch input events.

width

The width of the element as set in the editor.

worldCorners

An array of 4 Vec3s that represent the bottom left, bottom right, top right and top left corners of the component in world space.

wrapLines

Whether to automatically wrap lines based on the element width.

Events

click

Fired when the mouse is pressed and released on the component or when a touch starts and ends on the component.

mousedown

Fired when the mouse is pressed while the cursor is on the component.

mouseenter

Fired when the mouse cursor enters the component.

mouseleave

Fired when the mouse cursor leaves the component.

mousemove

Fired when the mouse cursor is moved on the component.

mouseup

Fired when the mouse is released while the cursor is on the component.

mousewheel

Fired when the mouse wheel is scrolled on the component.

touchcancel

Fired when a touch is canceled on the component.

touchend

Fired when a touch ends on the component.

touchmove

Fired when a touch moves after it started touching the component.

touchstart

Fired when a touch starts on the component.

Inherited

Properties

enabled

Enables or disables the component.

entity

The Entity that this Component is attached to.

system

The ComponentSystem used to create this Component.

Methods

fire

Fire an event, all additional arguments are passed on to the event listener.

hasEvent

Test if there are any handlers bound to an event name.

off

Detach an event handler from an event.

on

Attach an event handler to an event.

once

Attach an event handler to an event.

Details

Constructor

ElementComponent(system, entity)

Create a new ElementComponent instance.

Parameters

systemElementComponentSystem

The ComponentSystem that created this Component.

entityEntity

The Entity that this Component is attached to.

Properties

Vec2alignment

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.

Vec4, number[]anchor

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. e.g. a value of [0, 0, 1, 1] will make the component resize exactly as its parent.

pc.app.root.findByName("Inventory").element.anchor = new pc.Vec4(Math.random() * 0.1, 0, 1, 0);
pc.app.root.findByName("Inventory").element.anchor = [Math.random() * 0.1, 0, 1, 0];
booleanautoFitHeight

When true 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.

booleanautoFitWidth

When true 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.

booleanautoHeight

Automatically set the height of the component to be the same as the textHeight. Only works for ELEMENTTYPE_TEXT types.

booleanautoWidth

Automatically set the width of the component to be the same as the textWidth. Only works for ELEMENTTYPE_TEXT types.

numberbatchGroupId

Assign element to a specific batch group (see BatchGroup). Default is -1 (no group).

numberbottom

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.

numbercalculatedHeight

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.

numbercalculatedWidth

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.

Vec2[]canvasCorners

An array of 4 Vec2s that represent the bottom left, bottom right, top right and top left corners of the component in canvas pixels. Only works for screen space element components.

Colorcolor

The color of the image for ELEMENTTYPE_IMAGE types or the color of the text for ELEMENTTYPE_TEXT types.

numberdrawOrder

The draw order of the component. A higher value means that the component will be rendered on top of other components.

booleanenableMarkup

Flag for enabling markup processing. Only works for ELEMENTTYPE_TEXT types. Defaults to false.

stringfitMode

Set how the content should be fitted and preserve the aspect ratio of the source texture or sprite. Only works for ELEMENTTYPE_IMAGE types.

Fontfont

The font used for rendering the text. Only works for ELEMENTTYPE_TEXT types.

numberfontAsset

The id of the font asset used for rendering the text. Only works for ELEMENTTYPE_TEXT types.

numberfontSize

The size of the font. Only works for ELEMENTTYPE_TEXT types.

numberheight

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.

stringkey

The localization key to use to get the localized text from Application#i18n. Only works for ELEMENTTYPE_TEXT types.

number[]layers

An 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.

numberleft

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.

numberlineHeight

The height of each line of text. Only works for ELEMENTTYPE_TEXT types.

Vec4margin

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.

booleanmask

Switch Image Element into a mask. Masks do not render into the scene, but instead limit child elements to only be rendered where this element is rendered.

Materialmaterial

The material to use when rendering an image. Only works for ELEMENTTYPE_IMAGE types.

numbermaterialAsset

The id of the material asset to use when rendering an image. Only works for ELEMENTTYPE_IMAGE types.

numbermaxFontSize

The maximum size that the font can scale to when autoFitWidth or autoFitHeight are true.

numbermaxLines

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.

numberminFontSize

The minimum size that the font can scale to when autoFitWidth or autoFitHeight are true.

numberopacity

The opacity of the image for ELEMENTTYPE_IMAGE types or the text for ELEMENTTYPE_TEXT types.

ColoroutlineColor

The text outline effect color and opacity. Only works for ELEMENTTYPE_TEXT types.

numberoutlineThickness

The width of the text outline effect. Only works for ELEMENTTYPE_TEXT types.

Vec2, number[]pivot

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.

pc.app.root.findByName("Inventory").element.pivot = [Math.random() * 0.1, Math.random() * 0.1];
pc.app.root.findByName("Inventory").element.pivot = new pc.Vec2(Math.random() * 0.1, Math.random() * 0.1);
numberpixelsPerUnit

The number of pixels that map to one PlayCanvas unit. Only works for ELEMENTTYPE_IMAGE types who have a sliced sprite assigned.

numberrangeEnd

Index of the last character to render. Only works for ELEMENTTYPE_TEXT types.

numberrangeStart

Index of the first character to render. Only works for ELEMENTTYPE_TEXT types.

Vec4rect

Specifies which 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.

booleanrtlReorder

Reorder the text for RTL languages using a function registered by app.systems.element.registerUnicodeConverter.

Entity, nullscreen

The Entity with a ScreenComponent that this component belongs to. This is automatically set when the component is a child of a ScreenComponent.

Vec3[]screenCorners

An 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.

ColorshadowColor

The text shadow effect color and opacity. Only works for ELEMENTTYPE_TEXT types.

Vec2shadowOffset

The text shadow effect shift amount from original text. Only works for ELEMENTTYPE_TEXT types.

numberspacing

The spacing between the letters of the text. Only works for ELEMENTTYPE_TEXT types.

Spritesprite

The sprite to render. Only works for ELEMENTTYPE_IMAGE types which can render either a texture or a sprite.

numberspriteAsset

The id of the sprite asset to render. Only works for ELEMENTTYPE_IMAGE types which can render either a texture or a sprite.

numberspriteFrame

The frame of the sprite to render. Only works for ELEMENTTYPE_IMAGE types who have a sprite assigned.

stringtext

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:

  1. color - override the element's color property. Examples:
  • [color="#ff0000"]red text[/color]
  • [color="#00ff00"]green text[/color]
  • [color="#0000ff"]blue text[/color]
  1. outline - override the element's outlineColor and outlineThickness properties. Example:
  • [outline color="#ffffff" thickness="0.5"]text[/outline]
  1. 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.

numbertextHeight

The height of the text rendered by the component. Only works for ELEMENTTYPE_TEXT types.

Texturetexture

The texture to render. Only works for ELEMENTTYPE_IMAGE types.

numbertextureAsset

The id of the texture asset to render. Only works for ELEMENTTYPE_IMAGE types.

numbertextWidth

The width of the text rendered by the component. Only works for ELEMENTTYPE_TEXT types.

numbertop

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.

stringtype

The type of the ElementComponent. Can be:

booleanunicodeConverter

Convert unicode characters using a function registered by app.systems.element.registerUnicodeConverter.

booleanuseInput

If true then the component will receive Mouse or Touch input events.

numberwidth

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.

Vec3[]worldCorners

An array of 4 Vec3s that represent the bottom left, bottom right, top right and top left corners of the component in world space. Only works for 3D element components.

booleanwrapLines

Whether to automatically wrap lines based on the element width. Only works for ELEMENTTYPE_TEXT types, and when autoWidth is set to false.

Events

click

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.

Parameters

eventElementMouseEvent, ElementTouchEvent

The event.

mousedown

Fired when the mouse is pressed while the cursor is on the component. Only fired when useInput is true.

Parameters

eventElementMouseEvent

The event.

mouseenter

Fired when the mouse cursor enters the component. Only fired when useInput is true.

Parameters

eventElementMouseEvent

The event.

mouseleave

Fired when the mouse cursor leaves the component. Only fired when useInput is true.

Parameters

eventElementMouseEvent

The event.

mousemove

Fired when the mouse cursor is moved on the component. Only fired when useInput is true.

Parameters

eventElementMouseEvent

The event.

mouseup

Fired when the mouse is released while the cursor is on the component. Only fired when useInput is true.

Parameters

eventElementMouseEvent

The event.

mousewheel

Fired when the mouse wheel is scrolled on the component. Only fired when useInput is true.

Parameters

eventElementMouseEvent

The event.

touchcancel

Fired when a touch is canceled on the component. Only fired when useInput is true.

Parameters

eventElementTouchEvent

The event.

touchend

Fired when a touch ends on the component. Only fired when useInput is true.

Parameters

eventElementTouchEvent

The event.

touchmove

Fired when a touch moves after it started touching the component. Only fired when useInput is true.

Parameters

eventElementTouchEvent

The event.

touchstart

Fired when a touch starts on the component. Only fired when useInput is true.

Parameters

eventElementTouchEvent

The event.

Inherited

Properties

booleanenabled

Enables or disables the component.

Entityentity

The Entity that this Component is attached to.

ComponentSystemsystem

The ComponentSystem used to create this Component.

Methods

fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])

Fire an event, all additional arguments are passed on to the event listener.

obj.fire('test', 'This is the message');

Parameters

namestring

Name of event to fire.

arg1*

First argument that is passed to the event handler.

arg2*

Second argument that is passed to the event handler.

arg3*

Third argument that is passed to the event handler.

arg4*

Fourth argument that is passed to the event handler.

arg5*

Fifth argument that is passed to the event handler.

arg6*

Sixth argument that is passed to the event handler.

arg7*

Seventh argument that is passed to the event handler.

arg8*

Eighth argument that is passed to the event handler.

Returns

EventHandler

Self for chaining.

hasEvent(name)

Test if there are any handlers bound to an event name.

obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false

Parameters

namestring

The name of the event to test.

Returns

boolean

True if the object has handlers bound to the specified event name.

off([name], [callback], [scope])

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.

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

Parameters

namestring

Name of the event to unbind.

callbackHandleEventCallback

Function to be unbound.

scopeobject

Scope that was used as the this when the event is fired.

Returns

EventHandler

Self for chaining.

on(name, callback, [scope])

Attach an event handler to an event.

obj.on('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
const evt = obj.on('test', function (a, b) {
    console.log(a + b);
});
// some time later
evt.off();

Parameters

namestring

Name of the event to bind the callback to.

callbackHandleEventCallback

Function that is called when event is fired. Note the callback is limited to 8 arguments.

scopeobject

Object to use as 'this' when the event is fired, defaults to current this.

Returns

EventHandle

Can be used for removing event in the future.

once(name, callback, [scope])

Attach an event handler to an event. This handler will be removed after being fired once.

obj.once('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
obj.fire('test', 1, 2); // not going to get handled

Parameters

namestring

Name of the event to bind the callback to.

callbackHandleEventCallback

Function that is called when event is fired. Note the callback is limited to 8 arguments.

scopeobject

Object to use as 'this' when the event is fired, defaults to current this.

Returns

EventHandle
  • can be used for removing event in the future.