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.

85 lines
2.6 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.label !== undefined) {
  52. // Need to call following here in order to ensure value for `this.labelModule.size.height`
  53. this.labelModule.calculateLabelSize(ctx, selected, hover, x, y, 'hanging')
  54. let yLabel = y + 0.5 * this.height + 0.5 * this.labelModule.size.height;
  55. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  56. }
  57. this.updateBoundingBox(x,y);
  58. }
  59. /**
  60. *
  61. * @param {number} x
  62. * @param {number} y
  63. */
  64. updateBoundingBox(x, y) {
  65. this.boundingBox.top = y - this.options.size;
  66. this.boundingBox.left = x - this.options.size;
  67. this.boundingBox.right = x + this.options.size;
  68. this.boundingBox.bottom = y + this.options.size;
  69. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  70. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  71. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  72. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  73. }
  74. }
  75. }
  76. export default ShapeBase;