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.

80 lines
2.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. ctx.strokeStyle = values.borderColor;
  21. ctx.lineWidth = values.borderWidth;
  22. ctx.lineWidth /= this.body.view.scale;
  23. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  24. ctx.fillStyle = values.color;
  25. ctx.roundRect(this.left, this.top, this.width, this.height, values.borderRadius);
  26. // draw shadow if enabled
  27. this.enableShadow(ctx, values);
  28. // draw the background
  29. ctx.fill();
  30. // disable shadows for other elements.
  31. this.disableShadow(ctx, values);
  32. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  33. ctx.save();
  34. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  35. if (values.borderWidth > 0) {
  36. this.enableBorderDashes(ctx, values);
  37. //draw the border
  38. ctx.stroke();
  39. //disable dashed border for other elements
  40. this.disableBorderDashes(ctx, values);
  41. }
  42. ctx.restore();
  43. this.updateBoundingBox(x, y, ctx, selected, hover);
  44. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  45. this.top + this.textSize.height / 2 + this.margin.top, selected, hover);
  46. }
  47. updateBoundingBox(x, y, ctx, selected, hover) {
  48. this.resize(ctx, selected, hover);
  49. this.left = x - this.width / 2;
  50. this.top = y - this.height / 2;
  51. let borderRadius = this.options.shapeProperties.borderRadius; // only effective for box
  52. this.boundingBox.left = this.left - borderRadius;
  53. this.boundingBox.top = this.top - borderRadius;
  54. this.boundingBox.bottom = this.top + this.height + borderRadius;
  55. this.boundingBox.right = this.left + this.width + borderRadius;
  56. }
  57. distanceToBorder(ctx, angle) {
  58. this.resize(ctx);
  59. let borderWidth = this.options.borderWidth;
  60. return Math.min(
  61. Math.abs((this.width) / 2 / Math.cos(angle)),
  62. Math.abs((this.height) / 2 / Math.sin(angle))) + borderWidth;
  63. }
  64. }
  65. export default Box;