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.8 KiB

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