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
3.1 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. this.textSize = this.labelModule.getTextSize(ctx, selected, hover);
  27. var diameter = Math.max(this.textSize.width + this.margin.right + this.margin.left,
  28. this.textSize.height + this.margin.top + this.margin.bottom);
  29. this.options.size = diameter / 2;
  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 {{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
  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;