Skip to main content

Overview

All Photo Sphere Viewer objects — the Viewer and plugins — implement the standard EventTarget API. Events are strongly typed in TypeScript via a custom TypedEventTarget interface. Each event listener receives a single Event subclass with additional properties:
  • type — the event name string
  • target — reference to the viewer (or plugin) that dispatched the event

Listening to events

You can use the named event constant (preferred, for TypeScript type inference) or the plain string:
import { events } from '@photo-sphere-viewer/core';

// Using a constant — gives you full type information on `e`
viewer.addEventListener(events.PositionUpdatedEvent.type, (e) => {
  // e.type === 'position-updated'
  // e.target === viewer
  // e.position === { yaw: number, pitch: number }
  console.log(e.position);
});

// Using a plain string — simpler but no extra type inference
viewer.addEventListener('position-updated', ({ position }) => {
  console.log(position);
});
The full list of events is available in the API reference.

Main events

ready

Triggered once when the panorama image has been loaded and the viewer is ready to perform the first render. Use { once: true } so the listener is automatically removed after firing.
viewer.addEventListener('ready', () => {
  console.log('viewer is ready');
}, { once: true });

click / dblclick

Triggered when the user clicks on the viewer (excluding the navbar and side panel). The event data contains many details about the click location.
viewer.addEventListener('click', ({ data }) => {
  console.log(
    `${data.rightclick ? 'right ' : ''}clicked at yaw: ${data.yaw} pitch: ${data.pitch}`
  );
});
Event data (ClickData):
PropertyTypeDescription
yawnumberHorizontal angle of the clicked point
pitchnumberVertical angle of the clicked point
textureXnumberPixel X on the source panorama image
textureYnumberPixel Y on the source panorama image
rightclickbooleanWhether the right mouse button was used
targetHTMLElementThe actual DOM element that was clicked
A click event is always fired before a dblclick event.

position-updated

Triggered when the view yaw and/or pitch changes.
viewer.addEventListener('position-updated', ({ position }) => {
  console.log(`new position is yaw: ${position.yaw} pitch: ${position.pitch}`);
});

zoom-updated

Triggered when the zoom level changes.
viewer.addEventListener('zoom-updated', ({ zoomLevel }) => {
  console.log(`new zoom level is ${zoomLevel}`);
});

All viewer events

Interaction events

EventType stringDescription
ClickEventclickUser clicked on the viewer
DoubleClickEventdblclickUser double-clicked on the viewer
KeypressEventkey-pressA key was pressed (can be cancelled)
FullscreenEventfullscreenFullscreen was enabled or disabled

Position & zoom events

EventType stringDescription
PositionUpdatedEventposition-updatedCamera yaw or pitch changed
RollUpdatedEventroll-updatedCamera roll changed
ZoomUpdatedEventzoom-updatedZoom level changed
SizeUpdatedEventsize-updatedViewer container was resized

Panorama lifecycle events

EventType stringDescription
ReadyEventreadyViewer ready for first render
PanoramaLoadEventpanorama-loadA new panorama started loading
PanoramaLoadedEventpanorama-loadedPanorama has finished loading
PanoramaErrorEventpanorama-errorError while loading a panorama
TransitionDoneEventtransition-doneTransition completed (or was cancelled)
LoadProgressEventload-progressLoader progress value changed

Animation events

EventType stringDescriptionCancellable
BeforeAnimateEventbefore-animateFired before an animation startsYes
BeforeRotateEventbefore-rotateFired before a rotate operationYes
BeforeRenderEventbefore-renderFired before each render frameNo
RenderEventrenderFired on each renderNo
StopAllEventstop-allAll animations were stoppedNo

Configuration events

EventType stringDescription
ConfigChangedEventconfig-changedOne or more options were updated

UI events

EventType stringDescription
ShowNotificationEventshow-notificationNotification was shown
HideNotificationEventhide-notificationNotification was hidden
ShowOverlayEventshow-overlayOverlay was shown
HideOverlayEventhide-overlayOverlay was hidden
ShowPanelEventshow-panelSide panel was shown
HidePanelEventhide-panelSide panel was hidden
ShowTooltipEventshow-tooltipA tooltip was shown
HideTooltipEventhide-tooltipA tooltip was hidden

Three.js object events

These events require calling viewer.observeObjects(userDataKey) first.
EventType stringDescription
ObjectEnterEvententer-objectCursor entered a scene object
ObjectLeaveEventleave-objectCursor left a scene object
ObjectHoverEventhover-objectCursor moved over a scene object

Cancellable events

Some events can be cancelled by calling event.preventDefault(). This prevents the associated action from occurring:
import { events } from '@photo-sphere-viewer/core';

// Prevent rotation beyond a certain yaw
viewer.addEventListener(events.BeforeRotateEvent.type, (e) => {
  if (e.position.yaw > Math.PI) {
    e.preventDefault();
  }
});

// Modify the target position before the animation begins
viewer.addEventListener(events.BeforeAnimateEvent.type, (e) => {
  if (e.position) {
    e.position.pitch = 0; // force pitch to horizon
  }
});

config-changed helper

ConfigChangedEvent exposes a containsOptions() helper for efficiently checking which options were updated:
import { events } from '@photo-sphere-viewer/core';

viewer.addEventListener(events.ConfigChangedEvent.type, (e) => {
  if (e.containsOptions('fisheye', 'moveSpeed')) {
    // fisheye or moveSpeed was changed
  }
});

Build docs developers (and LLMs) love