What is @antv/matrix-util?
@antv/matrix-util is a utility library for matrix operations, commonly used in graphics and data visualization. It provides functions for matrix transformations, vector operations, and other mathematical computations that are essential for manipulating graphical elements.
What are @antv/matrix-util's main functionalities?
Matrix Multiplication
This feature allows you to multiply two 3x3 matrices. The code sample demonstrates how to use the `mat3.multiply` function to multiply two matrices and log the result.
const { mat3 } = require('@antv/matrix-util');
const matrixA = [1, 0, 0, 0, 1, 0, 0, 0, 1];
const matrixB = [2, 0, 0, 0, 2, 0, 0, 0, 2];
const result = mat3.multiply([], matrixA, matrixB);
console.log(result); // [2, 0, 0, 0, 2, 0, 0, 0, 2]
Vector Transformation
This feature allows you to transform a 2D vector using a 3x3 matrix. The code sample demonstrates how to use the `vec2.transformMat3` function to transform a vector and log the result.
const { vec2 } = require('@antv/matrix-util');
const vector = [1, 2];
const matrix = [1, 0, 0, 0, 1, 0, 2, 3, 1];
const result = vec2.transformMat3([], vector, matrix);
console.log(result); // [3, 5]
Matrix Inversion
This feature allows you to invert a 3x3 matrix. The code sample demonstrates how to use the `mat3.invert` function to invert a matrix and log the result.
const { mat3 } = require('@antv/matrix-util');
const matrix = [1, 2, 3, 0, 1, 4, 5, 6, 0];
const result = mat3.invert([], matrix);
console.log(result); // [ -24, 18, 5, 20, -15, -4, -5, 4, 1 ]
Other packages similar to @antv/matrix-util
gl-matrix
gl-matrix is a high-performance matrix and vector library for WebGL. It provides similar functionalities for matrix and vector operations, but is optimized for use in WebGL applications. Compared to @antv/matrix-util, gl-matrix offers more comprehensive support for 2D and 3D transformations.
mathjs
mathjs is a comprehensive math library for JavaScript and Node.js. It includes a wide range of mathematical functions, including matrix operations. While it is not specifically optimized for graphics, it provides a broader set of mathematical tools compared to @antv/matrix-util.
numeric
numeric is a library for numerical computations in JavaScript. It includes functions for matrix and vector operations, as well as other numerical methods. It is more focused on general numerical computations rather than graphics-specific transformations, making it a versatile alternative to @antv/matrix-util.
matrix-util
扩展了 gl-matrx 的 vec2, vec3, mat3, transform 方法
安装下载
tnpm i --save @antv/util
import { vec2, vec3 } from '@antv/matrix-util';
vec2.angle(v1, v2);
API 文档
推荐使用的 API 文档如下:
- vec2 新增了以下方法:
- angle(v1, v2)
- direction(v1, v2)
- angleTo(v1, v2)
- vertical(out, in) 求垂直向量
- vec3 未新增方法
- mat3 新增:
- translate(out, m, v)
- rotate(out, m, rad)
- scale(out, m, v)
- transform(m, [])
transform(m, [
['t', 100, 200],
['r', 40],
['s', 2, 2]
]);