import {DistanceMeasurementsPlugin} from '@xeokit/xeokit-sdk/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsPlugin.js'
DistanceMeasurementsPlugin
Extends:
Viewer plugin for measuring point-to-point distances.
- [Example 1: Model with distance measurements]
- [Example 2: Create distance measurements with mouse]
- [Example 3: Configuring units and scale
Overview
- A DistanceMeasurement represents a point-to-point measurement between two 3D points on one or two Entitys.
- As shown on the screen capture above, a DistanceMeasurement has one wire (light blue) that shows the direct point-to-point measurement, and three more wires (red, green and blue) that show the distance on each of the World-space X, Y and Z axis.
- Create DistanceMeasurements programmatically with DistanceMeasurementsPlugin#createMeasurement.
- Create DistanceMeasurements interactively using a DistanceMeasurementsControl.
- Existing DistanceMeasurements are registered by ID in DistanceMeasurementsPlugin#measurements.
- Destroy DistanceMeasurements using DistanceMeasurementsPlugin#destroyMeasurement.
- Configure global measurement units and scale via Metrics, located at Scene#metrics.
Example 1: Creating DistanceMeasurements Programmatically
In our first example, we'll use an XKTLoaderPlugin to load a model, and then use a DistanceMeasurementsPlugin to programmatically create two DistanceMeasurements.
Note how each DistanceMeasurement has origin
and target
endpoints, which each indicate a 3D World-space
position on the surface of an Entity. The endpoints can be attached to the same Entity, or to different Entitys.
import {Viewer, XKTLoaderPlugin, DistanceMeasurementsPlugin} from "xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas",
transparent: true
});
viewer.scene.camera.eye = [-2.37, 18.97, -26.12];
viewer.scene.camera.look = [10.97, 5.82, -11.22];
viewer.scene.camera.up = [0.36, 0.83, 0.40];
const xktLoader = new XKTLoaderPlugin(viewer);
const distanceMeasurements = new DistanceMeasurementsPlugin(viewer);
const model = xktLoader.load({
src: "./models/xkt/duplex/duplex.xkt"
});
model.on("loaded", () => {
const myMeasurement1 = distanceMeasurements.createMeasurement({
id: "distanceMeasurement1",
origin: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FLOH"],
worldPos: [0.044, 5.998, 17.767]
},
target: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FLOH"],
worldPos: [4.738, 3.172, 17.768]
},
visible: true,
wireVisible: true
});
const myMeasurement2 = distanceMeasurements.createMeasurement({
id: "distanceMeasurement2",
origin: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FNr2"],
worldPos: [0.457, 2.532, 17.766]
},
target: {
entity: viewer.scene.objects["1CZILmCaHETO8tf3SgGEXu"],
worldPos: [0.436, 0.001, 22.135]
},
visible: true,
wireVisible: true
});
});
Example 2: Creating DistanceMeasurements with Mouse Input
In our second example, we'll use an XKTLoaderPlugin to load a model, then we'll use a DistanceMeasurementsMouseControl to interactively create DistanceMeasurements with mouse input.
After we've activated the DistanceMeasurementsMouseControl, the first click on any Entity begins constructing a DistanceMeasurement, fixing its origin to that Entity. The next click on any Entity will complete the DistanceMeasurement, fixing its target to that second Entity.
The DistanceMeasurementsMouseControl will then wait for the next click on any Entity, to begin constructing another DistanceMeasurement, and so on, until deactivated again.
import {Viewer, XKTLoaderPlugin, DistanceMeasurementsPlugin, DistanceMeasurementsMouseControl, PointerLens} from "xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas",
transparent: true
});
viewer.scene.camera.eye = [-2.37, 18.97, -26.12];
viewer.scene.camera.look = [10.97, 5.82, -11.22];
viewer.scene.camera.up = [0.36, 0.83, 0.40];
const xktLoader = new XKTLoaderPlugin(viewer);
const distanceMeasurements = new DistanceMeasurementsPlugin(viewer);
const distanceMeasurementsControl = new DistanceMeasurementsMouseControl(distanceMeasurements, {
pointerLens : new PointerLens(viewer)
})
distanceMeasurementsControl.snapToVertex = true;
distanceMeasurementsControl.snapToEdge = true;
distanceMeasurementsControl.activate();
const model = xktLoader.load({
src: "./models/xkt/duplex/duplex.xkt"
});
Example 3: Configuring Measurement Units and Scale
In our third example, we'll use the Scene's Metrics to set the global unit of measurement to "meters"
. We'll also specify that a unit within the World-space coordinate system represents ten meters.
The wires belonging to our DistanceMeasurements show their lengths in Real-space coordinates, in the current unit of measurement. They will dynamically update as we set these configurations.
const metrics = viewer.scene.metrics;
metrics.units = "meters";
metrics.scale = 10.0;
Example 4: Attaching Mouse Handlers
In our fourth example, we'll attach event handlers to our plugin, to catch when the user hovers or right-clicks over our measurements.
import {Viewer, XKTLoaderPlugin, DistanceMeasurementsPlugin, DistanceMeasurementsMouseControl, PointerLens} from "xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas",
transparent: true
});
viewer.scene.camera.eye = [-2.37, 18.97, -26.12];
viewer.scene.camera.look = [10.97, 5.82, -11.22];
viewer.scene.camera.up = [0.36, 0.83, 0.40];
const xktLoader = new XKTLoaderPlugin(viewer);
const distanceMeasurements = new DistanceMeasurementsPlugin(viewer);
const distanceMeasurementsControl = new DistanceMeasurementsMouseControl(distanceMeasurements, {
pointerLens : new PointerLens(viewer)
})
distanceMeasurementsControl.snapToVertex = true;
distanceMeasurementsControl.snapToEdge = true;
distanceMeasurementsControl.activate();
distanceMeasurements.on("mouseOver", (e) => {
e.measurement.setHighlighted(true);
});
distanceMeasurements.on("mouseLeave", (e) => {
e.measurement.setHighlighted(false);
});
distanceMeasurements.on("contextMenu", (e) => {
// Show context menu
e.event.preventDefault();
});
const model = xktLoader.load({
src: "Duplex.xkt"
});
model.on("loaded", () => {
const myMeasurement1 = distanceMeasurements.createMeasurement({
id: "distanceMeasurement1",
origin: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FLOH"],
worldPos: [0.044, 5.998, 17.767]
},
target: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FLOH"],
worldPos: [4.738, 3.172, 17.768]
},
visible: true,
wireVisible: true
});
const myMeasurement2 = distanceMeasurements.createMeasurement({
id: "distanceMeasurement2",
origin: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FNr2"],
worldPos: [0.457, 2.532, 17.766]
},
target: {
entity: viewer.scene.objects["1CZILmCaHETO8tf3SgGEXu"],
worldPos: [0.436, 0.001, 22.135]
},
visible: true,
wireVisible: true
});
});
Example 5: Creating DistanceMeasurements with Touch Input
In our fifth example, we'll show how to create distance measurements with touch input, with snapping to the nearest vertex or edge. While creating the measurements, a long-touch when setting the start or end point will cause the point to snap to the nearest vertex or edge. A quick touch-release will immediately set the point at the tapped position on the object surface.
import {Viewer, XKTLoaderPlugin, DistanceMeasurementsPlugin, DistanceMeasurementsTouchControl} from "xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas",
transparent: true
});
viewer.scene.camera.eye = [-2.37, 18.97, -26.12];
viewer.scene.camera.look = [10.97, 5.82, -11.22];
viewer.scene.camera.up = [0.36, 0.83, 0.40];
const xktLoader = new XKTLoaderPlugin(viewer);
const distanceMeasurements = new DistanceMeasurementsPlugin(viewer);
const model = xktLoader.load({
src: "./models/xkt/duplex/duplex.xkt"
});
const distanceMeasurements = new DistanceMeasurementsPlugin(viewer);
const distanceMeasurementsTouchControl = new DistanceMeasurementsTouchControl(distanceMeasurements, {
pointerLens : new PointerLens(viewer),
snapToVertex: true,
snapToEdge: true
})
distanceMeasurementsTouchControl.activate();
Constructor Summary
Public Constructor | ||
public |
constructor(viewer: Viewer, cfg: Object) |
Member Summary
Public Members | ||
public get |
this get was deprecated.
Gets the default DistanceMeasurementsControl. |
|
public |
|
|
public |
defaultColor: * |
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public set |
Sets the minimum length, in pixels, of an axis wire beyond which its label is shown. |
|
public get |
Gets the minimum length, in pixels, of an axis wire beyond which its label is shown. |
|
public get |
measurements: {String: DistanceMeasurement} Gets the existing DistanceMeasurements, each mapped to its DistanceMeasurement#id. |
|
public get |
Gets the PointerLens attached to this DistanceMeasurementsPlugin. |
|
public set |
Sets whether the measurement added is rotation adjusted or not. |
|
public get |
Gets whether the measurement added is rotation adjusted or not. |
|
public |
zIndex: * |
Method Summary
Public Methods | ||
public |
clear() Destroys all DistanceMeasurements. |
|
public |
createMeasurement(params: Object): DistanceMeasurement Creates a DistanceMeasurement. |
|
public |
destroy() Destroys this DistanceMeasurementsPlugin. |
|
public |
destroyMeasurement(id: String) Destroys a DistanceMeasurement. |
|
public |
Gets if the axis wires of each DistanceMeasurement are visible. |
|
public |
getContainerElement(): * | HTMLElement | HTMLElement Gets the plugin's HTML container element, if any. |
|
public |
setAxisVisible(labelsShown: Boolean) Shows all or hides the axis wires of each DistanceMeasurement. |
|
public |
setLabelsShown(labelsShown: Boolean) Shows all or hides the distance label of each DistanceMeasurement. |
Inherited Summary
From class Plugin | ||
public |
ID for this Plugin, unique within its Viewer. |
|
public |
The Viewer that contains this Plugin. |
|
public |
destroy() Destroys this Plugin and removes it from its Viewer. |
|
public |
Logs an error message to the JavaScript developer console, prefixed with the ID of this Plugin. |
|
public |
Fires an event on this Plugin. |
|
public |
Returns true if there are any subscribers to the given event on this Plugin. |
|
public |
Logs a message to the JavaScript developer console, prefixed with the ID of this Plugin. |
|
public |
Cancels an event subscription that was previously made with Plugin#on or Plugin#once. |
|
public |
Subscribes to an event on this Plugin. |
|
public |
Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is subIdd. |
|
public |
scheduleTask(task: *) Schedule a task to perform on the next browser interval |
|
public |
Logs a warning message to the JavaScript developer console, prefixed with the ID of this Plugin. |
Public Constructors
public constructor(viewer: Viewer, cfg: Object) source
Creates this Plugin and installs it into the given Viewer.
Override:
Plugin#constructorParams:
Name | Type | Attribute | Description |
viewer | Viewer | The Viewer. |
|
cfg | Object |
|
Plugin configuration. |
cfg.id | String |
|
Optional ID for this plugin, so that we can find it within Viewer#plugins. |
cfg.labelMinAxisLength | Number |
|
The minimum length, in pixels, of an axis wire beyond which its label is shown. |
cfg.container | HTMLElement |
|
Container DOM element for markers and labels. Defaults to |
cfg.defaultVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultOriginVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultTargetVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultWireVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultLabelsVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultXLabelEnabled | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultYLabelEnabled | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultZLabelEnabled | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultLengthLabelEnabled | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultAxisVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultXAxisVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultYAxisVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultZAxisVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.useRotationAdjustment | boolean |
|
The default value of DistanceMeasurements |
cfg.defaultColor | string |
|
The default color of the length dots, wire and label. |
cfg.zIndex | number |
|
If set, the wires, dots and labels will have this zIndex (+1 for dots and +2 for labels). |
cfg.defaultLabelsOnWires | boolean |
|
The default value of the DistanceMeasurements |
cfg.pointerLens | PointerCircle |
|
A PointerLens to help the user position the pointer. This can be shared with other plugins. |
Public Members
public get control: DistanceMeasurementsControl source
Gets the default DistanceMeasurementsControl.
public defaultAxisVisible: * source
public defaultColor: * source
public defaultLabelsOnWires: * source
public defaultLabelsVisible: * source
public defaultLengthLabelEnabled: * source
public defaultOriginVisible: * source
public defaultTargetVisible: * source
public defaultVisible: * source
public defaultWireVisible: * source
public defaultXAxisVisible: * source
public defaultXLabelEnabled: * source
public defaultYAxisVisible: * source
public defaultYLabelEnabled: * source
public defaultZAxisVisible: * source
public defaultZLabelEnabled: * source
public set labelMinAxisLength: number source
Sets the minimum length, in pixels, of an axis wire beyond which its label is shown.
The axis wire's label is not shown when its length is less than this value.
This is 25
pixels by default.
Must not be less than 1
.
public get labelMinAxisLength: number: * source
Gets the minimum length, in pixels, of an axis wire beyond which its label is shown.
public get measurements: {String: DistanceMeasurement} source
Gets the existing DistanceMeasurements, each mapped to its DistanceMeasurement#id.
public get pointerLens: PointerCircle: * source
Gets the PointerLens attached to this DistanceMeasurementsPlugin.
public set useRotationAdjustment: boolean source
Sets whether the measurement added is rotation adjusted or not.
public get useRotationAdjustment: number: * source
Gets whether the measurement added is rotation adjusted or not.
public zIndex: * source
Public Methods
public createMeasurement(params: Object): DistanceMeasurement source
Creates a DistanceMeasurement.
The DistanceMeasurement is then registered by DistanceMeasurement#id in DistanceMeasurementsPlugin#measurements.
Params:
Name | Type | Attribute | Description |
params | Object | DistanceMeasurement configuration. |
|
params.id | String | Unique ID to assign to DistanceMeasurement#id. The DistanceMeasurement will be registered by this in DistanceMeasurementsPlugin#measurements and Scene.components. Must be unique among all components in the Viewer. |
|
params.origin.worldPos | Number[] | Origin World-space 3D position. |
|
params.origin.entity | Entity | Origin Entity. |
|
params.target.worldPos | Number[] | Target World-space 3D position. |
|
params.target.entity | Entity | Target Entity. |
|
params.visible | Boolean |
|
Whether to initially show the DistanceMeasurement. |
params.originVisible | Boolean |
|
Whether to initially show the DistanceMeasurement origin. |
params.targetVisible | Boolean |
|
Whether to initially show the DistanceMeasurement target. |
params.wireVisible | Boolean |
|
Whether to initially show the direct point-to-point wire between DistanceMeasurement#origin and DistanceMeasurement#target. |
params.axisVisible | Boolean |
|
Whether to initially show the axis-aligned wires between DistanceMeasurement#origin and DistanceMeasurement#target. |
params.xAxisVisible | Boolean |
|
Whether to initially show the X-axis-aligned wires between DistanceMeasurement#origin and DistanceMeasurement#target. |
params.yAxisVisible | Boolean |
|
Whether to initially show the Y-axis-aligned wires between DistanceMeasurement#origin and DistanceMeasurement#target. |
params.zAxisVisible | Boolean |
|
Whether to initially show the Z-axis-aligned wires between DistanceMeasurement#origin and DistanceMeasurement#target. |
params.xLabelEnabled | Boolean |
|
Whether to initially show the x label. |
params.yLabelEnabled | Boolean |
|
Whether to initially show the y label. |
params.zLabelEnabled | Boolean |
|
Whether to initially show the z label. |
params.lengthLabelEnabled | Boolean |
|
Whether to initially show the length label. |
params.labelsVisible | Boolean |
|
Whether to initially show the labels. |
params.lengthLabelVisible | Boolean |
|
Whether to initially show the labels. |
params.color | string |
|
The color of the length dot, wire and label. |
params.labelsOnWires | Boolean |
|
Determines if labels will be set on wires or one below the other. |
public destroy() source
Destroys this DistanceMeasurementsPlugin.
Destroys all DistanceMeasurements first.
Override:
Plugin#destroypublic destroyMeasurement(id: String) source
Destroys a DistanceMeasurement.
Params:
Name | Type | Attribute | Description |
id | String | ID of DistanceMeasurement to destroy. |
public getAxisVisible(): Boolean source
Gets if the axis wires of each DistanceMeasurement are visible.
public getContainerElement(): * | HTMLElement | HTMLElement source
Gets the plugin's HTML container element, if any.
Return:
* | HTMLElement | HTMLElement |
public setAxisVisible(labelsShown: Boolean) source
Shows all or hides the axis wires of each DistanceMeasurement.
Params:
Name | Type | Attribute | Description |
labelsShown | Boolean | Whether or not to show the axis wires. |
public setLabelsShown(labelsShown: Boolean) source
Shows all or hides the distance label of each DistanceMeasurement.
Params:
Name | Type | Attribute | Description |
labelsShown | Boolean | Whether or not to show the labels. |