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.

92 lines
3.4 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. /**
  4. * A Box Node/Cluster shape.
  5. *
  6. * @class Box
  7. * @extends NodeBase
  8. */
  9. class Box extends NodeBase {
  10. /**
  11. * @param {Object} options
  12. * @param {Object} body
  13. * @param {Label} labelModule
  14. * @constructor Box
  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 = this.selected, hover = this.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 = this.width / 2;
  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. this.initContextForDraw(ctx, values);
  48. ctx.roundRect(this.left, this.top, this.width, this.height, values.borderRadius);
  49. this.performFill(ctx, values);
  50. this.updateBoundingBox(x, y, ctx, selected, hover);
  51. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  52. this.top + this.textSize.height / 2 + this.margin.top, selected, hover);
  53. }
  54. /**
  55. *
  56. * @param {number} x width
  57. * @param {number} y height
  58. * @param {CanvasRenderingContext2D} ctx
  59. * @param {boolean} selected
  60. * @param {boolean} hover
  61. */
  62. updateBoundingBox(x, y, ctx, selected, hover) {
  63. this._updateBoundingBox(x, y, ctx, selected, hover);
  64. let borderRadius = this.options.shapeProperties.borderRadius; // only effective for box
  65. this._addBoundingBoxMargin(borderRadius);
  66. }
  67. /**
  68. *
  69. * @param {CanvasRenderingContext2D} ctx
  70. * @param {number} angle
  71. * @returns {number}
  72. */
  73. distanceToBorder(ctx, angle) {
  74. this.resize(ctx);
  75. let borderWidth = this.options.borderWidth;
  76. return Math.min(
  77. Math.abs((this.width) / 2 / Math.cos(angle)),
  78. Math.abs((this.height) / 2 / Math.sin(angle))) + borderWidth;
  79. }
  80. }
  81. export default Box;