arrow.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. /**
  3. * @fileoverview arrow
  4. * @author dengfuping_develop@163.com
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. var util_1 = require("@antv/util");
  8. var dom_1 = require("../util/dom");
  9. var Arrow = /** @class */ (function () {
  10. function Arrow(attrs, type) {
  11. this.cfg = {};
  12. var el = dom_1.createSVGElement('marker');
  13. var id = util_1.uniqueId('marker_');
  14. el.setAttribute('id', id);
  15. var shape = dom_1.createSVGElement('path');
  16. shape.setAttribute('stroke', attrs.stroke || 'none');
  17. shape.setAttribute('fill', attrs.fill || 'none');
  18. el.appendChild(shape);
  19. el.setAttribute('overflow', 'visible');
  20. el.setAttribute('orient', 'auto-start-reverse');
  21. this.el = el;
  22. this.child = shape;
  23. this.id = id;
  24. var cfg = attrs[type === 'marker-start' ? 'startArrow' : 'endArrow'];
  25. this.stroke = attrs.stroke || '#000';
  26. if (cfg === true) {
  27. this._setDefaultPath(type, shape);
  28. }
  29. else {
  30. this.cfg = cfg; // when arrow config exists
  31. this._setMarker(attrs.lineWidth, shape);
  32. }
  33. return this;
  34. }
  35. Arrow.prototype.match = function () {
  36. return false;
  37. };
  38. Arrow.prototype._setDefaultPath = function (type, el) {
  39. var parent = this.el;
  40. // 默认箭头的边长为 10,夹角为 60 度
  41. el.setAttribute('d', "M0,0 L" + 10 * Math.cos(Math.PI / 6) + ",5 L0,10");
  42. parent.setAttribute('refX', "" + 10 * Math.cos(Math.PI / 6));
  43. parent.setAttribute('refY', "" + 5);
  44. };
  45. Arrow.prototype._setMarker = function (r, el) {
  46. var parent = this.el;
  47. var path = this.cfg.path;
  48. var d = this.cfg.d;
  49. if (util_1.isArray(path)) {
  50. path = path
  51. .map(function (segment) {
  52. return segment.join(' ');
  53. })
  54. .join('');
  55. }
  56. el.setAttribute('d', path);
  57. parent.appendChild(el);
  58. if (d) {
  59. parent.setAttribute('refX', "" + d / r);
  60. }
  61. };
  62. Arrow.prototype.update = function (fill) {
  63. var child = this.child;
  64. if (child.attr) {
  65. child.attr('fill', fill);
  66. }
  67. else {
  68. child.setAttribute('fill', fill);
  69. }
  70. };
  71. return Arrow;
  72. }());
  73. exports.default = Arrow;
  74. //# sourceMappingURL=arrow.js.map