UNPKG

fabric-texture

Version:

This JavaScript library enables fast and efficient image distortion transformations using Canvas 2D.

69 lines (47 loc) 1.57 kB
[**fabric-texture**](../../../../README.md) *** # Function: calcMatrix() > **calcMatrix**(`newCoords`, `oldCoords`): `number`[] 计算二维仿射变换矩阵 基于三对对应点计算 2D 仿射变换矩阵的六个参数 [a, b, c, d, e, f] 变换矩阵用于将一组坐标映射到另一组坐标,满足以下方程: X = ax + cy + e Y = bx + dy + f ## Parameters ### newCoords \[`Coord`, `Coord`, `Coord`\] 变换后的三个点坐标 每个点包含 {x: number, y: number} 三点不能共线,否则无法计算变换矩阵 ### oldCoords \[`Coord`, `Coord`, `Coord`\] 变换前的三个点坐标 每个点包含 {x: number, y: number} 三点不能共线,否则无法计算变换矩阵 ## Returns `number`[] 变换矩阵的六个参数 [a, b, c, d, e, f] - a, b: x 方向的缩放和旋转 - c, d: y 方向的缩放和旋转 - e, f: x, y 方向的平移 ## Example ```typescript // 计算旋转45度的变换矩阵 const before = [ { x: 0, y: 0 }, // 原点 { x: 1, y: 0 }, // x轴上一点 { x: 0, y: 1 } // y轴上一点 ]; const after = [ { x: 0, y: 0 }, // 原点不变 { x: 0.707, y: 0.707 }, // 旋转后的x轴点 { x: -0.707, y: 0.707 } // 旋转后的y轴点 ]; const matrix = calcMatrix(after, before); // 返回近似值:[0.707, 0.707, -0.707, 0.707, 0, 0] ``` ## Remarks - 使用 safeFactor 处理零值和接近零值的情况 - 输入点必须按相同顺序对应 - 返回的矩阵参数可能存在微小的精度误差 - 三点共线会导致计算失败或结果不准确