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.

72 lines
2.1 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);
  37. this.labelModule.draw(ctx, x, y, selected);
  38. }
  39. updateBoundingBox(x,y) {
  40. this.left = x - this.width * 0.5;
  41. this.top = y - this.height * 0.5;
  42. this.boundingBox.left = this.left;
  43. this.boundingBox.top = this.top;
  44. this.boundingBox.bottom = this.top + this.height;
  45. this.boundingBox.right = this.left + this.width;
  46. }
  47. distanceToBorder(ctx, angle) {
  48. this.resize(ctx);
  49. var a = this.width * 0.5;
  50. var b = this.height * 0.5;
  51. var w = (Math.sin(angle) * a);
  52. var h = (Math.cos(angle) * b);
  53. return a * b / Math.sqrt(w * w + h * h);
  54. }
  55. }
  56. export default Ellipse;