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.

72 lines
2.2 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. let relativeIconSize = iconSize * this.body.view.scale;
  39. if (this.options.icon.code && relativeIconSize > this.options.scaling.label.drawThreshold - 1) {
  40. ctx.font = (selected ? "bold " : "") + iconSize + "px " + this.options.icon.face;
  41. // draw icon
  42. ctx.fillStyle = this.options.icon.color || "black";
  43. ctx.textAlign = "center";
  44. ctx.textBaseline = "middle";
  45. // draw shadow if enabled
  46. this.enableShadow(ctx);
  47. ctx.fillText(this.options.icon.code, x, y);
  48. // disable shadows for other elements.
  49. this.disableShadow(ctx);
  50. }
  51. }
  52. distanceToBorder(ctx, angle) {
  53. this.resize(ctx);
  54. this._distanceToBorder(angle);
  55. }
  56. }
  57. export default Icon;