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.

91 lines
2.4 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. /**
  4. * A Box Node/Cluster shape.
  5. *
  6. * @extends NodeBase
  7. */
  8. class Box 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 = this.selected, hover = this.hover) {
  25. if (this.needsRefresh(selected, hover)) {
  26. var dimensions = this.getDimensionsFromLabel(ctx, selected, hover);
  27. this.width = dimensions.width + this.margin.right + this.margin.left;
  28. this.height = dimensions.height + this.margin.top + this.margin.bottom;
  29. this.radius = this.width / 2;
  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 {ArrowOptions} 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. this.initContextForDraw(ctx, values);
  46. ctx.roundRect(this.left, this.top, this.width, this.height, values.borderRadius);
  47. this.performFill(ctx, values);
  48. this.updateBoundingBox(x, y, ctx, selected, hover);
  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. }
  52. /**
  53. *
  54. * @param {number} x width
  55. * @param {number} y height
  56. * @param {CanvasRenderingContext2D} ctx
  57. * @param {boolean} selected
  58. * @param {boolean} hover
  59. */
  60. updateBoundingBox(x, y, ctx, selected, hover) {
  61. this._updateBoundingBox(x, y, ctx, selected, hover);
  62. let borderRadius = this.options.shapeProperties.borderRadius; // only effective for box
  63. this._addBoundingBoxMargin(borderRadius);
  64. }
  65. /**
  66. *
  67. * @param {CanvasRenderingContext2D} ctx
  68. * @param {number} angle
  69. * @returns {number}
  70. */
  71. distanceToBorder(ctx, angle) {
  72. this.resize(ctx);
  73. let borderWidth = this.options.borderWidth;
  74. return Math.min(
  75. Math.abs((this.width) / 2 / Math.cos(angle)),
  76. Math.abs((this.height) / 2 / Math.sin(angle))) + borderWidth;
  77. }
  78. }
  79. export default Box;