Component
Direct Subclass:
Indirect Subclass:
Base class for all xeokit components.
Component IDs
Every Component has an ID that's unique within the parent Scene. xeokit generates the IDs automatically by default, however you can also specify them yourself. In the example below, we're creating a scene comprised of Scene, Material, ReadableGeometry and Mesh components, while letting xeokit generate its own ID for the ReadableGeometry:
import {Viewer, Mesh, buildTorusGeometry, ReadableGeometry, PhongMaterial, Texture, Fresnel} from "xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas"
});
viewer.scene.camera.eye = [0, 0, 5];
viewer.scene.camera.look = [0, 0, 0];
viewer.scene.camera.up = [0, 1, 0];
new Mesh(viewer.scene, {
geometry: new ReadableGeometry(viewer.scene, buildTorusGeometry({
center: [0, 0, 0],
radius: 1.5,
tube: 0.5,
radialSegments: 32,
tubeSegments: 24,
arc: Math.PI * 2.0
}),
material: new PhongMaterial(viewer.scene, {
id: "myMaterial",
ambient: [0.9, 0.3, 0.9],
shininess: 30,
diffuseMap: new Texture(viewer.scene, {
src: "textures/diffuse/uvGrid2.jpg"
}),
specularFresnel: new Fresnel(viewer.scene, {
leftColor: [1.0, 1.0, 1.0],
rightColor: [0.0, 0.0, 0.0],
power: 4
})
})
});
We can then find those components like this:
// Find the Material
var material = viewer.scene.components["myMaterial"];
// Find all PhongMaterials in the Scene
var phongMaterials = viewer.scene.types["PhongMaterial"];
// Find our Material within the PhongMaterials
var materialAgain = phongMaterials["myMaterial"];
Restriction on IDs
Auto-generated IDs are of the form "__0"
, "__1"
, "__2"
... and so on.
Scene maintains a map of these IDs, along with a counter that it increments each time it generates a new ID.
If Scene has created the IDs listed above, and we then destroy the Component
with ID "__1"
,
Scene will mark that ID as available, and will reuse it for the next default ID.
Therefore, two restrictions your on IDs:
- don't use IDs that begin with two underscores, and
- don't reuse auto-generated IDs of destroyed Components.
Logging
Components have methods to log ID-prefixed messages to the JavaScript console:
material.log("Everything is fine, situation normal.");
material.warn("Wait, whats that red light?");
material.error("Aw, snap!");
The logged messages will look like this in the console:
[LOG] myMaterial: Everything is fine, situation normal.
[WARN] myMaterial: Wait, whats that red light..
[ERROR] myMaterial: Aw, snap!
Destruction
Get notification of destruction of Components:
material.once("destroyed", function() {
this.log("Component was destroyed: " + this.id);
});
Or get notification of destruction of any Component within its Scene:
scene.on("componentDestroyed", function(component) {
this.log("Component was destroyed: " + component.id);
});
Then destroy a component like this:
material.destroy();
Constructor Summary
Public Constructor | ||
public |
|
Member Summary
Public Members | ||
public |
True as soon as this Component has been destroyed |
|
public |
ID of this Component, unique within the Scene. |
|
public |
meta: * Arbitrary, user-defined metadata on this component. |
|
public get |
The Component that owns the lifecycle of this Component, if any. |
|
public |
The parent Scene that contains this Component. |
|
public |
The viewer that contains this Scene. |
Method Summary
Public Methods | ||
public |
clear() Destroys all Components that are owned by this. |
|
public |
destroy() Destroys this component. |
|
public |
Logs an error for this component to the JavaScript console. |
|
public |
Fires an event on this component. |
|
public |
Returns true if there are any subscribers to the given event on this component. |
|
public |
Tests if this component is of the given type, or is a subclass of the given type. |
|
public |
Logs a console debugging message for this component. |
|
public |
Cancels an event subscription that was previously made with Component#on or Component#once. |
|
public |
Subscribes to an event on this component. |
|
public |
Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is subIdd. |
|
public |
scheduleTask(task: *) Schedule a task to perform on the next browser interval |
|
public |
Logs a warning for this component to the JavaScript console. |
Public Constructors
public constructor() source
Public Members
public destroyed: Boolean source
True as soon as this Component has been destroyed
Properties:
Name | Type | Attribute | Description |
destroyed | * |
public id: String | Number source
ID of this Component, unique within the Scene.
Components are mapped by this ID in Scene#components.
Properties:
Name | Type | Attribute | Description |
id | * |
public meta: * source
Arbitrary, user-defined metadata on this component.
Properties:
Name | Type | Attribute | Description |
metadata | * |
public get owner: Component source
The Component that owns the lifecycle of this Component, if any.
When that component is destroyed, this component will be automatically destroyed also.
Will be null if this Component has no owner.
Properties:
Name | Type | Attribute | Description |
owner | * |
Public Methods
public clear() source
Destroys all Components that are owned by this. These are Components that were instantiated with this Component as their first constructor argument.
public error(message: String) source
Logs an error for this component to the JavaScript console.
The console message will have this format: [ERROR] [<component type> =<component id>: <message>
Also fires the message as an "error" event on the parent Scene.
Params:
Name | Type | Attribute | Description |
message | String | The message to log |
public fire(event: String, value: Object, forget: Boolean) source
Fires an event on this component.
Notifies existing subscribers to the event, optionally retains the event to give to any subsequent notifications on the event as they are made.
public hasSubs(event: String): Boolean source
Returns true if there are any subscribers to the given event on this component.
Params:
Name | Type | Attribute | Description |
event | String | The event |
public isType(type: *): *: Boolean source
Tests if this component is of the given type, or is a subclass of the given type.
Params:
Name | Type | Attribute | Description |
type | * |
Return:
* |
public log(message: String) source
Logs a console debugging message for this component.
The console message will have this format: [LOG] [<component type> <component id>: <message>
Also fires the message as a "log" event on the parent Scene.
Params:
Name | Type | Attribute | Description |
message | String | The message to log |
public off(subId: String) source
Cancels an event subscription that was previously made with Component#on or Component#once.
Params:
Name | Type | Attribute | Description |
subId | String | Subscription ID |
public on(event: String, callback: Function, scope: Object): String source
Subscribes to an event on this component.
The callback is be called with this component as scope.
public once(event: String, callback: Function, scope: Object) source
Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is subIdd.
This is equivalent to calling Component#on, and then calling Component#off inside the callback function.
public scheduleTask(task: *) source
Schedule a task to perform on the next browser interval
Params:
Name | Type | Attribute | Description |
task | * |
public warn(message: String) source
Logs a warning for this component to the JavaScript console.
The console message will have this format: [WARN] [<component type> =<component id>: <message>
Also fires the message as a "warn" event on the parent Scene.
Params:
Name | Type | Attribute | Description |
message | String | The message to log |