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. '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. }
  16. }
  17. draw(ctx, x, y, selected, hover) {
  18. this.resize(ctx, selected);
  19. this.left = x - this.width * 0.5;
  20. this.top = y - this.height * 0.5;
  21. var borderWidth = this.options.borderWidth;
  22. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  23. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  24. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  25. ctx.lineWidth /= this.body.view.scale;
  26. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  27. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  28. ctx.ellipse(this.left, this.top, this.width, this.height);
  29. // draw shadow if enabled
  30. this.enableShadow(ctx);
  31. ctx.fill();
  32. // disable shadows for other elements.
  33. this.disableShadow(ctx);
  34. ctx.stroke();
  35. this.updateBoundingBox(x,y);
  36. this.labelModule.draw(ctx, x, y, selected);
  37. }
  38. updateBoundingBox(x,y) {
  39. this.left = x - this.width * 0.5;
  40. this.top = y - this.height * 0.5;
  41. this.boundingBox.left = this.left;
  42. this.boundingBox.top = this.top;
  43. this.boundingBox.bottom = this.top + this.height;
  44. this.boundingBox.right = this.left + this.width;
  45. }
  46. distanceToBorder(ctx, angle) {
  47. this.resize(ctx);
  48. var a = this.width * 0.5;
  49. var b = this.height * 0.5;
  50. var w = (Math.sin(angle) * a);
  51. var h = (Math.cos(angle) * b);
  52. return a * b / Math.sqrt(w * w + h * h);
  53. }
  54. }
  55. export default Ellipse;