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.

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