matrix.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { ext, vec2, vec3 } from '@antv/matrix-util';
  2. var identityMatrix = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  3. export function getMatrixByAngle(point, angle, matrix) {
  4. if (matrix === void 0) { matrix = identityMatrix; }
  5. if (!angle) {
  6. // 角度为 0 或者 null 时返回 null
  7. return null;
  8. }
  9. var m = ext.transform(matrix, [
  10. ['t', -point.x, -point.y],
  11. ['r', angle],
  12. ['t', point.x, point.y],
  13. ]);
  14. return m;
  15. }
  16. export function getMatrixByTranslate(point, currentMatrix) {
  17. if (!point.x && !point.y) {
  18. // 0,0 或者 nan 的情况下返回 null
  19. return null;
  20. }
  21. return ext.transform(currentMatrix || identityMatrix, [['t', point.x, point.y]]);
  22. }
  23. // 从矩阵获取旋转的角度
  24. export function getAngleByMatrix(matrix) {
  25. var xVector = [1, 0, 0];
  26. var out = [0, 0, 0];
  27. vec3.transformMat3(out, xVector, matrix);
  28. return Math.atan2(out[1], out[0]);
  29. }
  30. // 矩阵 * 向量
  31. function multiplyVec2(matrix, v) {
  32. var out = [0, 0];
  33. vec2.transformMat3(out, v, matrix);
  34. return out;
  35. }
  36. export function applyMatrix2BBox(matrix, bbox) {
  37. var topLeft = multiplyVec2(matrix, [bbox.minX, bbox.minY]);
  38. var topRight = multiplyVec2(matrix, [bbox.maxX, bbox.minY]);
  39. var bottomLeft = multiplyVec2(matrix, [bbox.minX, bbox.maxY]);
  40. var bottomRight = multiplyVec2(matrix, [bbox.maxX, bbox.maxY]);
  41. var minX = Math.min(topLeft[0], topRight[0], bottomLeft[0], bottomRight[0]);
  42. var maxX = Math.max(topLeft[0], topRight[0], bottomLeft[0], bottomRight[0]);
  43. var minY = Math.min(topLeft[1], topRight[1], bottomLeft[1], bottomRight[1]);
  44. var maxY = Math.max(topLeft[1], topRight[1], bottomLeft[1], bottomRight[1]);
  45. return {
  46. x: minX,
  47. y: minY,
  48. minX: minX,
  49. minY: minY,
  50. maxX: maxX,
  51. maxY: maxY,
  52. width: maxX - minX,
  53. height: maxY - minY,
  54. };
  55. }
  56. export function applyRotate(shape, rotate, x, y) {
  57. if (rotate) {
  58. var matrix = getMatrixByAngle({ x: x, y: y }, rotate, shape.getMatrix());
  59. shape.setMatrix(matrix);
  60. }
  61. }
  62. export function applyTranslate(shape, x, y) {
  63. var translateMatrix = getMatrixByTranslate({ x: x, y: y });
  64. shape.attr('matrix', translateMatrix);
  65. }
  66. //# sourceMappingURL=matrix.js.map