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.

77 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. 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. var borderWidth = values.borderWidth / this.body.view.scale;
  20. ctx.lineWidth = Math.min(this.width, borderWidth);
  21. ctx.strokeStyle = values.borderColor;
  22. ctx.fillStyle = values.color;
  23. ctx[shape](x, y, values.size);
  24. // draw shadow if enabled
  25. this.enableShadow(ctx, values);
  26. // draw the background
  27. ctx.fill();
  28. // disable shadows for other elements.
  29. this.disableShadow(ctx, values);
  30. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  31. ctx.save();
  32. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  33. if (borderWidth > 0) {
  34. this.enableBorderDashes(ctx, values);
  35. //draw the border
  36. ctx.stroke();
  37. //disable dashed border for other elements
  38. this.disableBorderDashes(ctx, values);
  39. }
  40. ctx.restore();
  41. if (this.options.label !== undefined) {
  42. // Need to call following here in order to ensure value for `this.labelModule.size.height`
  43. this.labelModule.calculateLabelSize(ctx, selected, hover, x, y, 'hanging')
  44. let yLabel = y + 0.5 * this.height + 0.5 * this.labelModule.size.height;
  45. this.labelModule.draw(ctx, x, yLabel, selected, hover, 'hanging');
  46. }
  47. this.updateBoundingBox(x,y);
  48. }
  49. updateBoundingBox(x,y) {
  50. this.boundingBox.top = y - this.options.size;
  51. this.boundingBox.left = x - this.options.size;
  52. this.boundingBox.right = x + this.options.size;
  53. this.boundingBox.bottom = y + this.options.size;
  54. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  55. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  56. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  57. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelModule.size.height);
  58. }
  59. }
  60. }
  61. export default ShapeBase;