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.

53 lines
1.5 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. class Text extends NodeBase {
  4. constructor(options, body, labelModule) {
  5. super(options, body, labelModule);
  6. this._setMargins(labelModule);
  7. }
  8. resize(ctx, selected) {
  9. if (this.width === undefined) {
  10. this.textSize = this.labelModule.getTextSize(ctx,selected);
  11. this.width = this.textSize.width + this.margin.right + this.margin.left;
  12. this.height = this.textSize.height + this.margin.top + this.margin.bottom;
  13. this.radius = 0.5*this.width;
  14. }
  15. }
  16. draw(ctx, x, y, selected, hover) {
  17. this.resize(ctx, selected || hover);
  18. this.left = x - this.width / 2;
  19. this.top = y - this.height / 2;
  20. // draw shadow if enabled
  21. this.enableShadow(ctx);
  22. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  23. this.top + this.textSize.height / 2 + this.margin.top, selected || hover);
  24. // disable shadows for other elements.
  25. this.disableShadow(ctx);
  26. this.updateBoundingBox(x, y, ctx, selected);
  27. }
  28. updateBoundingBox(x, y, ctx, selected) {
  29. this.resize(ctx, selected);
  30. this.left = x - this.width / 2;
  31. this.top = y - this.height / 2;
  32. this.boundingBox.top = this.top;
  33. this.boundingBox.left = this.left;
  34. this.boundingBox.right = this.left + this.width;
  35. this.boundingBox.bottom = this.top + this.height;
  36. }
  37. distanceToBorder(ctx, angle) {
  38. return this._distanceToBorder(ctx,angle);
  39. }
  40. }
  41. export default Text;