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.

84 lines
2.6 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 neutralborderWidth = this.options.borderWidth;
  23. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  24. var borderWidth = (selected ? selectionLineWidth : neutralborderWidth) / this.body.view.scale;
  25. ctx.lineWidth = Math.min(this.width, borderWidth);
  26. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  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. // draw the background
  32. ctx.fill();
  33. // disable shadows for other elements.
  34. this.disableShadow(ctx);
  35. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  36. ctx.save();
  37. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  38. if (borderWidth > 0) {
  39. this.enableBorderDashes(ctx);
  40. //draw the border
  41. ctx.stroke();
  42. //disable dashed border for other elements
  43. this.disableBorderDashes(ctx);
  44. }
  45. ctx.restore();
  46. this.updateBoundingBox(x, y, ctx, selected);
  47. this.labelModule.draw(ctx, x, y, selected);
  48. }
  49. updateBoundingBox(x, y, ctx, selected) {
  50. this.resize(ctx, selected); // just in case
  51. this.left = x - this.width * 0.5;
  52. this.top = y - this.height * 0.5;
  53. this.boundingBox.left = this.left;
  54. this.boundingBox.top = this.top;
  55. this.boundingBox.bottom = this.top + this.height;
  56. this.boundingBox.right = this.left + this.width;
  57. }
  58. distanceToBorder(ctx, angle) {
  59. this.resize(ctx);
  60. var a = this.width * 0.5;
  61. var b = this.height * 0.5;
  62. var w = (Math.sin(angle) * a);
  63. var h = (Math.cos(angle) * b);
  64. return a * b / Math.sqrt(w * w + h * h);
  65. }
  66. }
  67. export default Ellipse;