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.

52 lines
2.0 KiB

  1. /**
  2. * Created by Alex on 3/19/2015.
  3. */
  4. import NodeBase from '../util/NodeBase'
  5. class ShapeBase extends NodeBase {
  6. constructor(options, body, labelModule) {
  7. super(options, body, labelModule)
  8. }
  9. _resizeShape() {
  10. if (this.width === undefined) {
  11. var size = 2 * this.options.size;
  12. this.width = size;
  13. this.height = size;
  14. }
  15. }
  16. _drawShape(ctx, shape, sizeMultiplier, x, y, selected, hover) {
  17. this._resizeShape();
  18. this.left = x - this.width / 2;
  19. this.top = y - this.height / 2;
  20. var borderWidth = this.options.borderWidth;
  21. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  22. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  23. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  24. ctx.lineWidth /= this.body.view.scale;
  25. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  26. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  27. ctx[shape](x, y, this.options.size + sizeMultiplier * ctx.lineWidth);
  28. ctx.fill();
  29. ctx.stroke();
  30. this.boundingBox.top = y - this.options.size;
  31. this.boundingBox.left = x - this.options.size;
  32. this.boundingBox.right = x + this.options.size;
  33. this.boundingBox.bottom = y + this.options.size;
  34. if (this.options.label!== undefined) {
  35. let yLabel = y + 0.5 * this.height + 3 + sizeMultiplier * ctx.lineWidth; // the + 3 is to offset it a bit below the node.
  36. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  37. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  38. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  39. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  40. }
  41. }
  42. }
  43. export default ShapeBase;