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.

114 lines
3.4 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 dashed border if enabled
  59. this.enableBorderDashes(ctx);
  60. // draw shadow if enabled
  61. this.enableShadow(ctx);
  62. ctx.fill();
  63. //disable dashed border for other elements
  64. this.disableBorderDashes(ctx);
  65. // disable shadows for other elements.
  66. this.disableShadow(ctx);
  67. ctx.stroke();
  68. }
  69. _drawImageAtPosition(ctx) {
  70. if (this.imageObj.width != 0) {
  71. // draw the image
  72. ctx.globalAlpha = 1.0;
  73. // draw shadow if enabled
  74. this.enableShadow(ctx);
  75. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  76. // disable shadows for other elements.
  77. this.disableShadow(ctx);
  78. }
  79. }
  80. _drawImageLabel(ctx, x, y, selected) {
  81. var yLabel;
  82. var offset = 0;
  83. if (this.height !== undefined) {
  84. offset = this.height * 0.5;
  85. var labelDimensions = this.labelModule.getTextSize(ctx);
  86. if (labelDimensions.lineCount >= 1) {
  87. offset += labelDimensions.height / 2;
  88. }
  89. }
  90. yLabel = y + offset;
  91. if (this.options.label) {
  92. this.labelOffset = offset;
  93. }
  94. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  95. }
  96. }
  97. export default CircleImageBase;