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.

84 lines
3.0 KiB

9 years ago
  1. 'use strict';
  2. import NodeBase from '../util/NodeBase'
  3. class Box extends NodeBase {
  4. constructor (options, body, labelModule) {
  5. super(options,body,labelModule);
  6. this._setMargins(labelModule);
  7. }
  8. resize(ctx, selected) {
  9. if (this.width === undefined) {
  10. this.textSize = this.labelModule.getTextSize(ctx,selected);
  11. this.width = this.textSize.width + this.margin.right + this.margin.left;
  12. this.height = this.textSize.height + this.margin.top + this.margin.bottom;
  13. this.radius = this.width / 2;
  14. }
  15. }
  16. draw(ctx, x, y, selected, hover) {
  17. this.resize(ctx, selected);
  18. this.left = x - this.width / 2;
  19. this.top = y - this.height / 2;
  20. let borderWidth = this.options.borderWidth;
  21. let 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. let borderRadius = this.options.shapeProperties.borderRadius; // only effective for box
  28. ctx.roundRect(this.left, this.top, this.width, this.height, borderRadius);
  29. // draw shadow if enabled
  30. this.enableShadow(ctx);
  31. // draw the background
  32. ctx.fill();
  33. // disable shadows for other elements.
  34. this.disableShadow(ctx);
  35. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  36. ctx.save();
  37. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  38. if (borderWidth > 0) {
  39. this.enableBorderDashes(ctx);
  40. //draw the border
  41. ctx.stroke();
  42. //disable dashed border for other elements
  43. this.disableBorderDashes(ctx);
  44. }
  45. ctx.restore();
  46. this.updateBoundingBox(x,y,ctx,selected);
  47. this.labelModule.draw(ctx, this.left + this.textSize.width / 2 + this.margin.left,
  48. this.top + this.textSize.height / 2 + this.margin.top, selected);
  49. }
  50. updateBoundingBox(x,y, ctx, selected) {
  51. this.resize(ctx, selected);
  52. this.left = x - this.width / 2;
  53. this.top = y - this.height / 2;
  54. let borderRadius = this.options.shapeProperties.borderRadius; // only effective for box
  55. this.boundingBox.left = this.left - borderRadius;
  56. this.boundingBox.top = this.top - borderRadius;
  57. this.boundingBox.bottom = this.top + this.height + borderRadius;
  58. this.boundingBox.right = this.left + this.width + borderRadius;
  59. }
  60. distanceToBorder(ctx, angle) {
  61. this.resize(ctx);
  62. let borderWidth = this.options.borderWidth;
  63. return Math.min(
  64. Math.abs((this.width) / 2 / Math.cos(angle)),
  65. Math.abs((this.height) / 2 / Math.sin(angle))) + borderWidth;
  66. }
  67. }
  68. export default Box;