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.

133 lines
3.9 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. setOptions(options, imageObj) {
  9. this.options = options;
  10. if (imageObj) {
  11. this.imageObj = imageObj;
  12. }
  13. }
  14. /**
  15. * This function resizes the image by the options size when the image has not yet loaded. If the image has loaded, we
  16. * force the update of the size again.
  17. *
  18. * @private
  19. */
  20. _resizeImage() {
  21. let force = false;
  22. if (!this.imageObj.width || !this.imageObj.height) { // undefined or 0
  23. this.imageLoaded = false;
  24. }
  25. else if (this.imageLoaded === false) {
  26. this.imageLoaded = true;
  27. force = true;
  28. }
  29. if (!this.width || !this.height || force === true) { // undefined or 0
  30. var width, height, ratio;
  31. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  32. width = 0;
  33. height = 0;
  34. }
  35. if (this.options.shapeProperties.useImageSize === false) {
  36. if (this.imageObj.width > this.imageObj.height) {
  37. ratio = this.imageObj.width / this.imageObj.height;
  38. width = this.options.size * 2 * ratio || this.imageObj.width;
  39. height = this.options.size * 2 || this.imageObj.height;
  40. }
  41. else {
  42. if (this.imageObj.width && this.imageObj.height) { // not undefined or 0
  43. ratio = this.imageObj.height / this.imageObj.width;
  44. }
  45. else {
  46. ratio = 1;
  47. }
  48. width = this.options.size * 2;
  49. height = this.options.size * 2 * ratio;
  50. }
  51. }
  52. else {
  53. // when not using the size property, we use the image size
  54. width = this.imageObj.width;
  55. height = this.imageObj.height;
  56. }
  57. this.width = width;
  58. this.height = height;
  59. this.radius = 0.5 * this.width;
  60. }
  61. }
  62. _drawRawCircle(ctx, x, y, selected, hover, size) {
  63. var borderWidth = this.options.borderWidth;
  64. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  65. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  66. ctx.lineWidth = (selected ? selectionLineWidth : borderWidth);
  67. ctx.lineWidth *= this.networkScaleInv;
  68. ctx.lineWidth = Math.min(this.width, ctx.lineWidth);
  69. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  70. ctx.circle(x, y, size);
  71. // draw shadow if enabled
  72. this.enableShadow(ctx);
  73. // draw the background
  74. ctx.fill();
  75. // disable shadows for other elements.
  76. this.disableShadow(ctx);
  77. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  78. ctx.save();
  79. this.enableBorderDashes(ctx);
  80. //draw the border
  81. ctx.stroke();
  82. //disable dashed border for other elements
  83. this.disableBorderDashes(ctx);
  84. ctx.restore();
  85. }
  86. _drawImageAtPosition(ctx) {
  87. if (this.imageObj.width != 0) {
  88. // draw the image
  89. ctx.globalAlpha = 1.0;
  90. // draw shadow if enabled
  91. this.enableShadow(ctx);
  92. // draw image
  93. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  94. // disable shadows for other elements.
  95. this.disableShadow(ctx);
  96. }
  97. }
  98. _drawImageLabel(ctx, x, y, selected) {
  99. var yLabel;
  100. var offset = 0;
  101. if (this.height !== undefined) {
  102. offset = this.height * 0.5;
  103. var labelDimensions = this.labelModule.getTextSize(ctx);
  104. if (labelDimensions.lineCount >= 1) {
  105. offset += labelDimensions.height / 2;
  106. }
  107. }
  108. yLabel = y + offset;
  109. if (this.options.label) {
  110. this.labelOffset = offset;
  111. }
  112. this.labelModule.draw(ctx, x, yLabel, selected, 'hanging');
  113. }
  114. }
  115. export default CircleImageBase;