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.

63 lines
2.1 KiB

  1. import NodeBase from '../util/NodeBase'
  2. /**
  3. * Base class for constructing Node/Cluster Shapes.
  4. *
  5. * @param {Object} options
  6. * @param {Object} body
  7. * @param {Label} labelModule
  8. * @constructor ShapeBase
  9. * @extends NodeBase
  10. */
  11. class ShapeBase extends NodeBase {
  12. constructor(options, body, labelModule) {
  13. super(options, body, labelModule)
  14. }
  15. resize(ctx, selected = this.selected, hover = this.hover, values = { size: this.options.size }) {
  16. if (this.needsRefresh(selected, hover)) {
  17. this.labelModule.getTextSize(ctx, selected, hover);
  18. var size = 2 * values.size;
  19. this.width = size;
  20. this.height = size;
  21. this.radius = 0.5*this.width;
  22. }
  23. }
  24. _drawShape(ctx, shape, sizeMultiplier, x, y, selected, hover, values) {
  25. this.resize(ctx, selected, hover, values);
  26. this.left = x - this.width / 2;
  27. this.top = y - this.height / 2;
  28. this.initContextForDraw(ctx, values);
  29. ctx[shape](x, y, values.size);
  30. this.performFill(ctx, values);
  31. if (this.options.label !== undefined) {
  32. // Need to call following here in order to ensure value for `this.labelModule.size.height`
  33. this.labelModule.calculateLabelSize(ctx, selected, hover, x, y, 'hanging')
  34. let yLabel = y + 0.5 * this.height + 0.5 * this.labelModule.size.height;
  35. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  36. }
  37. this.updateBoundingBox(x,y);
  38. }
  39. updateBoundingBox(x,y) {
  40. this.boundingBox.top = y - this.options.size;
  41. this.boundingBox.left = x - this.options.size;
  42. this.boundingBox.right = x + this.options.size;
  43. this.boundingBox.bottom = y + this.options.size;
  44. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  45. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  46. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  47. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  48. }
  49. }
  50. }
  51. export default ShapeBase;