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.

82 lines
2.4 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. if (this.options.label !== undefined) {
  25. var iconTextSpacing = 5;
  26. this.labelModule.draw(ctx, x, y + this.height * 0.5 + iconTextSpacing, selected);
  27. }
  28. this.updateBoundingBox(x,y)
  29. }
  30. updateBoundingBox(x,y) {
  31. this.boundingBox.top = y - this.options.icon.size * 0.5;
  32. this.boundingBox.left = x - this.options.icon.size * 0.5;
  33. this.boundingBox.right = x + this.options.icon.size * 0.5;
  34. this.boundingBox.bottom = y + this.options.icon.size * 0.5;
  35. if (this.options.label !== undefined) {
  36. var iconTextSpacing = 5;
  37. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  38. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  39. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height + iconTextSpacing);
  40. }
  41. }
  42. _icon(ctx, x, y, selected) {
  43. let iconSize = Number(this.options.icon.size);
  44. if (this.options.icon.code !== undefined) {
  45. ctx.font = (selected ? "bold " : "") + iconSize + "px " + this.options.icon.face;
  46. // draw icon
  47. ctx.fillStyle = this.options.icon.color || "black";
  48. ctx.textAlign = "center";
  49. ctx.textBaseline = "middle";
  50. // draw shadow if enabled
  51. this.enableShadow(ctx);
  52. ctx.fillText(this.options.icon.code, x, y);
  53. // disable shadows for other elements.
  54. this.disableShadow(ctx);
  55. }
  56. else {
  57. 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.')
  58. }
  59. }
  60. distanceToBorder(ctx, angle) {
  61. this.resize(ctx);
  62. this._distanceToBorder(angle);
  63. }
  64. }
  65. export default Icon;