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.

51 lines
1.7 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. class Box extends NodeBase {
  4. constructor (options, body, labelModule) {
  5. super(options,body,labelModule);
  6. this._setMargins(labelModule);
  7. }
  8. resize(ctx, selected = this.selected, hover = this.hover) {
  9. if (this.needsRefresh(selected, hover)) {
  10. this.textSize = this.labelModule.getTextSize(ctx, selected, hover);
  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 = this.width / 2;
  14. }
  15. }
  16. draw(ctx, x, y, selected, hover, values) {
  17. this.resize(ctx, selected, hover);
  18. this.left = x - this.width / 2;
  19. this.top = y - this.height / 2;
  20. this.initContextForDraw(ctx, values);
  21. ctx.roundRect(this.left, this.top, this.width, this.height, values.borderRadius);
  22. this.performFill(ctx, values);
  23. this.updateBoundingBox(x, y, ctx, selected, hover);
  24. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  25. this.top + this.textSize.height / 2 + this.margin.top, selected, hover);
  26. }
  27. updateBoundingBox(x, y, ctx, selected, hover) {
  28. this._updateBoundingBox(x, y, ctx, selected, hover);
  29. let borderRadius = this.options.shapeProperties.borderRadius; // only effective for box
  30. this._addBoundingBoxMargin(borderRadius);
  31. }
  32. distanceToBorder(ctx, angle) {
  33. this.resize(ctx);
  34. let borderWidth = this.options.borderWidth;
  35. return Math.min(
  36. Math.abs((this.width) / 2 / Math.cos(angle)),
  37. Math.abs((this.height) / 2 / Math.sin(angle))) + borderWidth;
  38. }
  39. }
  40. export default Box;