arrow.js 2.3 KB

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