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.

81 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.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. // draw the background
  33. ctx.fill();
  34. // disable shadows for other elements.
  35. this.disableShadow(ctx);
  36. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  37. ctx.save();
  38. this.enableBorderDashes(ctx);
  39. //draw the border
  40. ctx.stroke();
  41. //disable dashed border for other elements
  42. this.disableBorderDashes(ctx);
  43. ctx.restore();
  44. this.updateBoundingBox(x, y, ctx, selected);
  45. this.labelModule.draw(ctx, x, y, selected);
  46. }
  47. updateBoundingBox(x, y, ctx, selected) {
  48. this.resize(ctx, selected); // just in case
  49. this.left = x - this.width * 0.5;
  50. this.top = y - this.height * 0.5;
  51. this.boundingBox.left = this.left;
  52. this.boundingBox.top = this.top;
  53. this.boundingBox.bottom = this.top + this.height;
  54. this.boundingBox.right = this.left + this.width;
  55. }
  56. distanceToBorder(ctx, angle) {
  57. this.resize(ctx);
  58. var a = this.width * 0.5;
  59. var b = this.height * 0.5;
  60. var w = (Math.sin(angle) * a);
  61. var h = (Math.cos(angle) * b);
  62. return a * b / Math.sqrt(w * w + h * h);
  63. }
  64. }
  65. export default Ellipse;