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.

87 lines
3.1 KiB

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