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.

127 lines
3.6 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. /**
  4. * An icon replacement for the default Node shape.
  5. *
  6. * @extends NodeBase
  7. */
  8. class Icon extends NodeBase {
  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 - Unused.
  21. * @param {boolean} [selected]
  22. * @param {boolean} [hover]
  23. */
  24. resize(ctx, selected, hover) {
  25. if (this.needsRefresh(selected, hover)) {
  26. this.iconSize = {
  27. width: Number(this.options.icon.size),
  28. height: Number(this.options.icon.size)
  29. };
  30. this.width = this.iconSize.width + this.margin.right + this.margin.left;
  31. this.height = this.iconSize.height + this.margin.top + this.margin.bottom;
  32. this.radius = 0.5*this.width;
  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.options.icon.size = this.options.icon.size || 50;
  47. this.left = x - this.width / 2;
  48. this.top = y - this.height / 2;
  49. this._icon(ctx, x, y, selected, hover, values);
  50. if (this.options.label !== undefined) {
  51. var iconTextSpacing = 5;
  52. this.labelModule.draw(ctx, this.left + this.iconSize.width / 2 + this.margin.left,
  53. y + this.height / 2 + iconTextSpacing, selected);
  54. }
  55. this.updateBoundingBox(x, y)
  56. }
  57. /**
  58. *
  59. * @param {number} x
  60. * @param {number} y
  61. */
  62. updateBoundingBox(x, y) {
  63. this.boundingBox.top = y - this.options.icon.size * 0.5;
  64. this.boundingBox.left = x - this.options.icon.size * 0.5;
  65. this.boundingBox.right = x + this.options.icon.size * 0.5;
  66. this.boundingBox.bottom = y + this.options.icon.size * 0.5;
  67. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  68. var iconTextSpacing = 5;
  69. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  70. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  71. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height + iconTextSpacing);
  72. }
  73. }
  74. /**
  75. *
  76. * @param {CanvasRenderingContext2D} ctx
  77. * @param {number} x width
  78. * @param {number} y height
  79. * @param {boolean} selected
  80. * @param {boolean} hover - Unused
  81. * @param {ArrowOptions} values
  82. */
  83. _icon(ctx, x, y, selected, hover, values) {
  84. let iconSize = Number(this.options.icon.size);
  85. if (this.options.icon.code !== undefined) {
  86. ctx.font = (selected ? "bold " : "") + iconSize + "px " + this.options.icon.face;
  87. // draw icon
  88. ctx.fillStyle = this.options.icon.color || "black";
  89. ctx.textAlign = "center";
  90. ctx.textBaseline = "middle";
  91. // draw shadow if enabled
  92. this.enableShadow(ctx, values);
  93. ctx.fillText(this.options.icon.code, x, y);
  94. // disable shadows for other elements.
  95. this.disableShadow(ctx, values);
  96. } else {
  97. console.error('When using the icon shape, you need to define the code in the icon options object. This can be done per node or globally.')
  98. }
  99. }
  100. /**
  101. *
  102. * @param {CanvasRenderingContext2D} ctx
  103. * @param {number} angle
  104. * @returns {number}
  105. */
  106. distanceToBorder(ctx, angle) {
  107. return this._distanceToBorder(ctx,angle);
  108. }
  109. }
  110. export default Icon;