此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

DOMPoint

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2020年1月⁩.

备注: 此特性在 Web Worker 中可用。

DOMPoint 对象表示坐标系统中的一个二维或三维点;它包含最多三个维度的坐标值以及一个可选的透视值。DOMPoint 基于 DOMPointReadOnly,但允许修改其属性的值。

一般而言,x 的正值表示位于原点右侧的位置,y 的正值表示位于原点下方,而 z 的正值表示从屏幕向外(即朝向用户)的方向。

DOMPointReadOnly DOMPoint

构造函数

DOMPoint()

根据零个或多个坐标分量值(以及可选的透视值 w)创建并返回一个新的 DOMPoint 对象。你也可以通过调用 DOMPoint.fromPoint() 静态方法,使用现有的 DOMPointDOMPointReadOnly 或具有相同属性的对象来创建一个新的点。

实例属性

DOMPoint 还继承父接口 DOMPointReadOnly 的属性。

DOMPoint.x

DOMPointx 坐标。

DOMPoint.y

DOMPointy 坐标。

DOMPoint.z

DOMPointz 坐标。

DOMPoint.w

DOMPoint 的透视值。

实例方法

DOMPoint 继承父接口 DOMPointReadOnly 的实例方法。

静态方法

DOMPoint 还可能继承父接口 DOMPointReadOnly 的静态方法。

DOMPoint.fromPoint()

根据一个已有的点(或包含相应属性的对象)创建一个新的可变 DOMPoint 对象,并使用该点的属性值进行初始化。

示例

WebXR 设备 API 中,DOMPointReadOnly 值用于表示位置和方向。在以下代码片段中,可以在 XRSession 的动画帧回调中,通过调用 XRFrame.getViewerPose() 来获取 XR 设备(例如 VR 头显或具备 AR 功能的手机)的姿态。返回的 XRPose 对象的 transform 属性包含两个 DOMPointReadOnly 类型的属性:position(表示位置向量)和 orientation(表示四元数方向)。

js
function onXRFrame(time, xrFrame) {
  let viewerPose = xrFrame.getViewerPose(xrReferenceSpace);

  if (viewerPose) {
    let position = viewerPose.transform.position;
    let orientation = viewerPose.transform.orientation;

    console.log(
      `XR 视图位置:{x: ${roundToTwo(position.x)}, y: ${roundToTwo(
        position.y,
      )}, z: ${roundToTwo(position.z)}}`,
    );

    console.log(
      `XR 视图方向:{x: ${roundToTwo(orientation.x)}, y: ${roundToTwo(
        orientation.y,
      )}, z: ${roundToTwo(orientation.z)}, w: ${roundToTwo(orientation.w)}}`,
    );
  }
}

规范

Specification
Geometry Interfaces Module Level 1
# DOMPoint

浏览器兼容性

参见