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.

71 lines
2.0 KiB

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