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.

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