DOMMatrixReadOnly: fromFloat32Array() static method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2020.

Note: This feature is available in Web Workers.

Phương thức tĩnh fromFloat32Array() của giao diện DOMMatrixReadOnly tạo một đối tượng DOMMatrixReadOnly mới từ một mảng các giá trị số thực dấu phẩy động độ chính xác đơn (32-bit).

Nếu mảng có 6 giá trị, kết quả là ma trận 2D; nếu mảng có 16 giá trị, kết quả là ma trận 3D. Ngược lại, ngoại lệ TypeError sẽ được ném ra.

Cú pháp

js
DOMMatrixReadOnly.fromFloat32Array(array)

Tham số

array

Một Float32Array với 6 hoặc 16 phần tử theo thứ tự cột chính.

Giá trị trả về

Một đối tượng DOMMatrixReadOnly.

Ngoại lệ

TypeError

Được ném ra nếu độ dài của tham số array không phải là 6 hoặc 16.

Ví dụ

Tạo ma trận 2D từ Float32Array

Ví dụ này tạo một ma trận 2D từ Float32Array có 6 phần tử.

js
const float32Array = new Float32Array([1, 0, 0, 1, 10, 20]);
const matrix2D = DOMMatrixReadOnly.fromFloat32Array(float32Array);

console.log(matrix2D.toString());
// Output: matrix(1, 0, 0, 1, 10, 20)

console.log(matrix2D.is2D);
// Output: true

Tạo ma trận 3D từ Float32Array

Ví dụ này tạo một ma trận 3D từ Float32Array có 16 phần tử.

js
const float32Array = new Float32Array([
  1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 10, 20, 30, 1,
]);
const matrix3D = DOMMatrixReadOnly.fromFloat32Array(float32Array);

console.log(matrix3D.is2D);
// Output: false

console.log(matrix3D.m41, matrix3D.m42, matrix3D.m43);
// Output: 10 20 30

Thông số kỹ thuật

Specification
Geometry Interfaces Module Level 1
# dom-dommatrixreadonly-fromfloat32array

Tương thích trình duyệt

Xem thêm