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.

74 lines
2.2 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. class Ellipse extends NodeBase {
  4. constructor(options, body, labelModule) {
  5. super(options, body, labelModule);
  6. }
  7. resize(ctx, selected) {
  8. if (this.width === undefined) {
  9. var textSize = this.labelModule.getTextSize(ctx, selected);
  10. this.width = textSize.width * 1.5;
  11. this.height = textSize.height * 2;
  12. if (this.width < this.height) {
  13. this.width = this.height;
  14. }
  15. this.radius = 0.5*this.width;
  16. }
  17. }
  18. draw(ctx, x, y, selected, hover) {
  19. this.resize(ctx, selected);
  20. this.left = x - this.width * 0.5;
  21. this.top = y - this.height * 0.5;
  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.ellipse(this.left, this.top, this.width, this.height);
  30. // draw shadow if enabled
  31. this.enableShadow(ctx);
  32. ctx.fill();
  33. // disable shadows for other elements.
  34. this.disableShadow(ctx);
  35. ctx.stroke();
  36. this.updateBoundingBox(x, y, ctx, selected);
  37. this.labelModule.draw(ctx, x, y, selected);
  38. }
  39. updateBoundingBox(x, y, ctx, selected) {
  40. this.resize(ctx, selected); // just in case
  41. this.left = x - this.width * 0.5;
  42. this.top = y - this.height * 0.5;
  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. }
  48. distanceToBorder(ctx, angle) {
  49. this.resize(ctx);
  50. var a = this.width * 0.5;
  51. var b = this.height * 0.5;
  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 Ellipse;