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.

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