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 BaseNode from '../util/baseNode'
  6. class Box extends BaseNode {
  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. console.log(this)
  25. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  26. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  27. ctx.lineWidth /= this.body.view.scale;
  28. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  29. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  30. ctx.roundRect(this.left, this.top, this.width, this.height, this.options.size);
  31. ctx.fill();
  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;