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.

58 lines
1.8 KiB

  1. /**
  2. * Created by Alex on 3/18/2015.
  3. */
  4. 'use strict';
  5. import NodeBase from '../util/NodeBase'
  6. class Box extends NodeBase {
  7. constructor (options, body, labelModule) {
  8. super(options,body,labelModule);
  9. }
  10. resize(ctx) {
  11. if (this.width === undefined) {
  12. var margin = 5;
  13. var textSize = this.labelModule.getTextSize(ctx,this.selected);
  14. this.width = textSize.width + 2 * margin;
  15. this.height = textSize.height + 2 * margin;
  16. }
  17. }
  18. draw(ctx, x, y, selected, hover) {
  19. this.resize(ctx);
  20. this.left = x - this.width / 2;
  21. this.top = y - this.height / 2;
  22. var borderWidth = this.options.borderWidth;
  23. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  24. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  25. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  26. ctx.lineWidth /= this.body.view.scale;
  27. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  28. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  29. ctx.roundRect(this.left, this.top, this.width, this.height, this.options.size);
  30. ctx.fill();
  31. ctx.stroke();
  32. this.boundingBox.top = this.top;
  33. this.boundingBox.left = this.left;
  34. this.boundingBox.right = this.left + this.width;
  35. this.boundingBox.bottom = this.top + this.height;
  36. this.labelModule.draw(ctx, x, y, selected);
  37. }
  38. distanceToBorder(ctx, angle) {
  39. this.resize(ctx);
  40. var a = this.width / 2;
  41. var b = this.height / 2;
  42. var w = (Math.sin(angle) * a);
  43. var h = (Math.cos(angle) * b);
  44. return a * b / Math.sqrt(w * w + h * h);
  45. }
  46. }
  47. export default Box;