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

    Class ModelElement

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

    The element creates and fronts a stable host entity: entity is that host, created when the application builds its hierarchy and kept across asset changes, so the element's transform and tags are instance placement that composes with whatever transform the asset authored on its root. The instantiated content is parented beneath the host and exposed as contentEntity.

    The element becomes ready once its current asset selection has settled: the container asset has loaded and its content root has been parented beneath the host, the load has failed (contentEntity stays null — listen for error, or check contentEntity, to tell the outcomes apart), or no asset is assigned at all. Changing asset re-arms readiness and instantiates anew, so a ready() obtained after the change resolves against the new content. A pc-model outside a pc-app, or referencing an unknown asset id, warns and never becomes ready.

    The pointer events below are dispatched by the containing <pc-app> element when the pointer intersects the model's geometry, exactly as for <pc-entity> — a hit on a content node that no pc-node fronts resolves to this element.

    enabled - The enabled state of the model.

    name - The name of the model.

    position - The position of the model.

    rotation - The rotation of the model.

    scale - The scale of the model.

    tags - The tags of the model.

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

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

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

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

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

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

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

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

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

    pointerover - Fired when the pointer moves onto the model. 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 model or an entity below it, having been over none of them. Does not bubble.

    pointermove - Fired when the pointer moves over the model.

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

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

    pointercancel - Fired on the model 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 model. 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 model and every entity below it. Does not bubble.

    click - Fired when a primary pointer button is pressed and then released over the model. 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.

    load - Fired each time a container asset finishes instantiating, including re-instantiation after asset changes. Does not bubble — listen on this element, or use a capture-phase listener on an ancestor.

    error - Fired when the container asset fails to load, with the engine's error in message. Does not bubble. The element still becomes ready — readiness means the load settled, not that it succeeded.

    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 asset(): string

      Gets the id of the pc-asset to use for the model.

      Returns string

      The asset ID.

    • set asset(value: string): void

      Sets the id of the pc-asset to use for the model.

      Parameters

      • value: string

        The asset ID.

      Returns void

    • 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 contentEntity(): Entity | null

      The root entity of the instantiated model content, parented beneath the host entity. null until the container asset has loaded and been instantiated, after a failed load, and again once the element has been removed from the document.

      Returns Entity | null

      The content root entity, 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

    • Starts (or restarts) the content load once the host has been built. Readiness is not announced here — it tracks the content settling, not the host being built.

      Returns void

    • Extends the owner reset for the content: the engine's destroy cascade has already taken the content root down with the host subtree, so only the reference and the in-flight load are dropped here. The next build re-creates the host and re-instantiates the content.

      Parameters

      • entity: Entity

        The host 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 snapshot of the instantiated node tree, or null while there is none (the container asset has not loaded, or the element has left the document). One call grounds a session — a browser console, a test, an agent — in the vocabulary pc-node binding resolves against: the instantiated names (HierarchyNode.name), paths, match indices, attached component types and the material assignments of render components (HierarchyNode.materials). String(...) of the result, or of any node in it, is the printable form.

      The snapshot is plain data, computed afresh each call: it does not follow later changes to the hierarchy, and mutating it changes nothing. It covers the instantiated content only — the host entity the element fronts is not part of the asset's node tree.

      Returns HierarchyNode | null

      The root of the instantiated node tree, or null.

    • 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<ModelElement>

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