ThreeView Events
This page describes all events available on a ThreeView instance.
Methods
Section titled “Methods”Registers event listeners for various view events.
on<K extends keyof ViewEvents>(event: K, handler: ViewEvents[K]): voidRemoves an event listener.
off<K extends keyof ViewEvents>(event: K, handler: ViewEvents[K]): voidExample:
const resizeHandler = (width, height) => { console.log(`Resized to ${width}x${height}`);};
// Register event listenerview.on("resize", resizeHandler);
// Later, remove the listenerview.off("resize", resizeHandler);Advanced Example
Section titled “Advanced Example”// Create named handlers for easy cleanupconst handlers = { handleClick: (event) => { console.log("Clicked:", event); }, handleResize: (width, height) => { console.log(`Resized: ${width}x${height}`); }, handlePick: (info) => { if (info) { console.log("Picked:", info.properties); } },};
// Register multiple listenersview.on("click", handlers.handleClick);view.on("resize", handlers.handleResize);view.on("featureClick", handlers.handlePick);
// Later, cleanup all listenersview.off("click", handlers.handleClick);view.off("resize", handlers.handleResize);view.off("featureClick", handlers.handlePick);Event Types
Section titled “Event Types”resize
Section titled “resize”Description:
Fires when the window is resized. Receives width and height in pixels.
Handler Type:
(width: number, height: number) => voidParameters:
width: Width after resize (pixels)height: Height after resize (pixels)
Example:
view.on("resize", (width, height) => { console.log(`Window resized: ${width}x${height}`);});featureClick
Section titled “featureClick”Description:
Fires when a feature is clicked or tapped. Receives the clicked feature information, or null if nothing pickable was clicked, and the click’s pointer event. It fires for any mouse button, so check event.button === 0 to act on the left button only. For a left click it fires before the click event, which receives the same event object.
The click pick is lazy: the GPU pick runs only while at least one featureClick listener is registered.
Handler Type:
(info: PickedFeature | null, event: MapPointerEvent | PointerEvent) => voidParameters:
info: Clicked feature information, ornullevent: The click’spointerupevent, passed also wheninfoisnull. It is aMapPointerEventwith themapcoordinates of theclickevent, or a plainPointerEventwithoutmapwhere the pointer is off the globe (the sky)
type PickedFeature = { batchId: number; // Picking ID of the rendered feature, valid while its feature set exists properties: Record<string, unknown> | undefined; // Feature properties layerId: string | undefined; // Layer ID featureId?: string | number; // Top-level GeoJSON `id` or MVT feature id (undefined when the feature has none) featureSetId?: FeatureSetId; // Feature set ID, as in the layer's featureCreated event (undefined outside a feature set) content?: FeatureSetContent; // Source content of the feature set (3D Tiles credit and metadata), as in featureCreated};To keep a selection, match it by featureId or a stable property, not by batchId: see Identifiers.
Example:
view.on("featureClick", (info) => { if (info) { console.log("Selected feature:", info.properties); console.log("Layer ID:", info.layerId); console.log("Batch ID:", info.batchId); } else { console.log("No feature selected"); }});The point drawn under the pointer
Section titled “The point drawn under the pointer”event.map is where the camera ray meets the ellipsoid, which ignores buildings and terrain. For the point drawn at the clicked pixel, such as a roof, a model or the terrain, call view.pickDepthPosition(event.clientX, event.clientY) in the listener itself. Until the next frame is rendered, the depth buffer still holds the frame on screen, so it returns the point under the pointer, or null where nothing is drawn (the sky). Called later, after an await or in a timeout, it may read a newer frame.
import { vector3ToGeodetic } from "@navaramap/three";
view.on("featureClick", (info, event) => { console.log(`Screen position: ${event.clientX}, ${event.clientY}`); if ("map" in event) { console.log(`On the ellipsoid (ECEF): ${event.map.x}, ${event.map.y}, ${event.map.z}`); } const drawn = view.pickDepthPosition(event.clientX, event.clientY); if (info && drawn) { const { lng, lat, height } = vector3ToGeodetic(drawn); console.log(`Clicked ${info.featureId} at ${lng}, ${lat}, ${height} m`); }});The same holds in featureHover, featureEnter and featureLeave listeners. In two cases the drawn point is not on the feature:
- The feature is searched within a few pixels around the pointer (
PICK_RADIUS, orTOUCH_PICK_RADIUSfor touch), while the depth is read at the pixel itself. A click just beside a thin line returns the line and the ground behind it, and a click a few pixels outside a feature’s silhouette against the sky returns the feature andnull. - When the underground is shown (
globe.hideUnderground = false), the depth keeps whichever of the terrain and the other objects is nearer to the camera, so for an object drawn below the terrain, such as an underground pipe,infois that object while the drawn point is the terrain surface above it.
featureHover
Section titled “featureHover”Description:
Fires when the hovered feature changes as the pointer moves. Receives the newly hovered feature, or null when the pointer leaves all pickable features, and the pointer event the feature was picked for. The event fires only on change, not on every pointer move.
Hover picking runs a GPU pick per frame while the pointer moves, so it is activated lazily: picks run only while at least one featureHover, featureEnter, or featureLeave listener is registered, and are suppressed while a button or finger is pressed (for example, during a camera drag). Because touch contact always counts as pressed, hover events never fire for touch.
Handler Type:
(info: PickedFeature | null, event: MapPointerEvent | PointerEvent) => voidParameters:
info: Hovered feature information, ornullevent: The latestpointermovebefore the pick, or thepointerleavewhen the pointer leaves the canvas. It is aMapPointerEvent, or a plainPointerEventwithoutmapoff the globe, as infeatureClick; see The point drawn under the pointer. Apointermoveis delivered with the pick about one frame later, after its dispatch has ended, sopreventDefault()andstopPropagation()have no effect on it and itscurrentTargetisnull
Example:
view.on("featureHover", (info) => { view.canvas.style.cursor = info ? "pointer" : "";});featureEnter
Section titled “featureEnter”Description:
Fires when the pointer starts hovering a pickable feature. Together with featureLeave, this is synthesized from the same hover picking as featureHover, so the same activation rules apply.
Handler Type:
(info: PickedFeature, event: MapPointerEvent | PointerEvent) => voidParameters:
info: The feature the pointer started hoveringevent: The pointer event, as infeatureHover
Example:
view.on("featureEnter", (info) => { console.log("Entered feature:", info.properties);});featureLeave
Section titled “featureLeave”Description:
Fires when the pointer stops hovering the previously hovered feature. Receives the feature that was left. When the pointer moves directly from one feature to another, featureLeave fires for the previous feature before featureEnter fires for the new one.
Handler Type:
(info: PickedFeature, event: MapPointerEvent | PointerEvent) => voidParameters:
info: The feature the pointer stopped hoveringevent: The pointer event, as infeatureHover: thepointerleavewhen the pointer left the canvas
Example:
view.on("featureLeave", (info) => { console.log("Left feature:", info.properties);});Description:
Fires when a layer-related event occurs.
Handler Type:
<K extends keyof LayerEvent>( k: K, layerId: string, ...args: Parameters<LayerEvent[K]>) => voidExample:
view.on("layer", (eventType, layerId, ...args) => { console.log(`Layer ${layerId} event: ${eventType}`, args);});preUpdate
Section titled “preUpdate”Description:
Fires on every frame before the update process, and before the layers re-evaluate their features: a layer.forceUpdate() called here is evaluated in the same frame. Receives a DOMHighResTimeStamp as a timestamp.
Handler Type:
(time: number) => voidParameters:
time:DOMHighResTimeStamp(high-precision timestamp in milliseconds)
Example:
view.on("preUpdate", (time) => { // Custom logic before update console.log(`Before update: ${time}ms`);});postUpdate
Section titled “postUpdate”Description:
Fires after the update process when state changes have occurred. Receives a DOMHighResTimeStamp as a timestamp.
Handler Type:
(time: number) => voidParameters:
time:DOMHighResTimeStamp(high-precision timestamp in milliseconds)
Example:
view.on("postUpdate", (time) => { // Custom logic after update console.log(`After update: ${time}ms`);});preRender
Section titled “preRender”Description:
Fires before rendering. When animation: true, fires every frame. Receives a DOMHighResTimeStamp as a timestamp.
Handler Type:
(time: number) => voidParameters:
time:DOMHighResTimeStamp(high-precision timestamp in milliseconds)
Example:
view.on("preRender", (time) => { // Custom logic before rendering console.log(`Before render: ${time}ms`);});postRender
Section titled “postRender”Description:
Fires after rendering. When animation: true, fires every frame. Receives a DOMHighResTimeStamp as a timestamp.
Handler Type:
(time: number) => voidParameters:
time:DOMHighResTimeStamp(high-precision timestamp in milliseconds)
Example:
view.on("postRender", (time) => { // Custom logic after rendering console.log(`After render: ${time}ms`);});pointerdown
Section titled “pointerdown”Description:
Fires when a pointer (mouse button, touch, or pen) is pressed on the map. Receives a MapPointerEvent containing map coordinates. Use event.pointerType ("mouse", "touch", or "pen") to tell the input kinds apart.
Handler Type:
(event: MapPointerEvent) => voidParameters:
event: Pointer event (containing map coordinates)
type MapPointerEvent = { map: { x: number; y: number; z: number }; // ECEF coordinates on the globe surface} & PointerEvent;Example:
view.on("pointerdown", (event) => { console.log(`Pointer down position: ${event.clientX}, ${event.clientY}`); console.log( `Map coordinates (ECEF): ${event.map.x}, ${event.map.y}, ${event.map.z}` );});pointerenter
Section titled “pointerenter”Description:
Fires when a pointer enters the canvas area. Receives a MapPointerEvent containing map coordinates.
Handler Type:
(event: MapPointerEvent) => voidParameters:
event: Pointer event (containing map coordinates)
Example:
view.on("pointerenter", (event) => { console.log("Pointer entered the map"); console.log(`Map coordinates: ${event.map.x}, ${event.map.y}, ${event.map.z}`);});pointerleave
Section titled “pointerleave”Description:
Fires when a pointer leaves the canvas area. Receives a MapPointerEvent containing map coordinates. For touch, this fires after the finger is lifted.
Handler Type:
(event: MapPointerEvent) => voidParameters:
event: Pointer event (containing map coordinates)
Example:
view.on("pointerleave", (event) => { console.log("Pointer left the map");});pointermove
Section titled “pointermove”Description:
Fires when a pointer moves on the map. Receives a MapPointerEvent containing map coordinates. For touch, this fires while a finger drags across the map.
Handler Type:
(event: MapPointerEvent) => voidParameters:
event: Pointer event (containing map coordinates)
Example:
view.on("pointermove", (event) => { console.log(`Pointer position: ${event.clientX}, ${event.clientY}`); console.log( `Map coordinates (ECEF): ${event.map.x}, ${event.map.y}, ${event.map.z}` );});pointerup
Section titled “pointerup”Description:
Fires when a pointer (mouse button, touch, or pen) is released on the map. Receives a MapPointerEvent containing map coordinates.
Handler Type:
(event: MapPointerEvent) => voidParameters:
event: Pointer event (containing map coordinates)
Example:
view.on("pointerup", (event) => { console.log(`Pointer up position: ${event.clientX}, ${event.clientY}`); console.log(`Map coordinates: ${event.map.x}, ${event.map.y}, ${event.map.z}`);});pointercancel
Section titled “pointercancel”Description:
Fires when the browser cancels an active pointer, for example when a system gesture takes over a touch. Receives the raw PointerEvent without map coordinates.
Handler Type:
(event: PointerEvent) => voidExample:
view.on("pointercancel", () => { console.log("Pointer interaction cancelled");});Description:
Fires when data and tile processing becomes idle, that is, when no updates such as tile loading or data processing have occurred for at least idleThreshold milliseconds. Continuous animations and effects do not count as activity, so this event fires even while they are running. It fires at most once per idle period and resets when processing activity resumes.
Handler Type:
() => voidExample:
view.on("idle", () => { console.log("Data and tile processing is idle");});Description:
Fires when the map is clicked or tapped. Receives a MapPointerEvent containing map coordinates; event.pointerType tells the input kinds apart.
Handler Type:
(event: MapPointerEvent) => voidParameters:
event: Pointer event (containing map coordinates)
Example:
view.on("click", (event) => { console.log(`Click position: ${event.clientX}, ${event.clientY}`); console.log(`Input type: ${event.pointerType}`); console.log( `Map coordinates (ECEF): ${event.map.x}, ${event.map.y}, ${event.map.z}` );});