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.

74 lines
2.8 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. /**
  4. * A text-based replacement for the default Node shape.
  5. *
  6. * @class Text
  7. * @extends NodeBase
  8. */
  9. class Text extends NodeBase {
  10. /**
  11. * @param {Object} options
  12. * @param {Object} body
  13. * @param {Label} labelModule
  14. * @constructor Text
  15. */
  16. constructor(options, body, labelModule) {
  17. super(options, body, labelModule);
  18. this._setMargins(labelModule);
  19. }
  20. /**
  21. *
  22. * @param {CanvasRenderingContext2D} ctx
  23. * @param {boolean} selected
  24. * @param {boolean} hover
  25. */
  26. resize(ctx, selected, hover) {
  27. if (this.needsRefresh(selected, hover)) {
  28. this.textSize = this.labelModule.getTextSize(ctx, selected, hover);
  29. this.width = this.textSize.width + this.margin.right + this.margin.left;
  30. this.height = this.textSize.height + this.margin.top + this.margin.bottom;
  31. this.radius = 0.5*this.width;
  32. }
  33. }
  34. /**
  35. *
  36. * @param {CanvasRenderingContext2D} ctx
  37. * @param {number} x width
  38. * @param {number} y height
  39. * @param {boolean} selected
  40. * @param {boolean} hover
  41. * @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
  42. */
  43. draw(ctx, x, y, selected, hover, values) {
  44. this.resize(ctx, selected, hover);
  45. this.left = x - this.width / 2;
  46. this.top = y - this.height / 2;
  47. // draw shadow if enabled
  48. this.enableShadow(ctx, values);
  49. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  50. this.top + this.textSize.height / 2 + this.margin.top, selected, hover);
  51. // disable shadows for other elements.
  52. this.disableShadow(ctx, values);
  53. this.updateBoundingBox(x, y, ctx, selected, hover);
  54. }
  55. /**
  56. *
  57. * @param {CanvasRenderingContext2D} ctx
  58. * @param {Number} angle
  59. * @returns {Number}
  60. */
  61. distanceToBorder(ctx, angle) {
  62. return this._distanceToBorder(ctx,angle);
  63. }
  64. }
  65. export default Text;