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.

91 lines
2.9 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. /**
  4. * An icon replacement for the default Node shape.
  5. *
  6. * @param {Object} options
  7. * @param {Object} body
  8. * @param {Label} labelModule
  9. * @constructor Icon
  10. * @extends NodeBase
  11. */
  12. class Icon extends NodeBase {
  13. constructor(options, body, labelModule) {
  14. super(options, body, labelModule);
  15. this._setMargins(labelModule);
  16. }
  17. resize(ctx, selected, hover) {
  18. if (this.needsRefresh(selected, hover)) {
  19. this.iconSize = {
  20. width: Number(this.options.icon.size),
  21. height: Number(this.options.icon.size)
  22. };
  23. this.width = this.iconSize.width + this.margin.right + this.margin.left;
  24. this.height = this.iconSize.height + this.margin.top + this.margin.bottom;
  25. this.radius = 0.5*this.width;
  26. }
  27. }
  28. draw(ctx, x, y, selected, hover, values) {
  29. this.resize(ctx, selected, hover);
  30. this.options.icon.size = this.options.icon.size || 50;
  31. this.left = x - this.width / 2;
  32. this.top = y - this.height / 2;
  33. this._icon(ctx, x, y, selected, hover, values);
  34. if (this.options.label !== undefined) {
  35. var iconTextSpacing = 5;
  36. this.labelModule.draw(ctx, this.left + this.iconSize.width / 2 + this.margin.left,
  37. y + this.height / 2 + iconTextSpacing, selected);
  38. }
  39. this.updateBoundingBox(x, y)
  40. }
  41. updateBoundingBox(x, y) {
  42. this.boundingBox.top = y - this.options.icon.size * 0.5;
  43. this.boundingBox.left = x - this.options.icon.size * 0.5;
  44. this.boundingBox.right = x + this.options.icon.size * 0.5;
  45. this.boundingBox.bottom = y + this.options.icon.size * 0.5;
  46. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  47. var iconTextSpacing = 5;
  48. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  49. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  50. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height + iconTextSpacing);
  51. }
  52. }
  53. _icon(ctx, x, y, selected, hover, values) {
  54. let iconSize = Number(this.options.icon.size);
  55. if (this.options.icon.code !== undefined) {
  56. ctx.font = (selected ? "bold " : "") + iconSize + "px " + this.options.icon.face;
  57. // draw icon
  58. ctx.fillStyle = this.options.icon.color || "black";
  59. ctx.textAlign = "center";
  60. ctx.textBaseline = "middle";
  61. // draw shadow if enabled
  62. this.enableShadow(ctx, values);
  63. ctx.fillText(this.options.icon.code, x, y);
  64. // disable shadows for other elements.
  65. this.disableShadow(ctx, values);
  66. } else {
  67. 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.')
  68. }
  69. }
  70. distanceToBorder(ctx, angle) {
  71. return this._distanceToBorder(ctx,angle);
  72. }
  73. }
  74. export default Icon;