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.

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