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.

60 lines
1.8 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. /**
  4. * A Box Node/Cluster shape.
  5. *
  6. * @param {Object} options
  7. * @param {Object} body
  8. * @param {Label} labelModule
  9. * @constructor Box
  10. * @extends NodeBase
  11. */
  12. class Box extends NodeBase {
  13. constructor (options, body, labelModule) {
  14. super(options,body,labelModule);
  15. this._setMargins(labelModule);
  16. }
  17. resize(ctx, selected = this.selected, hover = this.hover) {
  18. if (this.needsRefresh(selected, hover)) {
  19. this.textSize = this.labelModule.getTextSize(ctx, selected, hover);
  20. this.width = this.textSize.width + this.margin.right + this.margin.left;
  21. this.height = this.textSize.height + this.margin.top + this.margin.bottom;
  22. this.radius = this.width / 2;
  23. }
  24. }
  25. draw(ctx, x, y, selected, hover, values) {
  26. this.resize(ctx, selected, hover);
  27. this.left = x - this.width / 2;
  28. this.top = y - this.height / 2;
  29. this.initContextForDraw(ctx, values);
  30. ctx.roundRect(this.left, this.top, this.width, this.height, values.borderRadius);
  31. this.performFill(ctx, values);
  32. this.updateBoundingBox(x, y, ctx, selected, hover);
  33. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  34. this.top + this.textSize.height / 2 + this.margin.top, selected, hover);
  35. }
  36. updateBoundingBox(x, y, ctx, selected, hover) {
  37. this._updateBoundingBox(x, y, ctx, selected, hover);
  38. let borderRadius = this.options.shapeProperties.borderRadius; // only effective for box
  39. this._addBoundingBoxMargin(borderRadius);
  40. }
  41. distanceToBorder(ctx, angle) {
  42. this.resize(ctx);
  43. let borderWidth = this.options.borderWidth;
  44. return Math.min(
  45. Math.abs((this.width) / 2 / Math.cos(angle)),
  46. Math.abs((this.height) / 2 / Math.sin(angle))) + borderWidth;
  47. }
  48. }
  49. export default Box;