Fundamentals#

This page explains core concepts and patterns that are repeated across the webvis API.

CRUD stands for the four basic operations on any entity: Create, Read, Update and Delete. webvis exposes exactly these, and it is built around numeric IDs rather than object references. You create an entity, get back an ID, and then read / update / delete it by passing that ID to later calls.

Example - Create a new node entity, enable it, read its label, and remove it:

const nodeID = context.add({ dataURI: "urn:x-i3d:examples:x3d:v8" });   // CREATE → id
await context.setProperty(nodeID, webvis.Property.ENABLED, true);       // UPDATE
const label = await context.getProperty(nodeID, webvis.Property.LABEL); // READ
await context.remove(nodeID);                                           // DELETE

This create-returns-ID pattern is used not only by the scene graph but also by most other webvis APIs: clip planes, measurements, snapshots and annotations are first-class entities which are each created through their own API, hand you an ID on creation, and are modified or removed by that ID. Only the method names and the properties object differ.

Example - AnnotationAPI CRUD:

// CREATE → id
const annotationID = context.createAnnotation({
    content: "Check this weld",
    anchorPosition: [0, 0, 0],
    connectedNodeId: nodeID,
    enabled: true
});
const data = await context.requestAnnotationData(annotationID);  // READ
context.changeAnnotation(annotationID, { content: "Reviewed" }); // UPDATE
context.removeAnnotation(annotationID);                          // DELETE

webvis is reactive. State is not pushed to you synchronously; instead, every mutation emits a subclass of the webvis base event, and you subscribe to the EventTypes you care about. This keeps your code loosely coupled to webvis internals (and to asynchronous results streaming from the hub).

const listenerID = context.registerListener(
    [webvis.EventType.NODE_ADDED],     // event types (empty array = all)
    (event) => { /* ... */ }           // your callback
);

// later
context.unregisterListener(listenerID);

EventAPI.registerListener also takes two optional trailing arguments - a nodeID and a subtree flag - to scope a listener to one node and its children. They only affect node-related events; see EventAPI for the full mechanism.