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.

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