pc.ElementComponent
Extends: pc.Component
Enables an Entity to be positioned using anchors and screen coordinates under a pc.ScreenComponent or under other ElementComponents. Depending on its type it can be used to render images, text or just as a layout mechanism to build 2D and 3D user interfaces. If the component is a descendant of a pc.ScreenComponent, then the Entity's pc.Entity.setLocalPosition is in the pc.ScreenComponent's coordinate system.
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 pc.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 pc.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 pc.ELEMENTTYPE_IMAGE types or the color of the text for pc.ELEMENTTYPE_TEXT types. |
| drawOrder | The draw order of the component. |
| enableMarkup | Flag for enabling markup processing. |
| 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 pc.Application#i18n. |
| layers | An array of layer IDs (pc.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. |
| 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 pc.ELEMENTTYPE_IMAGE types or the text for pc.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 |
| screen | The Entity with a pc.ScreenComponent that this component belongs to. |
| screenCorners | An array of 4 pc.Vec3s that represent the bottom left, bottom right, top right and top left corners of the component relative to its parent pc.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. |
| textWidth | The width of the text rendered by the component. |
| texture | The texture to render. |
| textureAsset | The id of the texture asset to render. |
| 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 |
| 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 pc.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 cancelled 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)
Parameters
| system | pc.ElementComponentSystem | The ComponentSystem that created this Component. |
| entity | pc.Entity | The Entity that this Component is attached to. |
Properties
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 pc.ELEMENTTYPE_TEXT types.
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.
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.
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.
Automatically set the height of the component to be the same as the textHeight. Only works for pc.ELEMENTTYPE_TEXT types.
Automatically set the width of the component to be the same as the textWidth. Only works for pc.ELEMENTTYPE_TEXT types.
Assign element to a specific batch group (see pc.BatchGroup). Default value is -1 (no group).
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.
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 pc.LayoutGroupComponent. In these scenarios, calculatedHeight may be smaller or larger than the height that was set in the editor.
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 pc.LayoutGroupComponent. In these scenarios, calculatedWidth may be smaller or larger than the width that was set in the editor.
An array of 4 pc.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 ElementComponents.
The color of the image for pc.ELEMENTTYPE_IMAGE types or the color of the text for pc.ELEMENTTYPE_TEXT types.
The draw order of the component. A higher value means that the component will be rendered on top of other components.
Flag for enabling markup processing. Only works for pc.ELEMENTTYPE_TEXT types.
The id of the font asset used for rendering the text. Only works for pc.ELEMENTTYPE_TEXT types.
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 pc.LayoutGroupComponent. See calculatedHeight in order to ensure you are reading the true height at which the element will be rendered.
The localization key to use to get the localized text from pc.Application#i18n. Only works for pc.ELEMENTTYPE_TEXT types.
An array of layer IDs (pc.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.
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.
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.
The material to use when rendering an image. Only works for pc.ELEMENTTYPE_IMAGE types.
The id of the material asset to use when rendering an image. Only works for pc.ELEMENTTYPE_IMAGE types.
The maximum size that the font can scale to when autoFitWidth or autoFitHeight are true.
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.
The minimum size that the font can scale to when autoFitWidth or autoFitHeight are true.
The opacity of the image for pc.ELEMENTTYPE_IMAGE types or the text for pc.ELEMENTTYPE_TEXT types.
The text outline effect color and opacity. Only works for pc.ELEMENTTYPE_TEXT types.
The width of the text outline effect. Only works for pc.ELEMENTTYPE_TEXT types.
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.
The number of pixels that map to one PlayCanvas unit. Only works for pc.ELEMENTTYPE_IMAGE types who have a sliced sprite assigned.
Index of the first character to render. Only works for pc.ELEMENTTYPE_TEXT types.
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 pc.ELEMENTTYPE_IMAGE types.
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.
Reorder the text for RTL languages using a function registered by app.systems.element.registerUnicodeConverter.
The Entity with a pc.ScreenComponent that this component belongs to. This is automatically set when the component is a child of a ScreenComponent.
An array of 4 pc.Vec3s that represent the bottom left, bottom right, top right and top left corners of the component relative to its parent pc.ScreenComponent.
The text shadow effect color and opacity. Only works for pc.ELEMENTTYPE_TEXT types.
The text shadow effect shift amount from original text. Only works for pc.ELEMENTTYPE_TEXT types.
The spacing between the letters of the text. Only works for pc.ELEMENTTYPE_TEXT types.
The sprite to render. Only works for pc.ELEMENTTYPE_IMAGE types which can render either a texture or a sprite.
The id of the sprite asset to render. Only works for pc.ELEMENTTYPE_IMAGE types which can render either a texture or a sprite.
The frame of the sprite to render. Only works for pc.ELEMENTTYPE_IMAGE types who have a sprite assigned.
The height of the text rendered by the component. Only works for pc.ELEMENTTYPE_TEXT types.
The width of the text rendered by the component. Only works for pc.ELEMENTTYPE_TEXT types.
The id of the texture asset to render. Only works for pc.ELEMENTTYPE_IMAGE types.
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.
The type of the ElementComponent. Can be:
- pc.ELEMENTTYPE_GROUP: The component can be used as a layout mechanism to create groups of ElementComponents e.g. panels.
- pc.ELEMENTTYPE_IMAGE: The component will render an image
- pc.ELEMENTTYPE_TEXT: The component will render text
Convert unicode characters using a function registered by app.systems.element.registerUnicodeConverter.
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 pc.LayoutGroupComponent. See calculatedWidth in order to ensure you are reading the true width at which the element will be rendered.
An array of 4 pc.Vec3s that represent the bottom left, bottom right, top right and top left corners of the component in world space. Only works for 3D ElementComponents.
Whether to automatically wrap lines based on the element width. Only works for pc.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
| event | pc.ElementMouseEvent, pc.ElementTouchEvent | The event. |
mousedown
Fired when the mouse is pressed while the cursor is on the component. Only fired when useInput is true.
Parameters
| event | pc.ElementMouseEvent | The event. |
mouseenter
Fired when the mouse cursor enters the component. Only fired when useInput is true.
Parameters
| event | pc.ElementMouseEvent | The event. |
mouseleave
Fired when the mouse cursor leaves the component. Only fired when useInput is true.
Parameters
| event | pc.ElementMouseEvent | The event. |
mousemove
Fired when the mouse cursor is moved on the component. Only fired when useInput is true.
Parameters
| event | pc.ElementMouseEvent | The event. |
mouseup
Fired when the mouse is released while the cursor is on the component. Only fired when useInput is true.
Parameters
| event | pc.ElementMouseEvent | The event. |
mousewheel
Fired when the mouse wheel is scrolled on the component. Only fired when useInput is true.
Parameters
| event | pc.ElementMouseEvent | The event. |
touchcancel
Fired when a touch is cancelled on the component. Only fired when useInput is true.
Parameters
| event | pc.ElementTouchEvent | The event. |
touchend
Fired when a touch ends on the component. Only fired when useInput is true.
Parameters
| event | pc.ElementTouchEvent | The event. |
touchmove
Fired when a touch moves after it started touching the component. Only fired when useInput is true.
Parameters
| event | pc.ElementTouchEvent | The event. |
touchstart
Fired when a touch starts on the component. Only fired when useInput is true.
Parameters
| event | pc.ElementTouchEvent | The event. |
Inherited
Properties
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
| name | object | 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
pc.EventHandlerSelf 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
| name | string | The name of the event to test. |
Returns
booleanTrue 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.
var 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 hander functions, called 'test' with scope this
Parameters
| name | string | Name of the event to unbind. |
| callback | pc.callbacks.HandleEvent | Function to be unbound. |
| scope | object | Scope that was used as the this when the event is fired. |
Returns
pc.EventHandlerSelf 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
Parameters
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf for chaining.
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
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf for chaining.