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.

90 lines
3.6 KiB

  1. import NodeBase from '../util/NodeBase'
  2. /**
  3. * Base class for constructing Node/Cluster Shapes.
  4. *
  5. * @class ShapeBase
  6. * @extends NodeBase
  7. */
  8. class ShapeBase extends NodeBase {
  9. /**
  10. * @param {Object} options
  11. * @param {Object} body
  12. * @param {Label} labelModule
  13. * @constructor ShapeBase
  14. */
  15. constructor(options, body, labelModule) {
  16. super(options, body, labelModule)
  17. }
  18. /**
  19. *
  20. * @param {CanvasRenderingContext2D} ctx
  21. * @param {Boolean} [selected]
  22. * @param {Boolean} [hover]
  23. * @param {Object} [values={size: this.options.size}]
  24. */
  25. resize(ctx, selected = this.selected, hover = this.hover, values = { size: this.options.size }) {
  26. if (this.needsRefresh(selected, hover)) {
  27. this.labelModule.getTextSize(ctx, selected, hover);
  28. var size = 2 * values.size;
  29. this.width = size;
  30. this.height = size;
  31. this.radius = 0.5*this.width;
  32. }
  33. }
  34. /**
  35. *
  36. * @param {CanvasRenderingContext2D} ctx
  37. * @param {String} shape
  38. * @param {Number} sizeMultiplier - Unused! TODO: Remove next major release
  39. * @param {Number} x
  40. * @param {Number} y
  41. * @param {Boolean} selected
  42. * @param {Boolean} hover
  43. * @param {{toArrow: boolean, toArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), toArrowType: *, middleArrow: boolean, middleArrowScale: (number|allOptions.edges.arrows.middle.scaleFactor|{number}|Array), middleArrowType: (allOptions.edges.arrows.middle.type|{string}|string|*), fromArrow: boolean, fromArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), fromArrowType: *, arrowStrikethrough: (*|boolean|allOptions.edges.arrowStrikethrough|{boolean}), color: undefined, inheritsColor: (string|string|string|allOptions.edges.color.inherit|{string, boolean}|Array|*), opacity: *, hidden: *, length: *, shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *, dashes: (*|boolean|Array|allOptions.edges.dashes|{boolean, array}), width: *}} values
  44. * @private
  45. */
  46. _drawShape(ctx, shape, sizeMultiplier, x, y, selected, hover, values) {
  47. this.resize(ctx, selected, hover, values);
  48. this.left = x - this.width / 2;
  49. this.top = y - this.height / 2;
  50. this.initContextForDraw(ctx, values);
  51. ctx[shape](x, y, values.size);
  52. this.performFill(ctx, values);
  53. if (this.options.label !== undefined) {
  54. // Need to call following here in order to ensure value for `this.labelModule.size.height`
  55. this.labelModule.calculateLabelSize(ctx, selected, hover, x, y, 'hanging')
  56. let yLabel = y + 0.5 * this.height + 0.5 * this.labelModule.size.height;
  57. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  58. }
  59. this.updateBoundingBox(x,y);
  60. }
  61. /**
  62. *
  63. * @param {Number} x
  64. * @param {Number} y
  65. */
  66. updateBoundingBox(x, y) {
  67. this.boundingBox.top = y - this.options.size;
  68. this.boundingBox.left = x - this.options.size;
  69. this.boundingBox.right = x + this.options.size;
  70. this.boundingBox.bottom = y + this.options.size;
  71. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  72. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  73. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  74. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  75. }
  76. }
  77. }
  78. export default ShapeBase;