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.

67 lines
2.0 KiB

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