matrix.js 2.7 KB

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