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.3 KiB

  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. class Database extends NodeBase {
  4. constructor (options, body, labelModule) {
  5. super(options, body, labelModule);
  6. this._setMargins(labelModule);
  7. }
  8. resize(ctx, selected, hover) {
  9. if ((this.width === undefined) || (this.labelModule.differentState(selected, hover))) {
  10. this.textSize = this.labelModule.getTextSize(ctx, selected, hover);
  11. var size = this.textSize.width + this.margin.right + this.margin.left;
  12. this.width = size;
  13. this.height = size;
  14. this.radius = this.width / 2;
  15. }
  16. }
  17. draw(ctx, x, y, selected, hover, values) {
  18. this.resize(ctx, selected, hover);
  19. this.left = x - this.width / 2;
  20. this.top = y - this.height / 2;
  21. var borderWidth = values.borderWidth / this.body.view.scale;
  22. ctx.lineWidth = Math.min(this.width, borderWidth);
  23. ctx.strokeStyle = values.borderColor;
  24. ctx.fillStyle = values.color;
  25. ctx.database(x - this.width / 2, y - this.height / 2, this.width, this.height);
  26. // draw shadow if enabled
  27. this.enableShadow(ctx, values);
  28. // draw the background
  29. ctx.fill();
  30. // disable shadows for other elements.
  31. this.disableShadow(ctx, values);
  32. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  33. ctx.save();
  34. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  35. if (borderWidth > 0) {
  36. this.enableBorderDashes(ctx, values);
  37. //draw the border
  38. ctx.stroke();
  39. //disable dashed border for other elements
  40. this.disableBorderDashes(ctx, values);
  41. }
  42. ctx.restore();
  43. this.updateBoundingBox(x, y, ctx, selected, hover);
  44. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  45. this.top + this.textSize.height / 2 + this.margin.top, selected, hover);
  46. }
  47. updateBoundingBox(x, y, ctx, selected, hover) {
  48. this.resize(ctx, selected, hover);
  49. this.left = x - this.width * 0.5;
  50. this.top = y - this.height * 0.5;
  51. this.boundingBox.left = this.left;
  52. this.boundingBox.top = this.top;
  53. this.boundingBox.bottom = this.top + this.height;
  54. this.boundingBox.right = this.left + this.width;
  55. }
  56. distanceToBorder(ctx, angle) {
  57. return this._distanceToBorder(ctx,angle);
  58. }
  59. }
  60. export default Database;