HomeWeb Components API Reference - v0.31.0
    Preparing search index...

    Class EntityElement

    The EntityElement interface provides properties and methods for manipulating <pc-entity> elements. The EntityElement interface also inherits the properties and methods of the HTMLElement interface.

    An element added to an application that is already running is built in one go, along with everything declared under it: its entity and those of the entities below it, the content of every <pc-model> whose asset has loaded, and every component with its scripts. The entity joins the scene last, which enables the whole hierarchy at once, as addChild() does for a hierarchy built through the engine: every script in it runs initialize(), and then every script runs postInitialize(), before the insertion returns. Each element reports ready as it is built, which is before the entity has joined the scene. This holds however the subtree arrives: built with createElement(), parsed by innerHTML or insertAdjacentHTML(), or cloned from a <template>.

    Entities and models that one operation inserts side by side - the children of a fragment or a cloned <template>, the top-level elements of innerHTML or insertAdjacentHTML(), the arguments of append() - are all built before any of them joins the scene, so each can refer to the others: an entity: reference from one to another resolves, and its components and scripts are in place. They then join the scene one after the other, in document order, each as a hierarchy of its own, as they would in as many calls to addChild(). So the scripts of the first have initialized and post-initialized before the second joins, and until it joins the second is not in the scene graph, where findByName() would look for it. Put them under one entity to have their scripts initialize together.

    So what is inserted is configured before it is inserted, as a hierarchy is before addChild(): its scripts initialize with the attributes it has when it is inserted, and it can refer to what arrives with it or is already in the document, but not to what a later operation inserts.

    The pointer events below are dispatched by the containing <pc-app> element when the pointer is over this entity's geometry, and behave like the DOM's own pointer events: each targets the element fronting the deepest node under the pointer and propagates up the element tree, so a listener on an ancestor entity, or on <pc-scene>, also receives the events of the entities below it. pointerenter and pointerleave do not bubble - they are dispatched to each element the pointer moves onto or off. <pc-app> picks the scene for an event type while a listener for it is registered on an entity element or on <pc-scene>; its picking attribute covers listeners it cannot see, such as a framework's delegated handlers.

    enabled - The enabled state of the entity.

    name - The name of the entity.

    position - The position of the entity.

    rotation - The rotation of the entity.

    scale - The scale of the entity.

    tags - The tags of the entity.

    onpointerover - Script to run when the pointer moves onto the entity, or onto an entity below it.

    onpointerenter - Script to run when the pointer moves onto the entity or an entity below it, having been over none of them.

    onpointermove - Script to run when the pointer moves over the entity.

    onpointerdown - Script to run when a pointer button is pressed over the entity.

    onpointerup - Script to run when a pointer button is released over the entity.

    onpointercancel - Script to run when the browser cancels a press that began over the entity, for example because a touch became a scroll.

    onpointerout - Script to run when the pointer moves off the entity, or off an entity below it.

    onpointerleave - Script to run when the pointer moves off the entity and every entity below it.

    onclick - Script to run when the entity is clicked: a primary pointer button pressed and then released over it.

    pointerover - Fired when the pointer moves onto the entity. Bubbles; relatedTarget is the element the pointer came from, which is <pc-app> when it came from the background.

    pointerenter - Fired when the pointer moves onto the entity or an entity below it, having been over none of them. Does not bubble.

    pointermove - Fired when the pointer moves over the entity.

    pointerdown - Fired when a pointer button is pressed over the entity.

    pointerup - Fired when a pointer button is released over the entity.

    pointercancel - Fired on the entity a press began over when the browser cancels that press, for example because a touch became a scroll. No click follows.

    pointerout - Fired when the pointer moves off the entity. Bubbles; relatedTarget is the element the pointer went to, which is <pc-app> when it went to the background.

    pointerleave - Fired when the pointer moves off the entity and every entity below it. Does not bubble.

    click - Fired when a primary pointer button is pressed and then released over the entity. A press and release that picked different elements fires on their nearest common ancestor instead, as in the DOM. detail carries the click count, so a double click arrives as a click whose detail is 2.

    Hierarchy (View Summary)

    Index
    _appElement: AppElement | null = null

    The application element this entity is registered with, cached at registration time so the entity can be unregistered even once this element has left the DOM.

    _built: boolean = false

    Whether this entity has its place in the hierarchy: it has been parented, or it is the root of an inserted subtree, which is parented once the subtree under it is built. Read by subclasses that gate work on the entity being built.

    _connected: boolean = false

    Whether the element has connected: its connectedCallback has run since it was last put in the document. Set and cleared by the subclasses, whose callbacks those are. An element in the document that has yet to connect has only just arrived, in the DOM operation that is still connecting what it inserted - which is how a root tells the elements that arrived with it from the ones that were there before.

    enabled: boolean

    The enabled state of the entity.

    name: string

    The name of the entity.

    • get closestApp(): AppElement | null

      The nearest ancestor <pc-app> element, or null if this element has no <pc-app> ancestor. The search starts at the parent, so an element never resolves to itself.

      Returns AppElement | null

      The closest app element, or null.

    • get closestEntity(): EntityBaseElement | null

      The nearest ancestor element that fronts an entity — <pc-entity>, <pc-model> or <pc-node> — or null if this element has no such ancestor. The search starts at the parent, so an element never resolves to itself.

      Returns EntityBaseElement | null

      The closest entity-fronting element, or null.

    • get entity(): Entity | null

      The PlayCanvas entity instance. null until the element is ready, and again once the entity is gone — await whenReady or the element's ready() promise before accessing it.

      Returns Entity | null

      The entity instance, or null.

    • get position(): Vec3

      Gets the position of the entity.

      Returns Vec3

      The position of the entity.

    • set position(value: Vec3): void

      Sets the position of the entity.

      Parameters

      • value: Vec3

        The position of the entity.

      Returns void

    • get rotation(): Vec3

      Gets the rotation of the entity.

      Returns Vec3

      The rotation of the entity.

    • set rotation(value: Vec3): void

      Sets the rotation of the entity.

      Parameters

      • value: Vec3

        The rotation of the entity.

      Returns void

    • get scale(): Vec3

      Gets the scale of the entity.

      Returns Vec3

      The scale of the entity.

    • set scale(value: Vec3): void

      Sets the scale of the entity.

      Parameters

      • value: Vec3

        The scale of the entity.

      Returns void

    • get tags(): string[]

      Gets the tags of the entity.

      Returns string[]

      The tags of the entity.

    • set tags(value: string[]): void

      Sets the tags of the entity.

      Parameters

      • value: string[]

        The tags of the entity.

      Returns void

    • Builds this element and everything declared under it, in an application whose hierarchy already exists - along with the elements that arrived beside it. Called when the element connects.

      Everything is assembled before any of it joins the scene: the entities, the content of every model whose container has loaded, and every component with the script instances whose class is registered. Adding the root entity to its parent then enables the whole hierarchy at once, as the engine does for a hierarchy handed to addChild: every script in it runs initialize(), and then every script runs postInitialize(), with every entity and component in place. Each element reports ready as it is built, which is before its root joins the scene.

      One DOM operation can insert several entity-owning elements side by side: the children of a fragment or a cloned template, of innerHTML, or the arguments of append. The first of them to connect builds them all, so that what one declares can refer to another, and then adds them to the scene one after the other, in document order. Each is a hierarchy of its own to the engine, as each would be in as many calls to addChild: the scripts of one have initialized and post-initialized before the next joins the scene.

      While the application is still preloading, only the entities are built. The application builds every component under it once preload has finished, and starts its scripts together.

      An element whose ancestor is assembling the subtree it belongs to, or which another element has built, has nothing left to do here.

      Parameters

      • appElement: AppElement

        The application element, which has built its hierarchy.

      Returns void

    • Called once the backing entity is built — exactly once per build cycle: by _buildHierarchy when it has parented the entity, and by _buildSubtree for the root of an inserted subtree, which is parented last. The default announces readiness, which is what a built <pc-entity> means; <pc-model> overrides it to start loading content instead, because its readiness tracks the content settling rather than the host being built.

      Returns void

    • Handles the destruction of the backing entity. Resets the element so a later re-insertion starts clean: _built must be cleared alongside _entity, or _buildHierarchy would bail and a re-created entity would never be parented. Readiness is re-armed for the same reason — with the entity gone, a resolved ready promise would resume its awaiters against a null entity.

      Parameters

      • entity: Entity

        The entity that was destroyed.

      Returns void

    • Called when the element is fully initialized and ready. Subclasses should call this when they're ready. Resolves the ready promise and dispatches a bubbling, composed ready event. Signals at most once per readiness cycle: a repeat call before _resetReady has re-armed the promise does nothing.

      Returns void

    • Registers entity as this element's backing entity with the owning application, which joins engine nodes back to elements by identity (never by name).

      Parameters

      • entity: Entity

        The entity to register.

      Returns void

    • Returns the ready promise to its pending state. Subclasses should call this when the resource their readiness announced is torn down (typically from disconnectedCallback), so that a later re-initialization can signal readiness again. Does nothing while the promise is still pending — an in-flight waiter carries over to the next readiness cycle rather than being stranded on a promise nothing will ever resolve.

      Returns void

    • Returns a promise that resolves with this element when it's ready. This is the low-level primitive underlying whenReady, which is the recommended way to wait for elements.

      Readiness tracks the element's current lifecycle: once a ready element is torn down (for example by removing it from the document), this returns a fresh promise that resolves when the element is next ready. A promise obtained earlier stays resolved — call this again after re-inserting an element rather than reusing a promise from before its removal.

      Returns Promise<EntityElement>

      A promise that resolves with this element when it's ready.