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.

68 lines
2.1 KiB

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