import { PerspectiveCamera, Vector2, Vector3 } from 'three'; import { Cognite3DViewerToolBase } from '../Cognite3DViewerToolBase'; import { Cognite3DViewer } from '../../../api'; import { DataSourceType } from '../../../data-providers'; /** * Callback that is triggered whenever the 2D position of an overlay is updated * in {@link HtmlOverlayTool}. */ export type HtmlOverlayPositionUpdatedDelegate = (element: HTMLElement, position2D: Vector2, position3D: Vector3, distanceToCamera: number, userData: any) => void; /** * Callback that is triggered when a set of overlays are clustered together in * {@link HtmlOverlayTool}. */ export type HtmlOverlayCreateClusterDelegate = (overlayElements: { htmlElement: HTMLElement; userData: any; }[]) => HTMLElement; /** * Options for an overlay added using {@link HtmlOverlayTool.add}. */ export type HtmlOverlayOptions = { /** * Callback that is triggered whenever the position of the overlay is updated. Optional. */ positionUpdatedCallback?: HtmlOverlayPositionUpdatedDelegate; /** * Optional user specified data that is provided to the {@link HtmlOverlayCreateClusterDelegate} and * {@link HtmlOverlayPositionUpdatedDelegate}. */ userData?: any; }; /** * Controls how close overlay elements are clustered together. */ export type HtmlOverlayToolClusteringOptions = { /** * Currently only 'overlapInScreenSpace' is supported. In this mode, * overlays are clustered together into a single element as defined by * the {@link HtmlOverlayToolClusteringOptions.createClusterElementCallback} and hidden when they overlap * in screen space. The composite element is placed at the midpoint of * all clustered elements. * * Clustered elements are faded in/out using CSS styling `transition`, * `opacity` and `visibility`. */ mode: 'overlapInScreenSpace'; /** * Callback that is triggered when a set of overlays are clustered together * to create a "composite" element as a placeholder for the clustered elements. * Composite elements are cached and reused across frames, so this callback is * only invoked when the cluster composition changes (i.e. when the set of * clustered overlays differs from any previously seen cluster). * * Note: because the callback is not re-invoked while a cluster persists, * the produced composite element will not reflect later mutations to the * `userData` of any clustered overlay. Treat `userData` as immutable for * overlays that may be clustered, or rebuild the affected overlays * (remove + add) to force a fresh composite to be created. * * The composite's screen position is updated each frame by the tool, but * the callback itself should not depend on viewport state (camera position, * cluster midpoint, screen coordinates, etc.) since the returned element * is reused across frames as the camera moves. */ createClusterElementCallback: HtmlOverlayCreateClusterDelegate; }; /** * Options for the visualization of overlays */ export type HtmlOverlayToolOptions = { /** * Options for clustering the HTML overlays */ clusteringOptions?: HtmlOverlayToolClusteringOptions; }; /** * Manages HTMLoverlays for {@link Cognite3DViewer}. Attaches HTML elements to a * 3D position and updates its position/visibility as user moves the camera. This is * useful to create HTML overlays to highlight information about key positions in the 3D model. * * Attached elements *must* have CSS style 'position: absolute'. It's also recommended * in most cases to have styles 'pointer-events: none' and 'touch-action: none' to avoid * interfering with 3D navigation. Consider also applying 'transform: translate(-50%, -50%)' * to anchor the center of the element rather than the top-left corner. In some cases the * `zIndex`-attribute is necessary for the element to appear on top of the viewer. * * @example * ```js * const el = document.createElement('div'); * el.style.position = 'absolute'; // Required! * // Anchor to center of element * el.style.transform = 'translate(-50%, -50%)'; * // Avoid being target for events * el.style.pointerEvents = 'none; * el.style.touchAction = 'none'; * // Render in front of other elements * el.style.zIndex = 10; * * el.style.color = 'red'; * el.innerHtml = '

Overlay

'; * * const overlayTool = new HtmlOverlayTool(viewer); * overlayTool.add(el, new Vector3(10, 10, 10)); * // ... * overlayTool.remove(el); * // or, to remove all attached elements * overlayTool.clear(); * * // detach the tool from the viewer * overlayTool.dispose(); * ``` */ export declare class HtmlOverlayTool extends Cognite3DViewerToolBase { private readonly _viewer; private readonly _options; private readonly _htmlOverlays; private readonly _compositeOverlays; private _visible; private readonly TIMER_ADVANCE_MS; private readonly _elementIds; private _nextElementId; private readonly _clusterCache; private readonly _aliveClusterKeys; private readonly _onSceneRenderedHandler; private readonly _onViewerDisposedHandler; private readonly _preallocatedVariables; private readonly scheduleUpdate; private get viewerDomElement(); private get viewerCamera(); private get viewerCanvas(); constructor(viewer: Cognite3DViewer, options?: HtmlOverlayToolOptions); /** * Returns all added HTML elements along with their 3D positions. */ get elements(): { element: HTMLElement; position3D: Vector3; }[]; /** * Removes all elements and detaches from the viewer. * @override */ dispose(): void; /** * Registers a HTML overlay that will be updated on rendering. * * @param htmlElement * @param position3D * @param options */ add(htmlElement: HTMLElement, position3D: Vector3, options?: HtmlOverlayOptions): void; /** * Removes a overlay and removes it from the DOM. * @param htmlElement */ remove(htmlElement: HTMLElement): void; /** * Removes all attached HTML overlay elements. */ clear(): void; /** * Hide/unhide all HTML overlay elements. * @param enable */ visible(enable: boolean): void; /** * Updates positions of all overlays. This is automatically managed and there * shouldn't be any reason to trigger this unless the attached elements are * modified externally. * * Calling this function often might cause degraded performance. * @param customCamera Optional camera to be used in place of viewerCamera when calculating positions */ forceUpdate(customCamera?: PerspectiveCamera): void; /** * Update size of new elements. This is only done once as this causes * layout to be invalidated which is an expensive operation. */ private updateNewElementSizes; private commitDOMChanges; private clusterElements; private cleanupClusterElements; /** * Invalidate any cached cluster composites that referenced the given overlay * element. Used when an overlay is removed so its cluster composite is * detached from the DOM immediately rather than lingering until the next * frame's stale-cluster sweep. */ private invalidateClustersForElement; /** * Remove cluster cache entries that were not reused during the current frame * and detach their composite elements from the DOM. */ private removeStaleClusters; private clusterByOverlapInScreenSpace; private createClusterKey; private addComposite; private onSceneRendered; private onViewerDisposed; }