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. /**
  2. * Created by Alex on 3/18/2015.
  3. */
  4. 'use strict';
  5. import NodeUtil from './nodeUtil'
  6. class Icon extends NodeUtil {
  7. constructor(options, body, labelModule) {
  8. super(options, body, labelModule);
  9. }
  10. setOptions(options) {
  11. this.options = options;
  12. }
  13. resize(ctx) {
  14. if (this.width === undefined) {
  15. var margin = 5;
  16. var iconSize = {
  17. width: Number(this.options.icon.size),
  18. height: Number(this.options.icon.size)
  19. };
  20. this.width = iconSize.width + 2 * margin;
  21. this.height = iconSize.height + 2 * margin;
  22. }
  23. }
  24. draw(ctx, x, y, selected, hover) {
  25. this.resize(ctx);
  26. this.options.icon.size = this.options.icon.size || 50;
  27. this.left = x - this.width * 0.5;
  28. this.top = y - this.height * 0.5;
  29. this._icon(ctx, x, y, selected);
  30. this.boundingBox.top = y - this.options.icon.size * 0.5;
  31. this.boundingBox.left = x - this.options.icon.size * 0.5;
  32. this.boundingBox.right = x + this.options.icon.size * 0.5;
  33. this.boundingBox.bottom = y + this.options.icon.size * 0.5;
  34. if (this.options.label !== undefined) {
  35. var iconTextSpacing = 5;
  36. this.labelModule.draw(ctx, x, y + this.height * 0.5 + iconTextSpacing, selected);
  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);
  40. }
  41. }
  42. _icon(ctx, x, y, selected) {
  43. let iconSize = Number(this.options.icon.size);
  44. let relativeIconSize = iconSize * this.body.view.scale;
  45. if (this.options.icon.code && relativeIconSize > this.options.scaling.label.drawThreshold - 1) {
  46. ctx.font = (selected ? "bold " : "") + iconSize + "px " + this.options.icon.fontFace;
  47. // draw icon
  48. ctx.fillStyle = this.options.icon.color || "black";
  49. ctx.textAlign = "center";
  50. ctx.textBaseline = "middle";
  51. ctx.fillText(this.options.icon.code, x, y);
  52. }
  53. }
  54. distanceToBorder(ctx, angle) {
  55. this.resize(ctx);
  56. this._distanceToBorder(angle);
  57. }
  58. }
  59. export default Icon;