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.

126 lines
3.8 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.options.shapeProperties.useImageSize === false) {
  30. if (this.imageObj.width > this.imageObj.height) {
  31. ratio = this.imageObj.width / this.imageObj.height;
  32. width = this.options.size * 2 * ratio || this.imageObj.width;
  33. height = this.options.size * 2 || this.imageObj.height;
  34. }
  35. else {
  36. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  37. ratio = this.imageObj.height / this.imageObj.width;
  38. }
  39. else {
  40. ratio = 1;
  41. }
  42. width = this.options.size * 2;
  43. height = this.options.size * 2 * ratio;
  44. }
  45. }
  46. else {
  47. // when not using the size property, we use the image size
  48. width = this.imageObj.width;
  49. height = this.imageObj.height;
  50. }
  51. this.width = width;
  52. this.height = height;
  53. this.radius = 0.5 * this.width;
  54. }
  55. }
  56. _drawRawCircle(ctx, x, y, selected, hover, size) {
  57. var borderWidth = this.options.borderWidth;
  58. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  59. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  60. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  61. ctx.lineWidth *= this.networkScaleInv;
  62. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  63. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  64. ctx.circle(x, y, size);
  65. // draw shadow if enabled
  66. this.enableShadow(ctx);
  67. // draw the background
  68. ctx.fill();
  69. // disable shadows for other elements.
  70. this.disableShadow(ctx);
  71. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  72. ctx.save();
  73. this.enableBorderDashes(ctx);
  74. //draw the border
  75. ctx.stroke();
  76. //disable dashed border for other elements
  77. this.disableBorderDashes(ctx);
  78. ctx.restore();
  79. }
  80. _drawImageAtPosition(ctx) {
  81. if (this.imageObj.width != 0) {
  82. // draw the image
  83. ctx.globalAlpha = 1.0;
  84. // draw shadow if enabled
  85. this.enableShadow(ctx);
  86. // draw image
  87. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  88. // disable shadows for other elements.
  89. this.disableShadow(ctx);
  90. }
  91. }
  92. _drawImageLabel(ctx, x, y, selected) {
  93. var yLabel;
  94. var offset = 0;
  95. if (this.height !== undefined) {
  96. offset = this.height * 0.5;
  97. var labelDimensions = this.labelModule.getTextSize(ctx);
  98. if (labelDimensions.lineCount >= 1) {
  99. offset += labelDimensions.height / 2;
  100. }
  101. }
  102. yLabel = y + offset;
  103. if (this.options.label) {
  104. this.labelOffset = offset;
  105. }
  106. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  107. }
  108. }
  109. export default CircleImageBase;