line.js 2.3 KB

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