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.

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