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.

97 lines
3.0 KiB

  1. import NodeBase from '../util/NodeBase'
  2. /**
  3. * Base class for constructing Node/Cluster Shapes.
  4. *
  5. * @extends NodeBase
  6. */
  7. class ShapeBase extends NodeBase {
  8. /**
  9. * @param {Object} options
  10. * @param {Object} body
  11. * @param {Label} labelModule
  12. */
  13. constructor(options, body, labelModule) {
  14. super(options, body, labelModule)
  15. }
  16. /**
  17. *
  18. * @param {CanvasRenderingContext2D} ctx
  19. * @param {boolean} [selected]
  20. * @param {boolean} [hover]
  21. * @param {Object} [values={size: this.options.size}]
  22. */
  23. resize(ctx, selected = this.selected, hover = this.hover, values = { size: this.options.size }) {
  24. if (this.needsRefresh(selected, hover)) {
  25. this.labelModule.getTextSize(ctx, selected, hover);
  26. var size = 2 * values.size;
  27. this.width = size;
  28. this.height = size;
  29. this.radius = 0.5*this.width;
  30. }
  31. }
  32. /**
  33. *
  34. * @param {CanvasRenderingContext2D} ctx
  35. * @param {string} shape
  36. * @param {number} sizeMultiplier - Unused! TODO: Remove next major release
  37. * @param {number} x
  38. * @param {number} y
  39. * @param {boolean} selected
  40. * @param {boolean} hover
  41. * @param {ArrowOptions} values
  42. * @private
  43. */
  44. _drawShape(ctx, shape, sizeMultiplier, x, y, selected, hover, values) {
  45. this.resize(ctx, selected, hover, values);
  46. this.left = x - this.width / 2;
  47. this.top = y - this.height / 2;
  48. this.initContextForDraw(ctx, values);
  49. ctx[shape](x, y, values.size);
  50. this.performFill(ctx, values);
  51. if (this.options.icon !== undefined) {
  52. if (this.options.icon.code !== undefined) {
  53. ctx.font = (selected ? "bold " : "")
  54. + (this.height / 2) + "px "
  55. + (this.options.icon.face || 'FontAwesome');
  56. ctx.fillStyle = this.options.icon.color || "black";
  57. ctx.textAlign = "center";
  58. ctx.textBaseline = "middle";
  59. ctx.fillText(this.options.icon.code, x, y);
  60. }
  61. }
  62. if (this.options.label !== undefined) {
  63. // Need to call following here in order to ensure value for `this.labelModule.size.height`
  64. this.labelModule.calculateLabelSize(ctx, selected, hover, x, y, 'hanging')
  65. let yLabel = y + 0.5 * this.height + 0.5 * this.labelModule.size.height;
  66. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  67. }
  68. this.updateBoundingBox(x,y);
  69. }
  70. /**
  71. *
  72. * @param {number} x
  73. * @param {number} y
  74. */
  75. updateBoundingBox(x, y) {
  76. this.boundingBox.top = y - this.options.size;
  77. this.boundingBox.left = x - this.options.size;
  78. this.boundingBox.right = x + this.options.size;
  79. this.boundingBox.bottom = y + this.options.size;
  80. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  81. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  82. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  83. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  84. }
  85. }
  86. }
  87. export default ShapeBase;