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.

125 lines
4.0 KiB

  1. /**
  2. * Created by Alex on 3/19/2015.
  3. */
  4. class NodeUtil {
  5. constructor(options, body, labelModule) {
  6. this.body = body;
  7. this.labelModule = labelModule;
  8. this.setOptions(options);
  9. this.top = undefined;
  10. this.left = undefined;
  11. this.height = undefined;
  12. this.height = undefined;
  13. this.boundingBox = {top: 0, left: 0, right: 0, bottom: 0};
  14. }
  15. _drawRawCircle(ctx, x, y, selected, hover, size) {
  16. var borderWidth = this.options.borderWidth;
  17. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  18. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  19. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  20. ctx.lineWidth *= this.networkScaleInv;
  21. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  22. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  23. ctx.circle(x, y, size);
  24. ctx.fill();
  25. ctx.stroke();
  26. }
  27. _drawImageAtPosition(ctx) {
  28. if (this.imageObj.width != 0) {
  29. // draw the image
  30. ctx.globalAlpha = 1.0;
  31. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  32. }
  33. }
  34. _distanceToBorder(angle) {
  35. var borderWidth = 1;
  36. return Math.min(
  37. Math.abs(this.width / 2 / Math.cos(angle)),
  38. Math.abs(this.height / 2 / Math.sin(angle))) + borderWidth;
  39. }
  40. _resizeShape() {
  41. if (this.width === undefined) {
  42. var size = 2 * this.options.size;
  43. this.width = size;
  44. this.height = size;
  45. }
  46. }
  47. _drawShape(ctx, shape, sizeMultiplier, x, y, selected, hover) {
  48. this._resizeShape();
  49. this.left = x - this.width / 2;
  50. this.top = y - this.height / 2;
  51. var borderWidth = this.options.borderWidth;
  52. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  53. // choose draw method depending on the shape
  54. switch (shape) {
  55. case 'dot':
  56. sizeMultiplier = 2;
  57. break;
  58. case 'square':
  59. sizeMultiplier = 2;
  60. break;
  61. case 'triangle':
  62. sizeMultiplier = 3;
  63. break;
  64. case 'triangleDown':
  65. sizeMultiplier = 3;
  66. break;
  67. case 'star':
  68. sizeMultiplier = 4;
  69. break;
  70. }
  71. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  72. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  73. ctx.lineWidth /= this.body.view.scale;
  74. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  75. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  76. ctx[shape](x, y, this.options.size);
  77. ctx.fill();
  78. ctx.stroke();
  79. this.boundingBox.top = y - this.options.size;
  80. this.boundingBox.left = x - this.options.size;
  81. this.boundingBox.right = x + this.options.size;
  82. this.boundingBox.bottom = y + this.options.size;
  83. if (this.options.label!== undefined) {
  84. this.labelModule.draw(ctx, x, y + 0.5* this.height, selected, 'hanging');
  85. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  86. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  87. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  88. }
  89. }
  90. _drawImageLabel(ctx, x, y, selected) {
  91. var yLabel;
  92. var offset = 0;
  93. if (this.height !== undefined) {
  94. offset = this.height * 0.5;
  95. var labelDimensions = this.labelModule.getTextSize(ctx);
  96. if (labelDimensions.lineCount >= 1) {
  97. offset += labelDimensions.height / 2;
  98. offset += 3;
  99. }
  100. }
  101. yLabel = y + offset;
  102. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  103. }
  104. }
  105. export default NodeUtil;