vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.1 KiB

  1. class NodeBase {
  2. constructor(options, body, labelModule) {
  3. this.body = body;
  4. this.labelModule = labelModule;
  5. this.setOptions(options);
  6. this.top = undefined;
  7. this.left = undefined;
  8. this.height = undefined;
  9. this.width = undefined;
  10. this.radius = undefined;
  11. this.margin = undefined;
  12. this.refreshNeeded = true;
  13. this.boundingBox = {top: 0, left: 0, right: 0, bottom: 0};
  14. }
  15. setOptions(options) {
  16. this.options = options;
  17. }
  18. _setMargins(labelModule) {
  19. this.margin = {};
  20. if (this.options.margin) {
  21. if (typeof this.options.margin == 'object') {
  22. this.margin.top = this.options.margin.top;
  23. this.margin.right = this.options.margin.right;
  24. this.margin.bottom = this.options.margin.bottom;
  25. this.margin.left = this.options.margin.left;
  26. } else {
  27. this.margin.top = this.options.margin;
  28. this.margin.right = this.options.margin;
  29. this.margin.bottom = this.options.margin;
  30. this.margin.left = this.options.margin;
  31. }
  32. }
  33. labelModule.adjustSizes(this.margin)
  34. }
  35. _distanceToBorder(ctx,angle) {
  36. var borderWidth = this.options.borderWidth;
  37. this.resize(ctx);
  38. return Math.min(
  39. Math.abs(this.width / 2 / Math.cos(angle)),
  40. Math.abs(this.height / 2 / Math.sin(angle))) + borderWidth;
  41. }
  42. enableShadow(ctx, values) {
  43. if (values.shadow) {
  44. ctx.shadowColor = values.shadowColor;
  45. ctx.shadowBlur = values.shadowSize;
  46. ctx.shadowOffsetX = values.shadowX;
  47. ctx.shadowOffsetY = values.shadowY;
  48. }
  49. }
  50. disableShadow(ctx, values) {
  51. if (values.shadow) {
  52. ctx.shadowColor = 'rgba(0,0,0,0)';
  53. ctx.shadowBlur = 0;
  54. ctx.shadowOffsetX = 0;
  55. ctx.shadowOffsetY = 0;
  56. }
  57. }
  58. enableBorderDashes(ctx, values) {
  59. if (values.borderDashes !== false) {
  60. if (ctx.setLineDash !== undefined) {
  61. let dashes = values.borderDashes;
  62. if (dashes === true) {
  63. dashes = [5,15]
  64. }
  65. ctx.setLineDash(dashes);
  66. }
  67. else {
  68. console.warn("setLineDash is not supported in this browser. The dashed borders cannot be used.");
  69. this.options.shapeProperties.borderDashes = false;
  70. values.borderDashes = false;
  71. }
  72. }
  73. }
  74. disableBorderDashes(ctx, values) {
  75. if (values.borderDashes !== false) {
  76. if (ctx.setLineDash !== undefined) {
  77. ctx.setLineDash([0]);
  78. }
  79. else {
  80. console.warn("setLineDash is not supported in this browser. The dashed borders cannot be used.");
  81. this.options.shapeProperties.borderDashes = false;
  82. values.borderDashes = false;
  83. }
  84. }
  85. }
  86. /**
  87. * Determine if the shape of a node needs to be recalculated.
  88. *
  89. * @protected
  90. */
  91. needsRefresh(selected, hover) {
  92. if (this.refreshNeeded === true) {
  93. // This is probably not the best location to reset this member.
  94. // However, in the current logic, it is the most convenient one.
  95. this.refreshNeeded = false;
  96. return true;
  97. }
  98. return (this.width === undefined) || (this.labelModule.differentState(selected, hover));
  99. }
  100. initContextForDraw(ctx, values) {
  101. var borderWidth = values.borderWidth / this.body.view.scale;
  102. ctx.lineWidth = Math.min(this.width, borderWidth);
  103. ctx.strokeStyle = values.borderColor;
  104. ctx.fillStyle = values.color;
  105. }
  106. performStroke(ctx, values) {
  107. var borderWidth = values.borderWidth / this.body.view.scale;
  108. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  109. ctx.save();
  110. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  111. if (borderWidth > 0) {
  112. this.enableBorderDashes(ctx, values);
  113. //draw the border
  114. ctx.stroke();
  115. //disable dashed border for other elements
  116. this.disableBorderDashes(ctx, values);
  117. }
  118. ctx.restore();
  119. }
  120. performFill(ctx, values) {
  121. // draw shadow if enabled
  122. this.enableShadow(ctx, values);
  123. // draw the background
  124. ctx.fill();
  125. // disable shadows for other elements.
  126. this.disableShadow(ctx, values);
  127. this.performStroke(ctx, values);
  128. }
  129. _addBoundingBoxMargin(margin) {
  130. this.boundingBox.left -= margin;
  131. this.boundingBox.top -= margin;
  132. this.boundingBox.bottom += margin;
  133. this.boundingBox.right += margin;
  134. }
  135. /**
  136. * Actual implementation of this method call.
  137. *
  138. * Doing it like this makes it easier to override
  139. * in the child classes.
  140. */
  141. _updateBoundingBox(x, y, ctx, selected, hover) {
  142. if (ctx !== undefined) {
  143. this.resize(ctx, selected, hover);
  144. }
  145. this.left = x - this.width / 2;
  146. this.top = y - this.height/ 2;
  147. this.boundingBox.left = this.left;
  148. this.boundingBox.top = this.top;
  149. this.boundingBox.bottom = this.top + this.height;
  150. this.boundingBox.right = this.left + this.width;
  151. }
  152. /**
  153. * Default implementation of this method call.
  154. *
  155. * This acts as a stub which can be overridden.
  156. */
  157. updateBoundingBox(x, y, ctx, selected, hover) {
  158. this._updateBoundingBox(x, y, ctx, selected, hover);
  159. }
  160. }
  161. export default NodeBase;