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.

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