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.

204 lines
5.5 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. * @param {boolean} selected
  90. * @param {boolean} hover
  91. * @returns {boolean}
  92. * @protected
  93. */
  94. needsRefresh(selected, hover) {
  95. if (this.refreshNeeded === true) {
  96. // This is probably not the best location to reset this member.
  97. // However, in the current logic, it is the most convenient one.
  98. this.refreshNeeded = false;
  99. return true;
  100. }
  101. return (this.width === undefined) || (this.labelModule.differentState(selected, hover));
  102. }
  103. initContextForDraw(ctx, values) {
  104. var borderWidth = values.borderWidth / this.body.view.scale;
  105. ctx.lineWidth = Math.min(this.width, borderWidth);
  106. ctx.strokeStyle = values.borderColor;
  107. ctx.fillStyle = values.color;
  108. }
  109. performStroke(ctx, values) {
  110. var borderWidth = values.borderWidth / this.body.view.scale;
  111. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  112. ctx.save();
  113. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  114. if (borderWidth > 0) {
  115. this.enableBorderDashes(ctx, values);
  116. //draw the border
  117. ctx.stroke();
  118. //disable dashed border for other elements
  119. this.disableBorderDashes(ctx, values);
  120. }
  121. ctx.restore();
  122. }
  123. performFill(ctx, values) {
  124. // draw shadow if enabled
  125. this.enableShadow(ctx, values);
  126. // draw the background
  127. ctx.fill();
  128. // disable shadows for other elements.
  129. this.disableShadow(ctx, values);
  130. this.performStroke(ctx, values);
  131. }
  132. _addBoundingBoxMargin(margin) {
  133. this.boundingBox.left -= margin;
  134. this.boundingBox.top -= margin;
  135. this.boundingBox.bottom += margin;
  136. this.boundingBox.right += margin;
  137. }
  138. /**
  139. * Actual implementation of this method call.
  140. *
  141. * Doing it like this makes it easier to override
  142. * in the child classes.
  143. *
  144. * @param {number} x width
  145. * @param {number} y height
  146. * @param {CanvasRenderingContext2D} ctx
  147. * @param {boolean} selected
  148. * @param {boolean} hover
  149. * @private
  150. */
  151. _updateBoundingBox(x, y, ctx, selected, hover) {
  152. if (ctx !== undefined) {
  153. this.resize(ctx, selected, hover);
  154. }
  155. this.left = x - this.width / 2;
  156. this.top = y - this.height/ 2;
  157. this.boundingBox.left = this.left;
  158. this.boundingBox.top = this.top;
  159. this.boundingBox.bottom = this.top + this.height;
  160. this.boundingBox.right = this.left + this.width;
  161. }
  162. /**
  163. * Default implementation of this method call.
  164. * This acts as a stub which can be overridden.
  165. *
  166. * @param {number} x width
  167. * @param {number} y height
  168. * @param {CanvasRenderingContext2D} ctx
  169. * @param {boolean} selected
  170. * @param {boolean} hover
  171. */
  172. updateBoundingBox(x, y, ctx, selected, hover) {
  173. this._updateBoundingBox(x, y, ctx, selected, hover);
  174. }
  175. }
  176. export default NodeBase;