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.7 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. /**
  4. * A text-based replacement for the default Node shape.
  5. *
  6. * @extends NodeBase
  7. */
  8. class Text extends NodeBase {
  9. /**
  10. * @param {Object} options
  11. * @param {Object} body
  12. * @param {Label} labelModule
  13. */
  14. constructor(options, body, labelModule) {
  15. super(options, body, labelModule);
  16. this._setMargins(labelModule);
  17. }
  18. /**
  19. *
  20. * @param {CanvasRenderingContext2D} ctx
  21. * @param {boolean} selected
  22. * @param {boolean} hover
  23. */
  24. resize(ctx, selected, hover) {
  25. if (this.needsRefresh(selected, hover)) {
  26. this.textSize = this.labelModule.getTextSize(ctx, selected, hover);
  27. this.width = this.textSize.width + this.margin.right + this.margin.left;
  28. this.height = this.textSize.height + this.margin.top + this.margin.bottom;
  29. this.radius = 0.5*this.width;
  30. }
  31. }
  32. /**
  33. *
  34. * @param {CanvasRenderingContext2D} ctx
  35. * @param {number} x width
  36. * @param {number} y height
  37. * @param {boolean} selected
  38. * @param {boolean} hover
  39. * @param {{toArrow: boolean, toArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), toArrowType: *, middleArrow: boolean, middleArrowScale: (number|allOptions.edges.arrows.middle.scaleFactor|{number}|Array), middleArrowType: (allOptions.edges.arrows.middle.type|{string}|string|*), fromArrow: boolean, fromArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), fromArrowType: *, arrowStrikethrough: (*|boolean|allOptions.edges.arrowStrikethrough|{boolean}), color: undefined, inheritsColor: (string|string|string|allOptions.edges.color.inherit|{string, boolean}|Array|*), opacity: *, hidden: *, length: *, shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *, dashes: (*|boolean|Array|allOptions.edges.dashes|{boolean, array}), width: *}} values
  40. */
  41. draw(ctx, x, y, selected, hover, values) {
  42. this.resize(ctx, selected, hover);
  43. this.left = x - this.width / 2;
  44. this.top = y - this.height / 2;
  45. // draw shadow if enabled
  46. this.enableShadow(ctx, values);
  47. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  48. this.top + this.textSize.height / 2 + this.margin.top, selected, hover);
  49. // disable shadows for other elements.
  50. this.disableShadow(ctx, values);
  51. this.updateBoundingBox(x, y, ctx, selected, hover);
  52. }
  53. /**
  54. *
  55. * @param {CanvasRenderingContext2D} ctx
  56. * @param {number} angle
  57. * @returns {number}
  58. */
  59. distanceToBorder(ctx, angle) {
  60. return this._distanceToBorder(ctx,angle);
  61. }
  62. }
  63. export default Text;