polygon.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.PolygonCrosshair = void 0;
  4. var tslib_1 = require("tslib");
  5. var util_1 = require("../../util");
  6. var base_1 = require("./base");
  7. var constant_1 = require("./constant");
  8. var PolygonCrosshair = /** @class */ (function (_super) {
  9. tslib_1.__extends(PolygonCrosshair, _super);
  10. function PolygonCrosshair(options) {
  11. return _super.call(this, (0, util_1.deepAssign)({}, PolygonCrosshair.defaultOptions, options)) || this;
  12. }
  13. Object.defineProperty(PolygonCrosshair.prototype, "crosshairPath", {
  14. get: function () {
  15. return this.createPolygonPath();
  16. },
  17. enumerable: false,
  18. configurable: true
  19. });
  20. PolygonCrosshair.prototype.update = function (cfg) {
  21. _super.prototype.update.call(this, cfg);
  22. };
  23. Object.defineProperty(PolygonCrosshair.prototype, "points", {
  24. /**
  25. * 得到从中心出发,各个点方向的单位向量
  26. */
  27. get: function () {
  28. var _a = this.attributes, startAngle = _a.startAngle, sides = _a.sides;
  29. var a = (Math.PI * 2) / sides;
  30. // 单位向量
  31. var unit = [1, 0];
  32. var points = [];
  33. for (var i = 0; i < sides; i += 1) {
  34. points.push((0, util_1.rotate)(unit, [0, 0], (startAngle / 180) * Math.PI + a * i));
  35. }
  36. return points;
  37. },
  38. enumerable: false,
  39. configurable: true
  40. });
  41. /**
  42. * 1. 判断point位于哪一个扇区
  43. * 2. 计算中心到point的线段与所在扇区的边的交点
  44. * 3. 计算等效半径
  45. */
  46. PolygonCrosshair.prototype.setPointer = function (_a) {
  47. var _b = tslib_1.__read(_a, 2), x = _b[0], y = _b[1];
  48. _super.prototype.setPointer.call(this, [x, y]);
  49. var _c = tslib_1.__read(this.localPointer, 2), lx = _c[0], ly = _c[1];
  50. var center = this.attributes.center;
  51. // 求交点
  52. var _d = tslib_1.__read(this.intersection([lx, ly]), 2), ix = _d[0], iy = _d[1];
  53. if (!ix || !iy)
  54. return;
  55. var equivalentRadius = (0, util_1.lineLen)(center, [lx, ly]) / (0, util_1.lineLen)(center, [ix, iy]);
  56. var path = this.createPolygonPath(equivalentRadius);
  57. this.crosshairShape.attr({ path: path });
  58. };
  59. PolygonCrosshair.prototype.adjustLayout = function () {
  60. (0, util_1.hide)(this.tagShape);
  61. };
  62. PolygonCrosshair.prototype.createPolygonPath = function (radius) {
  63. var _a = this.attributes, defaultRadius = _a.defaultRadius, _b = tslib_1.__read(_a.center, 2), cx = _b[0], cy = _b[1];
  64. var path = this.points.map(function (_a, index) {
  65. var _b = tslib_1.__read(_a, 2), x = _b[0], y = _b[1];
  66. var _c = tslib_1.__read((0, util_1.scale)([x, y], radius || defaultRadius), 2), tx = _c[0], ty = _c[1];
  67. return [index === 0 ? 'M' : 'L', cx + tx, cy + ty];
  68. });
  69. path.push(['Z']);
  70. return path;
  71. };
  72. /**
  73. * 求点与扇区单位边的交点
  74. */
  75. PolygonCrosshair.prototype.intersection = function (_a) {
  76. var _b;
  77. var _c = tslib_1.__read(_a, 2), x = _c[0], y = _c[1];
  78. var points = this.points;
  79. var _d = tslib_1.__read(this.attributes.center, 2), cx = _d[0], cy = _d[1];
  80. var ix;
  81. var iy;
  82. // 遍历每个边
  83. for (var i = 1; i <= points.length; i += 1) {
  84. var _e = tslib_1.__read(points[i - 1], 2), sx = _e[0], sy = _e[1];
  85. var _f = tslib_1.__read(points[i % points.length], 2), ex = _f[0], ey = _f[1];
  86. var inter = (0, util_1.intersection)([x, y], [cx, cy], [sx + cx, sy + cy], [ex + cx, ey + cy]);
  87. if (inter.length !== 0) {
  88. // 存在交点
  89. _b = tslib_1.__read(inter, 2), ix = _b[0], iy = _b[1];
  90. }
  91. }
  92. return [ix, iy];
  93. };
  94. PolygonCrosshair.tag = 'polygon-crosshair';
  95. PolygonCrosshair.defaultOptions = {
  96. style: constant_1.POLYGON_CROSSHAIR_DEFAULT_STYLE,
  97. };
  98. return PolygonCrosshair;
  99. }(base_1.CrosshairBase));
  100. exports.PolygonCrosshair = PolygonCrosshair;
  101. //# sourceMappingURL=polygon.js.map