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.

78 lines
2.3 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 dashed border if enabled
  31. this.enableBorderDashes(ctx);
  32. // draw shadow if enabled
  33. this.enableShadow(ctx);
  34. ctx.fill();
  35. //disable dashed border for other elements
  36. this.disableBorderDashes(ctx);
  37. // disable shadows for other elements.
  38. this.disableShadow(ctx);
  39. ctx.stroke();
  40. this.updateBoundingBox(x, y, ctx, selected);
  41. this.labelModule.draw(ctx, x, y, selected);
  42. }
  43. updateBoundingBox(x, y, ctx, selected) {
  44. this.resize(ctx, selected); // just in case
  45. this.left = x - this.width * 0.5;
  46. this.top = y - this.height * 0.5;
  47. this.boundingBox.left = this.left;
  48. this.boundingBox.top = this.top;
  49. this.boundingBox.bottom = this.top + this.height;
  50. this.boundingBox.right = this.left + this.width;
  51. }
  52. distanceToBorder(ctx, angle) {
  53. this.resize(ctx);
  54. var a = this.width * 0.5;
  55. var b = this.height * 0.5;
  56. var w = (Math.sin(angle) * a);
  57. var h = (Math.cos(angle) * b);
  58. return a * b / Math.sqrt(w * w + h * h);
  59. }
  60. }
  61. export default Ellipse;