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 = '