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.

129 lines
5.5 KiB

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