polygon.js 3.9 KB

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