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.

54 lines
1.9 KiB

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