Event Listeners
As there are different kind of events that you might be interested in, the viewer has an event system to listen to them. The event listener is called whenever this event is emitted. The event listener is added with a callback and can be removed with the token that has been received.
In the following example, we register an event listener whenever the scene has been customized.
// create a listener that is called whenever the session has been customized
const token = addListener(EVENTTYPE.SESSION.SESSION_CUSTOMIZED, (e) => {
console.log("Session customized", `Session customized: ${JSON.stringify(e)}`);
});
session.getParameterByName("Length")[0].value = 10;
await session.customize();
// once you are done listening, remove the listener
// removeListener(token)
Available Event Types
The viewer provides several categories of events through the EVENTTYPE object. Below is a summary of the most commonly used events in each category.
Session Events (EVENTTYPE.SESSION)
Event | Fires when |
|---|---|
| A new session has been created. |
| A customization request completes and the scene is updated with new outputs. |
| The initial outputs of a session have been loaded and are ready. |
| A session has been closed. |
| An error occurred in the session. |
Viewport Events (EVENTTYPE.VIEWPORT)
Event | Fires when |
|---|---|
| A new viewport has been created. |
| The viewport has been updated. |
| The viewport busy mode has started (a session is computing). |
| The viewport busy mode has ended. |
Camera Events (EVENTTYPE.CAMERA)
Event | Fires when |
|---|---|
| The user begins interacting with the camera (orbit, pan, zoom). |
| The user stops interacting with the camera. |
| The camera position or target changes (fires continuously during interaction). |
Interaction Events (EVENTTYPE.INTERACTION)
Event | Fires when |
|---|---|
| An object has been selected. |
| An object has been deselected. |
| The mouse hovers over an interactive object. |
| The mouse leaves an interactive object. |
| A drag interaction begins. |
| A dragged object moves. |
| A drag interaction ends. |
Scene Events (EVENTTYPE.SCENE)
Event | Fires when |
|---|---|
| The bounding box of the scene has changed. |
| The bounding box of the scene is empty. |
Output Events (EVENTTYPE.OUTPUT)
Event | Fires when |
|---|---|
| An output has been updated with new content after a customization. |
Task Events (EVENTTYPE.TASK)
Event | Fires when |
|---|---|
| A task (e.g., customization request) has started. |
| A task progress has been updated. |
| A task has ended. |
| A task has been cancelled. |
Parameter Events (EVENTTYPE.PARAMETER)
Event | Fires when |
|---|---|
| A parameter's value has changed (locally, before customize). |
| A parameter's session value has changed (after a successful customization). |
For the complete list of events and their payload types, see the EVENTTYPE reference in the API documentation.
Listening to Multiple Events
You can register listeners for multiple event types. Each listener returns a token that you can use to remove it later:
// Listen for camera changes
const cameraToken = addListener(EVENTTYPE.CAMERA.CAMERA_END, (e) => {
console.log("Camera stopped moving", e);
});
// Listen for selection events
const selectToken = addListener(EVENTTYPE.INTERACTION.SELECT_ON, (e) => {
console.log("Selection changed", e);
});
// Clean up when done
removeListener(cameraToken);
removeListener(selectToken);
Always remove listeners when they are no longer needed, especially in frameworks like React where components unmount and remount. See the Troubleshooting page for guidance on avoiding stale closures in React event handlers.