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.

86 lines
2.2 KiB

  1. 'use strict';
  2. import CircleImageBase from '../util/CircleImageBase'
  3. /**
  4. * A Circle Node/Cluster shape.
  5. *
  6. * @extends CircleImageBase
  7. */
  8. class Circle extends CircleImageBase {
  9. /**
  10. * @param {Object} options
  11. * @param {Object} body
  12. * @param {Label} labelModule
  13. */
  14. constructor(options, body, labelModule) {
  15. super(options, body, labelModule);
  16. this._setMargins(labelModule);
  17. }
  18. /**
  19. *
  20. * @param {CanvasRenderingContext2D} ctx
  21. * @param {boolean} [selected]
  22. * @param {boolean} [hover]
  23. */
  24. resize(ctx, selected = this.selected, hover = this.hover) {
  25. if (this.needsRefresh(selected, hover)) {
  26. var dimensions = this.getDimensionsFromLabel(ctx, selected, hover);
  27. var diameter = Math.max(dimensions.width + this.margin.right + this.margin.left,
  28. dimensions.height + this.margin.top + this.margin.bottom);
  29. this.options.size = diameter / 2; // NOTE: this size field only set here, not in Ellipse, Database, Box
  30. this.width = diameter;
  31. this.height = diameter;
  32. this.radius = this.width / 2;
  33. }
  34. }
  35. /**
  36. *
  37. * @param {CanvasRenderingContext2D} ctx
  38. * @param {number} x width
  39. * @param {number} y height
  40. * @param {boolean} selected
  41. * @param {boolean} hover
  42. * @param {ArrowOptions} values
  43. */
  44. draw(ctx, x, y, selected, hover, values) {
  45. this.resize(ctx, selected, hover);
  46. this.left = x - this.width / 2;
  47. this.top = y - this.height / 2;
  48. this._drawRawCircle(ctx, x, y, values);
  49. this.updateBoundingBox(x,y);
  50. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  51. y, selected, hover);
  52. }
  53. /**
  54. *
  55. * @param {number} x width
  56. * @param {number} y height
  57. */
  58. updateBoundingBox(x, y) {
  59. this.boundingBox.top = y - this.options.size;
  60. this.boundingBox.left = x - this.options.size;
  61. this.boundingBox.right = x + this.options.size;
  62. this.boundingBox.bottom = y + this.options.size;
  63. }
  64. /**
  65. *
  66. * @param {CanvasRenderingContext2D} ctx
  67. * @param {number} angle - Unused
  68. * @returns {number}
  69. */
  70. distanceToBorder(ctx, angle) { // eslint-disable-line no-unused-vars
  71. this.resize(ctx);
  72. return this.width * 0.5;
  73. }
  74. }
  75. export default Circle;