line.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. /**
  5. * @fileoverview line
  6. * @author dengfuping_develop@163.com
  7. */
  8. var g_math_1 = require("@antv/g-math");
  9. var util_1 = require("@antv/util");
  10. var constant_1 = require("../constant");
  11. var base_1 = require("./base");
  12. var Line = /** @class */ (function (_super) {
  13. tslib_1.__extends(Line, _super);
  14. function Line() {
  15. var _this = _super !== null && _super.apply(this, arguments) || this;
  16. _this.type = 'line';
  17. _this.canFill = false;
  18. _this.canStroke = true;
  19. return _this;
  20. }
  21. Line.prototype.getDefaultAttrs = function () {
  22. var attrs = _super.prototype.getDefaultAttrs.call(this);
  23. return tslib_1.__assign(tslib_1.__assign({}, attrs), { x1: 0, y1: 0, x2: 0, y2: 0, startArrow: false, endArrow: false });
  24. };
  25. Line.prototype.createPath = function (context, targetAttrs) {
  26. var attrs = this.attr();
  27. var el = this.get('el');
  28. util_1.each(targetAttrs || attrs, function (value, attr) {
  29. if (attr === 'startArrow' || attr === 'endArrow') {
  30. if (value) {
  31. var id = util_1.isObject(value)
  32. ? context.addArrow(attrs, constant_1.SVG_ATTR_MAP[attr])
  33. : context.getDefaultArrow(attrs, constant_1.SVG_ATTR_MAP[attr]);
  34. el.setAttribute(constant_1.SVG_ATTR_MAP[attr], "url(#" + id + ")");
  35. }
  36. else {
  37. el.removeAttribute(constant_1.SVG_ATTR_MAP[attr]);
  38. }
  39. }
  40. else if (constant_1.SVG_ATTR_MAP[attr]) {
  41. el.setAttribute(constant_1.SVG_ATTR_MAP[attr], value);
  42. }
  43. });
  44. };
  45. /**
  46. * Use math calculation to get length of line
  47. * @return {number} length
  48. */
  49. Line.prototype.getTotalLength = function () {
  50. var _a = this.attr(), x1 = _a.x1, y1 = _a.y1, x2 = _a.x2, y2 = _a.y2;
  51. return g_math_1.Line.length(x1, y1, x2, y2);
  52. };
  53. /**
  54. * Use math calculation to get point according to ratio as same sa Canvas version
  55. * @param {number} ratio
  56. * @return {Point} point
  57. */
  58. Line.prototype.getPoint = function (ratio) {
  59. var _a = this.attr(), x1 = _a.x1, y1 = _a.y1, x2 = _a.x2, y2 = _a.y2;
  60. return g_math_1.Line.pointAt(x1, y1, x2, y2, ratio);
  61. };
  62. return Line;
  63. }(base_1.default));
  64. exports.default = Line;
  65. //# sourceMappingURL=line.js.map