DOMMatrixReadOnly:transformPoint() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

注意:此功能在 Web Workers 中可用。

DOMMatrixReadOnly 接口的 transformPoint 方法会创建一个新的 DOMPoint 对象,通过矩阵转换指定的点。矩阵和原始点都不会被修改。

您也可以通过 DOMPointReadOnly.matrixTransform() 方法将矩阵应用于点,来创建一个新的 DOMPoint

语法

js
transformPoint()
transformPoint(point)

参数

point

一个 DOMPointDOMPointReadOnly 实例,或一个包含以下最多四个属性的对象

x

空间中点的 x 坐标,数值类型。默认值为 0

y

空间中点的 y 坐标,数值类型。默认值为 0

z

空间中点的 z 坐标,或深度坐标,数值类型。默认值为 0。正值表示更靠近用户,负值表示向屏幕后方退去。

w

点的 w 透视值,数值类型。默认值为 1

返回值

一个 DOMPoint

示例

2D 变换

js
const matrix = new DOMMatrixReadOnly();
const point = new DOMPointReadOnly(10, 20); // DOMPointReadOnly {x: 10, y: 20, z: 0, w: 1}
let newPoint = matrix.transformPoint(point); // DOMPoint {x: 10, y: 20, z: 0, w: 1}

3D 变换

在此示例中,我们将一个 3D 点应用于一个 3D 矩阵

js
// Matrix with translate(22, 37, 10) applied
const matrix3D = new DOMMatrixReadOnly([
  1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 22, 37, 10, 1,
]);
const point3D = new DOMPointReadOnly(5, 10, 3); // DOMPointReadOnly {x: 5, y: 10, z: 3, w: 1}
const transformedPoint3D = point3D.matrixTransform(matrix3D); // DOMPoint {x: 27, y: 47, z: 13, w: 1}

规范

规范
Geometry Interfaces Module Level 1
# dom-dommatrixreadonly-transformpoint

浏览器兼容性

另见