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.

65 lines
2.1 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. ctx.fillText(this.options.icon.code, x, y);
  46. }
  47. }
  48. distanceToBorder(ctx, angle) {
  49. this.resize(ctx);
  50. this._distanceToBorder(angle);
  51. }
  52. }
  53. export default Icon;