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.

74 lines
2.4 KiB

  1. import NodeBase from '../util/NodeBase'
  2. class ShapeBase extends NodeBase {
  3. constructor(options, body, labelModule) {
  4. super(options, body, labelModule)
  5. }
  6. _resizeShape(selected = this.selected, hover = this.hover, values = { size: this.options.size }) {
  7. if (this.needsRefresh(selected, hover)) {
  8. var size = 2 * values.size;
  9. this.width = size;
  10. this.height = size;
  11. this.radius = 0.5*this.width;
  12. }
  13. }
  14. _drawShape(ctx, shape, sizeMultiplier, x, y, selected, hover, values) {
  15. this._resizeShape(selected, hover, values);
  16. this.left = x - this.width / 2;
  17. this.top = y - this.height / 2;
  18. var borderWidth = values.borderWidth / this.body.view.scale;
  19. ctx.lineWidth = Math.min(this.width, borderWidth);
  20. ctx.strokeStyle = values.borderColor;
  21. ctx.fillStyle = values.color;
  22. ctx[shape](x, y, values.size);
  23. // draw shadow if enabled
  24. this.enableShadow(ctx, values);
  25. // draw the background
  26. ctx.fill();
  27. // disable shadows for other elements.
  28. this.disableShadow(ctx, values);
  29. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  30. ctx.save();
  31. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  32. if (borderWidth > 0) {
  33. this.enableBorderDashes(ctx, values);
  34. //draw the border
  35. ctx.stroke();
  36. //disable dashed border for other elements
  37. this.disableBorderDashes(ctx, values);
  38. }
  39. ctx.restore();
  40. if (this.options.label !== undefined) {
  41. let yLabel = y + 0.5 * this.height + 3; // the + 3 is to offset it a bit below the node.
  42. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  43. }
  44. this.updateBoundingBox(x,y);
  45. }
  46. updateBoundingBox(x,y) {
  47. this.boundingBox.top = y - this.options.size;
  48. this.boundingBox.left = x - this.options.size;
  49. this.boundingBox.right = x + this.options.size;
  50. this.boundingBox.bottom = y + this.options.size;
  51. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  52. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  53. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  54. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height + 3);
  55. }
  56. }
  57. }
  58. export default ShapeBase;