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.

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