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.

119 lines
3.6 KiB

  1. import NodeBase from '../util/NodeBase'
  2. class CircleImageBase extends NodeBase {
  3. constructor(options, body, labelModule) {
  4. super(options, body, labelModule);
  5. this.labelOffset = 0;
  6. this.imageLoaded = false;
  7. }
  8. /**
  9. * This function resizes the image by the options size when the image has not yet loaded. If the image has loaded, we
  10. * force the update of the size again.
  11. *
  12. * @private
  13. */
  14. _resizeImage() {
  15. let force = false;
  16. if (!this.imageObj.width || !this.imageObj.height) { // undefined or 0
  17. this.imageLoaded = false;
  18. }
  19. else if (this.imageLoaded === false) {
  20. this.imageLoaded = true;
  21. force = true;
  22. }
  23. if (!this.width || !this.height || force === true) { // undefined or 0
  24. var width, height, ratio;
  25. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  26. width = 0;
  27. height = 0;
  28. }
  29. if (this.imageObj.width > this.imageObj.height) {
  30. ratio = this.imageObj.width / this.imageObj.height;
  31. width = this.options.size * 2 * ratio || this.imageObj.width;
  32. height = this.options.size * 2 || this.imageObj.height;
  33. }
  34. else {
  35. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  36. ratio = this.imageObj.height / this.imageObj.width;
  37. }
  38. else {
  39. ratio = 1;
  40. }
  41. width = this.options.size * 2 || this.imageObj.width;
  42. height = this.options.size * 2 * ratio || this.imageObj.height;
  43. }
  44. this.width = width;
  45. this.height = height;
  46. this.radius = 0.5 * this.width;
  47. }
  48. }
  49. _drawRawCircle(ctx, x, y, selected, hover, size) {
  50. var borderWidth = this.options.borderWidth;
  51. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  52. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  53. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  54. ctx.lineWidth *= this.networkScaleInv;
  55. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  56. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  57. ctx.circle(x, y, size);
  58. // draw shadow if enabled
  59. this.enableShadow(ctx);
  60. // draw the background
  61. ctx.fill();
  62. // disable shadows for other elements.
  63. this.disableShadow(ctx);
  64. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  65. ctx.save();
  66. this.enableBorderDashes(ctx);
  67. //draw the border
  68. ctx.stroke();
  69. //disable dashed border for other elements
  70. this.disableBorderDashes(ctx);
  71. ctx.restore();
  72. }
  73. _drawImageAtPosition(ctx) {
  74. if (this.imageObj.width != 0) {
  75. // draw the image
  76. ctx.globalAlpha = 1.0;
  77. // draw shadow if enabled
  78. this.enableShadow(ctx);
  79. // draw image
  80. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  81. // disable shadows for other elements.
  82. this.disableShadow(ctx);
  83. }
  84. }
  85. _drawImageLabel(ctx, x, y, selected) {
  86. var yLabel;
  87. var offset = 0;
  88. if (this.height !== undefined) {
  89. offset = this.height * 0.5;
  90. var labelDimensions = this.labelModule.getTextSize(ctx);
  91. if (labelDimensions.lineCount >= 1) {
  92. offset += labelDimensions.height / 2;
  93. }
  94. }
  95. yLabel = y + offset;
  96. if (this.options.label) {
  97. this.labelOffset = offset;
  98. }
  99. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  100. }
  101. }
  102. export default CircleImageBase;