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.

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