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.

74 lines
2.2 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. }
  7. resize(ctx, selected) {
  8. if (this.width === undefined) {
  9. let margin = 5;
  10. let textSize = this.labelModule.getTextSize(ctx,selected);
  11. this.width = textSize.width + 2 * margin;
  12. this.height = textSize.height + 2 * margin;
  13. this.radius = 0.5*this.width;
  14. }
  15. }
  16. draw(ctx, x, y, selected, hover) {
  17. this.resize(ctx, selected);
  18. this.left = x - this.width / 2;
  19. this.top = y - this.height / 2;
  20. let borderWidth = this.options.borderWidth;
  21. let selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  22. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  23. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  24. ctx.lineWidth /= this.body.view.scale;
  25. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  26. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  27. let borderRadius = 6;
  28. ctx.roundRect(this.left, this.top, this.width, this.height, borderRadius);
  29. //draw dashed border if enabled
  30. this.enableBorderDashes(ctx);
  31. // draw shadow if enabled
  32. this.enableShadow(ctx);
  33. ctx.fill();
  34. //disable dashed border for other elements
  35. this.disableBorderDashes(ctx);
  36. // disable shadows for other elements.
  37. this.disableShadow(ctx);
  38. ctx.stroke();
  39. this.updateBoundingBox(x,y);
  40. this.labelModule.draw(ctx, x, y, selected);
  41. }
  42. updateBoundingBox(x,y) {
  43. this.left = x - this.width * 0.5;
  44. this.top = y - this.height * 0.5;
  45. this.boundingBox.left = this.left;
  46. this.boundingBox.top = this.top;
  47. this.boundingBox.bottom = this.top + this.height;
  48. this.boundingBox.right = this.left + this.width;
  49. }
  50. distanceToBorder(ctx, angle) {
  51. this.resize(ctx);
  52. let a = this.width / 2;
  53. let b = this.height / 2;
  54. let w = (Math.sin(angle) * a);
  55. let h = (Math.cos(angle) * b);
  56. return a * b / Math.sqrt(w * w + h * h);
  57. }
  58. }
  59. export default Box;