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.

83 lines
2.6 KiB

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